我为工作创建了一个跟踪数据库,该数据库具有前端接口和后端表。现在,我有一段代码,在启动时会将前端与后端表链接起来,以便人们可以将前端从“ G:\ Sections \ MEO \ DDPO \ Active Issues \ Tracking Database”复制并粘贴到他们的桌面,它将仍然能够访问后端文件。
我要避免的问题是,如果有人更改了后端位置的文件路径,则前端将无法找到后端。
示例:
原始路径G:\ Sections \ MEO \ DDPO \ Active Issues \ Tracking Database \ Database Backend \ tracking Database_be.accdb
如果有人修改了路径
G:\ New Sections \ MEO \ DDPO \ Active Issues \ Tracking Database \ Database Backend \ tracking Database_be.accdb
这是我用于前端的当前代码,以确保启动时链接到后端。
Private Function ReconnectTables() As Boolean
On Error Resume Next
Dim tdf As DAO.TableDef
Dim dbs As DAO.Database
Dim strPath As String
Dim strConnect As String
Set dbs = CurrentDb
strPath = dbs.Name
strPath = "G:\Sections\MEO\DDPO\Active Issues\Tracking Database\Database Backend\tracking Database_be.accdb"
strConnect = strPath
For Each tdf In dbs.TableDefs
If tdf.Connect <> "" Then
tdf.Connect = ";DATABASE=" & strConnect
tdf.RefreshLink
End If
Next
Set dbs = Nothing
If Err.Number = 0 Then ReconnectTables = True
End Function
感谢您可以提供的帮助
门罗
答案 0 :(得分:0)
我最终从代码中删除了硬编写的路径,并添加了一行代码来检查表中的值。存储在表中的值是用户可以以另一种形式选择的路径,如果它检测到表未链接,则将立即调用该路径。
我的新代码如下:
Private Function ReconnectTables() As Boolean
On Error Resume Next
Dim tdf As DAO.TableDef
Dim dbs As DAO.Database
Dim strPath As String
Dim strConnect As String
Set dbs = CurrentDb
strPath = dbs.Name
strPath = me.BackEndPath.Value
strConnect = strPath
For Each tdf In dbs.TableDefs
If tdf.Connect <> "" Then
tdf.Connect = ";DATABASE=" & strConnect
tdf.RefreshLink
End If
Next
Set dbs = Nothing
If Err.Number = 0 Then ReconnectTables = True
End Function
我以前只是有一个消息框,如果未链接表,则会弹出该消息框,但现在我添加了两行代码以在未链接表的情况下打开表单(frmBEpath)。
Private Sub Form_Load()
On Error Resume Next
'DoCmd.ShowToolbar "Ribbon", acToolbarNo
If ReconnectTables() = True Then
strVerClient = Nz(DLookup("[VersionNumber]", "[tblVersionClient]"), "")
strVerServer = Nz(DLookup("[VersionNumber]", "[tblVersionServer]"), "")
Me.Repaint
Else
'MsgBox "Couldn't Find Data Tables. Exiting.", vbCritical, "Error"
DoCmd.Close
DoCmd.OpenForm "frmBEpath"
End If
End Sub
此新表格告诉用户未链接表,并为他们提供了浏览后端文件的按钮。当他们选择后端文件并单击“是”按钮时,它将路径名存储在表中。
Option Compare Database
Public Function FolderSelection() As String
Dim objFD As Object
Dim strOut As String
strOut = vbNullString
Set objFD = Application.FileDialog(3)
If objFD.Show = -1 Then
strOut = objFD.SelectedItems(1)
End If
Set objFD = Nothing
FolderSelection = strOut
End Function
Private Sub btnBrowse_Click()
Dim strChoice As String
strChoice = FolderSelection
If Len(strChoice) > 0 Then
Me.txtPath = strChoice
End If
End Sub
Private Sub btnConfirmYes_Click()
Me.BackEndPath.Value = Me.txtPath.Value
DoCmd.Close
DoCmd.OpenForm "frmsplash"
End Sub