在论坛中学习到一个OBDC自动连接的一段代码,如下:
Function LinkToPubsAuthorsDSNLess()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strConnect As String
Dim strServer As String
Dim strDatabase As String
Dim strUID As String
Dim strPWD As String
strServer = "IP地址"
strDatabase = "数据库名称"
strUID = "用户名"
strPWD = "数据库密码"
strConnect = "ODBC;DRIVER={SQL Server}" _
& ";SERVER=" & strServer _
& ";DATABASE=" & strDatabase _
& ";UID=" & strUID _
& ";WD=" & strPWD & ";"
Set db = CurrentDb()
Set tdf = db.CreateTableDef("要创建的链接表名称")
tdf.SourceTableName = "目标表名称"
tdf.Connect = strConnect
db.TableDefs.Append tdf
db.TableDefs.Refresh
Set tdf = Nothing
Set db = Nothing
End Function