Article ID: 159691
Article Last Modified on 1/19/2007
Table: tblODBCDataSources
-------------------------
Field Name: DataBase
Data Type: Text
Field Size: 50
Field Name: UID
Data Type: Text
Field Size: 50
Field Name: PWD
Data Type: Text
Field Size: 50
Field Name: Server
Data Type: Text
Field Size: 50
Field Name: ODBCTableName
Data Type: Text
Field Size: 50
Field Name: LocalTableName
Data Type: Text
Field Size: 50
Field Name: DSN
Data Type: Text
Field Size: 50
Table Properties: tblODBCDataSources
------------------------------------
PrimaryKey: LocalTableName
Field Name Value
-------------------------------------
DataBase Pubs
UID <username>
PWD <strong password>
Server SQLPUBS
ODBCTableName dbo.authors
LocalTableName Authors
DSN Pubs
Note that the ODBCTableName represents the name of the table in
the source database (in this example, SQL Server), and that the LocalTableName
represents the name assigned to the linked table in the Access database.
'***************************************************************
'The DoesTblExist function validates the existence of a TableDef
'object in the current database. The result determines if an
'object should be appended or its Connect property refreshed.
'***************************************************************
Function DoesTblExist(strTblName As String) As Boolean
On Error Resume Next
Dim db As Database, tbl As TableDef
Set db = CurrentDb
Set tbl = db.TableDefs(strTblName)
If Err.Number = 3265 Then ' Item not found.
DoesTblExist = False
Exit Function
End If
DoesTblExist = True
End Function
Function CreateODBCLinkedTables() As Boolean
On Error GoTo CreateODBCLinkedTables_Err
Dim strTblName As String, strConn As String
Dim db As Database, rs As Recordset, tbl As TableDef
' ---------------------------------------------
' Register ODBC database(s)
' ---------------------------------------------
Set db = CurrentDb
Set rs = db.OpenRecordset("tblODBCDataSources")
With rs
While Not .EOF
DBEngine.RegisterDatabase rs("DSN"), _
"SQL Server", _
True, _
"Description=VSS - " & rs("DataBase") & _
Chr(13) & "Server=" & rs("Server") & _
Chr(13) & "Database=" & rs("DataBase")
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = rs("LocalTableName")
strConn = "ODBC;"
strConn = strConn & "DSN=" & rs("DSN") & ";"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=" & rs("DataBase") & ";"
strConn = strConn & "UID=" & rs("UID") & ";"
strConn = strConn & "PWD=" & rs("PWD") & ";"
strConn = strConn & "TABLE=" & rs("ODBCTableName")
If (DoesTblExist(strTblName) = False) Then
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _
strConn)
db.TableDefs.Append tbl
Else
Set tbl = db.TableDefs(strTblName)
tbl.Connect = strConn
tbl.RefreshLink
End If
rs.MoveNext
Wend
End With
CreateODBCLinkedTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinkedTables_End:
Exit Function
CreateODBCLinkedTables_Err:
MsgBox Err.Description, vbCritical, "MyApp"
Resume CreateODBCLinkedTables_End
End Function
?CreateODBCLinkedTables()Note that you receive the message "Refreshed ODBC Data Sources." Also note that a new linked table called Authors exists in your database, and a new User DSN called Pubs exists in the ODBC Administrator in Control Panel.
112131 ACC: Creating Virtual Indexes with SQL Data-Definition Queries
Additional query words: inf
Keywords: kbhowto kbusage KB159691