问题
For ease of distribution I want all the mysql linked tables to be DSN-less. I also want to be able to relink the tables to a different server easily (for test purposes), so I'm doing the link setup in vba code. This is the code I use to setup/refresh any links:
Private Sub relink_mysql_tables(mysql_connection As String)
Dim db As Database
Set db = CurrentDb()
Dim tbldef As TableDef
For Each tbldef In db.TableDefs
If (tbldef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
tbldef.Connect = mysql_connection
tbldef.RefreshLink
End If
Next
End Sub
The mysql_connection string I use is:
DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3
This is all fine and dandy and everything works until I close ms-access and reopen it. The user name and password seems to get forgotten. If I try and open a linked table, for example, I'm presented with a ODBC Connector pop-up asking me to enter user name and password.
I notice that after running the above relink code that if I hover the mouse pointer over a linked table (a table called 'tender' in this instance) it shows the current connection string, but with the user name and password omitted:
ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Option=3;TABLE=tender
I read elsewhere that adding ";Persist Security Info=True" to the connection string would work but it doesn't! The user name and password is still forgotten the next time I restart ms-access. My current solution is to re-run the relink code every-time the database is started but this seems like an unnecessary overhead to me. Is the a way for access to remember the user name and password within the linked tables?
I'm using Mysql5.5 and Access2007 by the way.
回答1:
I found an answer with the help of this microsoft article. Rather than using the RefreshLink method, delete and recreate the link with the dbAttachedODBC option:
Public Sub relink_mysql_tables(mysql_connection As String)
Dim db As Database
Dim tblDef As TableDef
Dim sLocalTableName As String
Dim sRemoteTableName As String
' new collection '
Dim newTableDefs As New Collection
' current database '
Set db = CurrentDb()
' create new table defs '
For Each tblDef In db.TableDefs
If (tblDef.Attributes And TableDefAttributeEnum.dbAttachedODBC) Then
sLocalTableName = tblDef.Name
sRemoteTableName = tblDef.SourceTableName
' create new linked table def '
Set tblDef = db.CreateTableDef(sLocalTableName, dbAttachSavePWD, sRemoteTableName, mysql_connection)
newTableDefs.Add tblDef
End If
Next
' delete old table defs '
For Each tblDef In newTableDefs
db.TableDefs.Delete tblDef.Name
Next
' add new table defs to current database '
For Each tblDef In newTableDefs
db.TableDefs.Append tblDef
Next
The connection string is the same as before but with the addition of the prefix "ODBC;":
ODBC;DRIVER={MySQL ODBC 5.1 Driver};Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypassword;Option=3
来源:https://stackoverflow.com/questions/5695194/dsn-less-connection-to-mysql-server-in-ms-access-not-remembering-user-name-and-p