How can I force Access to change the user-name and password for a DSN less ODBC connection?

感情迁移 提交于 2019-12-13 01:13:42

问题


Preface

I have a Access 2010 front-end with a MySQL back-end. I want to use a default MySQL user-name to initially connect to the back-end DB and do password checks etc then switch to a user specific MySQL user-name.

I have already created code to change the connection string and reconnect the MySQL tables with the new user-name but Access annoyingly keep remembering the original user-name and password and uses that one.

Question

How can I force MS Access 2010 to forget the original user-name and password to connect to an ODBC and use the new one in the connection string?

Replication

To recreate my problem

  • MySQL:
    • Create a new schema called "test"
    • Create 3 tables on the new schema:
      • "table1"
      • "table2"
      • "table3"
    • Create two new users that have access to that schema:
      • Name: "SQLUser1", Pass: "Pass01"
      • Name: "SQLUser2", Pass: "Pass02"
  • Access End:
    • Create a new MS Access 2010 project
    • Create a new empty form
      • Add 2 buttons called "Cmd_User1" and "Cmd_User2"
    • Add the example code below
      • You will need to correct the server name (sServer = "MySQL") in GenConString() function.
    • Run the form
    • Click "Cmd_User1" button
    • Click "Cmd_User2" button
  • Now check the MySQL logs and it will have used user: "SQLUser1" for both connections :(

Example Code:

Option Compare Database
Option Explicit

Private Sub Cmd_User1_Click()
    'Remove all existing tables
    Call RemoveAllTables
    'Connect the tables
    Call AttachDSNLessTable("table1", "table2", GenConString("SQLUser1", "Pass01"))
    Call AttachDSNLessTable("table2", "table2", GenConString("SQLUser1", "Pass01"))
    Call AttachDSNLessTable("table3", "table3", GenConString("SQLUser1", "Pass01"))
End Sub

Private Sub Cmd_User2_Click()
    'Remove all existing tables
    Call RemoveAllTables
    'Connect the tables
    Call AttachDSNLessTable("table1", "table1", GenConString("SQLUser2", "Pass02"))
    Call AttachDSNLessTable("table2", "table2", GenConString("SQLUser2", "Pass02"))
    Call AttachDSNLessTable("table3", "table3", GenConString("SQLUser2", "Pass02"))
End Sub

Private Sub RemoveAllTables()
    Dim bFound As Boolean, TblDef As DAO.TableDef
    bFound = True 'Force to loop once
    While (bFound = True)
        bFound = False
        For Each TblDef In CurrentDb.TableDefs
            If Not (TblDef.Connect = "") Then
                Call CurrentDb.TableDefs.Delete(TblDef.Name)
                bFound = True
            End If
        Next TblDef
    Wend
End Sub

Private Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stConnect As String)
    On Error GoTo AttachDSNLessTable_Err

    Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
    CurrentDb.TableDefs.Append td
    AttachDSNLessTable = True
    Exit Function

AttachDSNLessTable_Err:
    AttachDSNLessTable = False
    MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description
End Function

Private Function GenConString(ByVal sUserName As String, ByVal sPassword As String) As String
    Dim sConString As String, sServer As String, sDatabase As String, iPort As Integer
    'Pull back all the required fields
    sServer = "MySQL"
    sDatabase = "test"
    iPort = "3306"
    'Generate connection string
    sConString = "ODBC;Driver={MySQL ODBC 5.1 Driver}; " _
                & "Server=" & sServer & "; " _
                & "Database=" & sDatabase & "; " _
                & "UID=" & sUserName & "; " _
                & "PWD=" & sPassword & "; " _
                & "Port=" & iPort & "; " _
                & "Option=3"
    'Return new connection string
    GenConString = sConString
End Function

回答1:


I reckon your best approach is to only have one connection to your MySQL database, using your second username. As for the original connection when you are checking the passwords etc, could you not save a pass-through query with the first username and run with that?



来源:https://stackoverflow.com/questions/8434434/how-can-i-force-access-to-change-the-user-name-and-password-for-a-dsn-less-odbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!