我有一个带有MySQL后端的Access 2010前端。我想使用默认的MySQL用户名初始连接到后端数据库并进行密码检查等,然后切换到用户特定的MySQL用户名。
我已经创建了代码来更改连接字符串并使用新的用户名重新连接MySQL表,但Access烦人地记住原始用户名和密码并使用它。
如何强制MS Access 2010忘记连接到ODBC的原始用户名和密码,并在连接字符串中使用新的用户名和密码?
重新创建我的问题
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
答案 0 :(得分:1)
我认为您最好的方法是使用您的第二个用户名只与您的MySQL
数据库建立一个连接。至于在检查密码等时的原始连接,你能不能用第一个用户名保存传递查询并运行吗?