Although there are a lot of postings on the ACCXP Web site, there are still a lot of friends asking questions about it. Should letter Netizen's proposal, combine Alex Master Moderator's Relocation link table File source code, this aspect of the specific operation is described as follows:
Assume the foreground database file name is Frontbase.mdb
Background database file name is Backdata.mdb
Frontbase link Table tbl1, tbl2, tbl3, ..., Link to Backdata.mdb
First we want to determine if the link is correct in the Startup form Load event of the foreground database file
The method is to open any linked table, assuming the TBL1, the following code:
Public Function Checklinks () as Boolean
' Check the link to the background database, and return True if the link exists and is correct.
Dim dbs as Database, rst as DAO. Recordset
Set dbs = CurrentDb ()
' Open the linked table to see if the sheet link information is correct.
On Error Resume Next
Set rst = dbs. OpenRecordset ("Tbl1")
Rst. Close
' Returns True if there are no errors.
If ERR = 0 Then
Checklinks = True
Else
Checklinks = False
End If
End Function
Load event for startup form:
Private Sub Form_Load ()
If checklinks = False Then
DoCmd.OpenForm "Frmconnect"
End If
End Sub
Frmconnect connection form as shown below
The next thing is how to refresh the linked table.
The button on the right of the above form is used to invoke the API to open the file dialog box, as follows:
Declare Function getopenfilename Lib "Comdlg32.dll" Alias "Getopenfilenamea" (Popenfilename as OpenFileName) as Boolean
Type OpenFileName
lStructSize as Long
hWndOwner as Long
HInstance as Long
Lpstrfilter as String
Lpstrcustomfilter as String
Nmaxcustfilter as Long
Nfilterindex as Long
Lpstrfile as String
nMaxFile as Long
Lpstrfiletitle as String
Nmaxfiletitle as Long
lpstrInitialDir as String
Lpstrtitle as String
Flags as Long
Nfileoffset as Integer
Nfileextension as Integer
Lpstrdefext as String
Lcustdata as Long
Lpfnhook as Long
Lptemplatename as String
End Type
Private Sub Fileopen_click ()
Dim Ofn as OpenFileName
Dim Rtn as String
Ofn.lstructsize = Len (ofn)
Ofn.hwndowner = Me.hwnd
Ofn.lpstrfilter = "Database file (*.mdb)" & vbNullChar & "*.mdb"
Ofn.lpstrfile = Space (254)
Ofn.nmaxfile = 255
Ofn.lpstrfiletitle = Space (254)
Ofn.nmaxfiletitle = 255
Ofn.lpstrinitialdir = Currentproject.path
Ofn.lpstrtitle = "Background data file is"
Ofn.flags = 6148
RTN = GetOpenFileName (ofn)
Filename.setfocus
If RTN = True Then
Filename.text = Ofn.lpstrfile
Filename.text = Filename.text
Ok. Enabled = True
Else
Filename.text = ""
End If
End Sub
The connection button refreshes the linked table with the following code:
Private Sub OK_Click ()
Dim Tabdef as TableDef
For each tabdef in Currentdb.tabledefs
If Len (Tabdef.connect) > 0 Then
Tabdef.connect = ";D atabase=" & Me.FileName.Text & "; pwd= "+ Background database password
Tabdef.refreshlink
End If
Next
MsgBox "Connection Successful! "
DoCmd.Close acform, Me.Name
End Sub
In fact, there are only two steps to determine if the link is correct and refresh the linked table.