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