Link tables on strat up!

  • Thread starter Thread starter Ladi
  • Start date Start date
L

Ladi

Dear friends,

Becouse the system is based on different files on the same folder, I'm
wondering to know wether is the possibility to link tables on start up so
the folder can be moved or renamed and the Access application can always
find the access data file. Please note that I use binding on forms to tables
(no variables).

Thanks
Ladi
 
Hi Ladi

Yes, you can do this with code that runs when your database opens. The
following function will link (or relink) a table:

Public Function LinkTable(LocalTable As String, _
SourceDatabase As String, _
Optional ByVal SourceTableName As String, _
Optional ForceRefresh As Boolean = False) As Long
Dim db As DAO.Database, tbl As DAO.TableDef
Dim sConnect As String, ErrSts As Long
On Error GoTo ProcErr
If Len(SourceTableName) = 0 Then SourceTableName = LocalTable
Set db = DBEngine(0)(0)
If SourceDatabase = db.Name Then
Err.Raise vbObjectError + 1, , "Linked table must be in a different
database"
End If
sConnect = ";DATABASE=" & SourceDatabase
On Error Resume Next
Set tbl = db(LocalTable)
On Error GoTo ProcErr
If Not tbl Is Nothing Then
With tbl
If (.Attributes And (dbAttachedTable Or dbAttachedODBC)) = 0 Then
Err.Raise vbObjectError + 2, , "Local table exists with this name"
End If
If ForceRefresh _
Or (.SourceTableName <> SourceTableName) _
Or (.Connect <> sConnect) Then
db.TableDefs.Delete LocalTable
Else
GoTo ProcEnd
End If
End With
End If
Set tbl = db.CreateTableDef(LocalTable)
tbl.SourceTableName = SourceTableName
tbl.Connect = sConnect
db.TableDefs.Append tbl
ProcEnd:
LinkTable = ErrSts
Exit Function
ProcErr:
With Err
ErrSts = .Number
MsgBox "Error linking table '" & SourceTableName & vbCrLf & .Description
End With
Resume ProcEnd
End Function

It is more robust than some other methods you could use, like
TransferDatabase.
(Note that you need a reference to DAO. or the db and tdf variables need to
be declared "As Object")

The path where your mdb resides is returned by CurrentProject.Path, so you
could use it like this:
Call LinkTable( "MyTable"; CurrentProject.Path & "\MyDb.mdb" )

That will always look for the back end data file in the same folder as the
front end.

Or, if you want to move it independently, you could store the path to the
back end either as a registry setting, of in an INI file.
 
A more flexible way to find the back end would be to use a common dialog box
to allow the user to browse for it. I do this in my applications:
I have an invisible common dialog box contrl on the form.
In the Open event of the start up form:
Check to see if the links are still good.
If they are not, show the dialog box
If they did not cancel, link to the back end the dialog box returned.
Of course, you have to do error checking to make sure the back end has the
tables you want.

Graham Mandeno said:
Hi Ladi

Yes, you can do this with code that runs when your database opens. The
following function will link (or relink) a table:

Public Function LinkTable(LocalTable As String, _
SourceDatabase As String, _
Optional ByVal SourceTableName As String, _
Optional ForceRefresh As Boolean = False) As Long
Dim db As DAO.Database, tbl As DAO.TableDef
Dim sConnect As String, ErrSts As Long
On Error GoTo ProcErr
If Len(SourceTableName) = 0 Then SourceTableName = LocalTable
Set db = DBEngine(0)(0)
If SourceDatabase = db.Name Then
Err.Raise vbObjectError + 1, , "Linked table must be in a different
database"
End If
sConnect = ";DATABASE=" & SourceDatabase
On Error Resume Next
Set tbl = db(LocalTable)
On Error GoTo ProcErr
If Not tbl Is Nothing Then
With tbl
If (.Attributes And (dbAttachedTable Or dbAttachedODBC)) = 0 Then
Err.Raise vbObjectError + 2, , "Local table exists with this name"
End If
If ForceRefresh _
Or (.SourceTableName <> SourceTableName) _
Or (.Connect <> sConnect) Then
db.TableDefs.Delete LocalTable
Else
GoTo ProcEnd
End If
End With
End If
Set tbl = db.CreateTableDef(LocalTable)
tbl.SourceTableName = SourceTableName
tbl.Connect = sConnect
db.TableDefs.Append tbl
ProcEnd:
LinkTable = ErrSts
Exit Function
ProcErr:
With Err
ErrSts = .Number
MsgBox "Error linking table '" & SourceTableName & vbCrLf & .Description
End With
Resume ProcEnd
End Function

It is more robust than some other methods you could use, like
TransferDatabase.
(Note that you need a reference to DAO. or the db and tdf variables need to
be declared "As Object")

The path where your mdb resides is returned by CurrentProject.Path, so you
could use it like this:
Call LinkTable( "MyTable"; CurrentProject.Path & "\MyDb.mdb" )

That will always look for the back end data file in the same folder as the
front end.

Or, if you want to move it independently, you could store the path to the
back end either as a registry setting, of in an INI file.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Ladi said:
Dear friends,

Becouse the system is based on different files on the same folder, I'm
wondering to know wether is the possibility to link tables on start up so
the folder can be moved or renamed and the Access application can always
find the access data file. Please note that I use binding on forms to
tables (no variables).

Thanks
Ladi
 
I sort of disagree, at least the part about using the common
dialog control. That thing causes no end of trouble, not
least is the need to have a license to distribute it and the
difficulty of installing it.

A far better way is to use an API call to the Window's file
browse routine. See:
http://www.mvps.org/access/api/index.html

Depending on the situation, entire relinking procedures are
at:
http://www.mvps.org/access/tables/tbl0009.htm
http://www.bway.net/~dfassoc/download/Access/Reconnect.html
http://www.mvps.org/access/tables/tbl0012.htm
http://www.mvps.org/access/tables/tbl0010.htm
 
Back
Top