|
Public Function gf_LinkSqlServer() As Boolean
On Error GoTo Err_LinkSqlServer
Dim strConn As String, dbCurr As DAO.Database
Dim tdf As Object
Dim dbs As Object
strConn = "ODBC;DRIVER=SQL Server;SERVER=IP,端口;DATABASE=sql数据库名;UID=saWD=密码"
Set dbCurr = DBEngine.Workspaces(0).OpenDatabase("OfficeCn", False, False, strConn)
Set dbs = CurrentDb
For Each tdf In dbs.TableDefs
If tdf.Connect <> "" Then
tdf.Connect = strConn
tdf.RefreshLink
End If
Next
dbCurr.Close
Set dbCurr = Nothing
MsgBox "连接成功", vbInformation, "连接SQL Server"
gf_LinkSqlServer = True
Exit Function
Err_LinkSqlServer:
Err.Clear
MsgBox "连接出错!", vbCritical, "连接SQL Server"
gf_LinkSqlServer = False
DoCmd.Quit
End Function
|
|