How to run code

LaurieW

Registered User.
Local time
Today, 06:00
Joined
May 9, 2002
Messages
99
I am working on my first bound application between SQL Server and Access 2000. I am using a manual to try to set this up. The manual has given the following code to link one table/relink all tables. I assume I should run the relink all tables code every time a user opens the database ... but how do I get it to run? I've tried several different variations (macro using RunCode; on a form in the Open event) and always come up with errors. What am I missing here? It must be something simple ... thanks, Laurie

Here's the code for the first module called "LinkSingleTable":
Public Function LinkTableDAO( _
strLinkName As String, _
strDBName As String, _
strTableName As String, _
strDSNname As String) As Boolean

'Links or re-links a single table.
'Returns True or False based on Err value.

Dim db As DAO.Database
Dim tdf As DAO.TableDef

On Error Resume Next
Set db = CurrentDb
' if the link already exists, delete it
Set tdf = db.TableDefs(strLinkName)
If Err.Number = 0 Then
db.TableDefs.Delete strLinkName
db.TableDefs.Refresh
Else
'Ignore error and reset
Err.Number = 0
End If

'Create a new TableDef object
Set tdf = db.CreateTableDef(strLinkName)

'Set the Connect and SourceTableName properties to establish the link
tdf.Connect = "ODBC;Database=" & strDBName & ";DSN = " & strDSNname _
& ";Integrated Security=True"
tdf.SourceTableName = strTableName

'Append to the database's TableDefs collection
db.TableDefs.Append tdf

LinkTableDAO = (Err = 0)
End Function
* * * * *
Here is the code for the second module called "LinkAllTables":
Function RelinkAllTables( _
strSQLDB As String, _
strDSN As String) As Boolean

'Relinks existing ODBc linked tables

Dim tdf As TableDef
Dim fLink As Boolean

On Error GoTo HandleErr
RelinkAllTables = False
For Each tdf In CurrentDb.TableDefs
With tdf
'Only process linked ODBC tables
If .Attributes = dbAttachedODBC Then
fLink = LinkTableDAO( _
strLinkName:=.Name, _
strDBName:=strSQLDB, _
strTableName:=.SourceTableName, _
strDSNname:=strDSN)
'If there's a problem with one table, don't bother processing the rest.
If Not fLink Then GoTo ExitHere
End If
End With
Next tdf
RelinkAllTables = fLink

ExitHere:
Exit Function

HandleErr:
RelinkAllTables = False
MsgBox _
Prompt:=Err & ": " & Err.Description, Title:="Error in RelinkAllTables"
Resume ExitHere

End Function
 

Users who are viewing this thread

Back
Top Bottom