原创:冰雪 gfuuyygy@163.com
说明:本示例适用于文件夹被移动或更名后,文件夹内的前端库窗体打开时,自动连接后端库的表1
本示例需引用ADO和DAO
模块:
Public Function CheckLinks() As Boolean
' 检查到后台数据库的链接;如果链接存在且正确的话,返回 True 。
Dim dbs As Database, rst As DAO.Recordset
Set dbs = CurrentDb()
' 打开链接表查看表链接信息是否正确。
On Error Resume Next
Set rst = dbs.OpenRecordset("tbl1")
rst.Close
' 如果没有错误,返回 True 。
If Err = 0 Then
CheckLinks = True
Else
CheckLinks = False
End If
End Function
应用:
Private Sub Form_Load()
Dim hdk As String
Dim cat As ADOX.Catalog
Dim tdf As ADOX.Table
If CheckLinks = False Then '如果连接错误
hdk = CurrentProject.Path & "\后端库名.mdb"
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
Set tdf = cat.Tables("表1")
tdf.Properties("jet oledb:link datasource") = hdk
End If
End Sub