在链接表中设置用户和pws

时间:2015-07-10 12:25:40

标签: vba ms-access access-vba ms-access-2007

使用下面的代码我设置链接表参数,我设置UsrPws,但每次程序启动时我使用链接表我收到此错误:

enter image description here

然后

enter image description here

现在我必须取消标记可信连接并再次输入UsrPws

造成这种情况的原因是什么?

Public Function SetLinkedTablesTruck()

    Dim db As DAO.Database
    Dim tdf As TableDef
    On Error GoTo ErrorHandler    
    Set db = CurrentDb
    ' Loop Table Defs
    For Each tdf In db.TableDefs
    If tdf.Name = "TruckOUT" Then
    MsgBox tdf.Connect
    tdf.Connect = "ODBC;DRIVER=SQL Server;SERVER=MYITSRV;DATABASE=dbItalianDb;TABLE=dbo.truckView;UID=UserView;PWD=UserView"
        tdf.RefreshLink
        db.TableDefs.Refresh
    End If

    Next

    Set tdf = Nothing
    Set db = Nothing

    MsgBox "Tables Re-Linked"

ExitHandler:
   Exit Function

ErrorHandler:
   MsgBox "Error in SetLinkedTables : " & err.Description
   Resume ExitHandler

End Function

1 个答案:

答案 0 :(得分:-1)

您通常不包括表名,因此:

tdf.Connect = "ODBC;DRIVER=SQL Server;SERVER=MYITSRV;DATABASE=dbItalianDb;UID=UserView;PWD=UserView"