|
4#
楼主 |
发表于 2012-7-30 09:51:25
|
只看该作者
如果有刷新链接可以,就不会有这个问题了,无论是有代码刷新还是用ACCESS 自带的链接表管理器,都无法刷新,提示ODBC 调用失败,另尝试很多种链接表的代码,都是一提示,把代码摘录如下:
1 创建链接表的
Private Sub Command1_Click()
DoCmd.TransferDatabase acLink, "ODBC", _
"ODBC;DSN=ERP2012;UID=saWD=123456;LANGUAGE=us_english;" _
& "DATABASE=ERP2012", acTable, "CP_CPZD", "CP_CPZD", True
End Sub
2 ODBC刷新链表的
Public Function Relink() As Boolean
Dim strCn As String
Dim strSrv As String
Dim strUser As String
Dim strPWD As String
Dim strDataBase As String
Dim Rst_Cprk As Recordset
' '判断通过ADO能否连到服务器
If ConnectWB = False Then Exit Function
strSrv = Nz(DLookup("[svrName]", "path", "[Bz]='1'"), "")
strUser = Nz(DLookup("[userName]", "path", "[Bz]='1'"), "")
strPWD = Nz(DLookup("[pwd]", "path", "[Bz]='1'"), "")
strDataBase = Nz(DLookup("[dataName]", "path", "[Bz]='1'"), "")
Dim strFileName As String
Dim tdf As TableDef
Dim strConnect As String
strConnect = "ODBC;Driver=SQL Server;Server=" & strSrv _
& ";UID=" & strUser & "WD=" & strPWD & ";DATABASE=" & strDataBase
' 循环处理此数据库的所有表。
For Each tdf In CurrentDb.TableDefs
' 如果表有一个连接串,那么该表是一个链接表。
If Len(tdf.Connect) > 0 Then
tdf.Connect = strConnect
Err = 0
On Error Resume Next
tdf.RefreshLink ' 重新链接该表。
If Err <> 0 Then
Relink = False
Exit Function
End If
End If
Next tdf
Relink = True
End Function
3 ADO刷新链表的
Private Sub Command2_Click()
' strTargetDB: 被链接的数据库路径名
' strProviderString: 连接字符串
' strSourceTbl: 被链接的源表名称
' strLinkTblName: 要重设链接的链接表的名称
' 作者:朱亦文
Dim strTargetDB() As String
Dim strProviderString() As String
Dim strSourceTbl() As String
Dim strLinkTblName() As String
Dim catDB As ADOX.Catalog
Dim tblLink As ADOX.Table
Dim tmpLink As ADOX.Table
Dim i As Integer
Dim j As Integer
Set catDB = New ADOX.Catalog
catDB.ActiveConnection = CurrentProject.Connection
i = catDB.Tables.Count
ReDim strTargetDB(i)
ReDim strProviderString(i)
ReDim strSourceTbl(i)
ReDim strLinkTblName(i)
i = 1
For Each tmpLink In catDB.Tables
If tmpLink.Properties("Jet OLEDB:Create Link") Then
If Trim(tmpLink.Properties("Jet OLEDB:Remote Table Name")) <> "" Then
Debug.Print tmpLink.Name & " | " & tmpLink.Properties("Jet OLEDB:Remote Table Name") & " | " & tmpLink.Properties("Jet OLEDBink Datasource")
strLinkTblName(i) = tmpLink.Name
strTargetDB(i) = tmpLink.Properties("Jet OLEDBink Datasource")
strProviderString(i) = tmpLink.Properties("Jet OLEDBink Provider String")
strSourceTbl(i) = tmpLink.Properties("Jet OLEDB:Remote Table Name")
Do While InStr(1, strTargetDB(i), "\") <> 0
strTargetDB(i) = Mid(strTargetDB(i), InStr(1, strTargetDB(i), "\") + 1, Len(strTargetDB(i)))
Loop
strTargetDB(i) = CurrentProject.Path & "\" & strTargetDB(i)
i = i + 1
End If
End If
Next
j = i - 1
For i = 1 To j
catDB.Tables.Delete strLinkTblName(i)
Set tblLink = New ADOX.Table
With tblLink
.Name = strLinkTblName(i)
Set .ParentCatalog = catDB
.Properties("Jet OLEDB:Create Link") = True
.Properties("Jet OLEDB:Link Datasource") = strTargetDB(i)
.Properties("Jet OLEDB:Link Provider String") = strProviderString(i)
.Properties("Jet OLEDB:Remote Table Name") = strSourceTbl(i)
End With
catDB.Tables.Append tblLink
Set tblLink = Nothing
Next
Set catDB = Nothing
End Sub
以上方法都试,开发环境是 ACCESS 2010 + SQL SERVER 2008 R2 |
|