如何强制Access更改DSN较少ODBC连接的用户名和密码?

时间:2011-12-08 16:41:41

标签: ms-access vba access-vba ms-access-2010

前言

我有一个带有MySQL后端的Access 2010前端。我想使用默认的MySQL用户名初始连接到后端数据库并进行密码检查等,然后切换到用户特定的MySQL用户名。

我已经创建了代码来更改连接字符串并使用新的用户名重新连接MySQL表,但Access烦人地记住原始用户名和密码并使用它。

问题

如何强制MS Access 2010忘记连接到ODBC的原始用户名和密码,并在连接字符串中使用新的用户名和密码?

复制

重新创建我的问题

  • MySQL的:
    • 创建一个名为“test”的新架构
    • 在新架构上创建3个表:
      • “TABLE1”
      • “表2”
      • “表3”
    • 创建两个有权访问该架构的新用户:
      • 名称:“SQLUser1”,传递:“Pass01”
      • 名称:“SQLUser2”,传递:“Pass02”
  • 访问结束:
    • 创建新的MS Access 2010项目
    • 创建一个新的空表单
      • 添加名为“Cmd_User1”和“Cmd_User2”的2个按钮
    • 添加以下示例代码
      • 您需要在GenConString()函数中更正服务器名称(sServer =“MySQL”)。
    • 运行表单
    • 点击“Cmd_User1”按钮
    • 点击“Cmd_User2”按钮
  • 现在检查MySQL日志,它将使用用户:“SQLUser1”用于两个连接:(

示例代码:

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 个答案:

答案 0 :(得分:1)

我认为您最好的方法是使用您的第二个用户名只与您的MySQL数据库建立一个连接。至于在检查密码等时的原始连接,你能不能用第一个用户名保存传递查询并运行吗?