Link To Access

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

'Location of DB

Public Const txtDBPath As String =

"C:\mydatabase\DB.accdb"

'Password of Backend Database


Public Const txtDBPassword As String =

"a"

Sub relinktables()
On Error GoTo relinktables_Err
'Routine to relink the tables automatically. Change the constant
LnkDataBase to the desired one and run the sub
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strTable As String
Set dbs = CurrentDb()
For Each tdf In dbs.TableDefs
If Len(tdf.Connect) > 1 Then 'Only relink linked tables
If tdf.Connect <> ";DATABASE=" & txtDBPath Then 'only relink
tables if the are not linked right
strTable = tdf.Name
dbs.TableDefs(strTable).Connect =
txtDBPassword &

"MS Access;PWD=" &

";DATABASE=" & txtDBPath

dbs.TableDefs(strTable).RefreshLink
End If
End If
Next tdf

'For Error handling


relinktables_Exit:
Exit Sub
relinktables_Err:

MsgBox Error$
Resume relinktables_Exit
End Sub

------------

Const LnkDataBase = "C:\MyDB_be.accdb"


Const DBPassword = "123"
Sub relinktables()
'Routine to relink the tables automatically. Change the constant
LnkDataBase to the desired one and run the sub
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strTable As String
Set dbs = CurrentDb()
For Each tdf In dbs.TableDefs
If Len(tdf.Connect) > 1 Then 'Only relink linked tables
If tdf.Connect <> ";DATABASE=" & LnkDataBase Then 'only relink
tables if the are not linked right
If Left(tdf.Connect, 4) <> "ODBC" Then 'Don't want to relink
any ODBC tables
strTable = tdf.Name
dbs.TableDefs(strTable).Connect = "MS Access;PWD=" &
DBPassword & ";DATABASE=" & LnkDataBase
dbs.TableDefs(strTable).RefreshLink
End If
End If
End If
Next tdf
End Sub

You might also like