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("职务表") '只须查找其中一个表即可
rst.Close
' 如果没有错误,返回 True 。
If Err = 0 Then
CheckLinks = True
Else
CheckLinks = False
End If
End Function
以下是连接窗体的代码
If CheckLinks = False Then '如果后台数据库链接错误,则重新链接后台数据库
Dim tabDef As TableDef
Dim FileName
Dim box As String
FileName = Application.CurrentProject.path & "\home.m__"
If Dir(FileName) = "" Then
box = MsgBox("没找到数据端,请确定本系统已正确安装到你的电脑中", vbAbortRetryIgnore + vbQuestion, "^-^ 提醒!")
If box = vbIgnore Then
i = i + 4
Me.Label14.Caption = "链接数据端失败"
Me.Label20.Visible = True
Me.Label20.Caption = "×"
Me.Label20.ForeColor = 255
ElseIf box = vbAbort Then
DoCmd.Close
Else
i = i - 79
End If
Else
For Each tabDef In CurrentDb.TableDefs
If Len(tabDef.Connect) > 0 Then
Sub LinkTable(strMDBFileName As String, strUserName As String, strPassword As String, strSourceTableName As String)
'
' strMDBFileName ...... 后台数据库名(MBD)
' strUserName ......... 打开数据库的用户名
' strPassword ......... 打开数据库的密码
' strSourceTableName ..要建立连接的数据表名(在后台数据库中)
'
' 作者:鱼儿游游
' 时间:2011.05.25
'
Dim strConnect As String '连接字串
Dim dbs As Object 'Database
Dim tdf As Object 'DAO.TableDef
Dim strLocalTableName As String '连接表的名称