Question : ODBC tables connecting on startup

Hi Experts,

As the title suggests i need my ODBC tables to all connect when the database loads. I posted a similar question a while back regarding a pass thru query and it turns out you can go into the query properties and store the user name and password in there.

How is this possible with ODBC tables?

Many Thanks

Answer : ODBC tables connecting on startup

The snippet below is for a system I use where some tables are linked to SQL Server 2005

You need a table that lists each table that is to be linked with the local name being what you call the table in Access and the remote name being the SQL Server name e.g. Users in Access and dbo.Users in SQL.

I alsp have a table where I store the Server name and Database name (SQL Server and Database).

This example uses Windows authentication. You'd need to add in User names and passwords (see the link in the earlier post) if using SQL Authentication.

The beauty of dsn - less is that you don'y have to worry about a dsn (file or otherwise), the drawback, you cannot create a link manually, so you need a button to run this code.

Kelvin
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
Public Function RelinkSQLTables()
On Error GoTo EH
 
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strServer As String
Dim strDatabase As String
Dim strConnect As String
Dim strTblServer As String
Dim strTblLocal As String
Dim rsTables As DAO.Recordset
Dim sSQL As String
 
Set db = CurrentDb()
 
sSQL = "SELECT * FROM DatabaseTables where Not IsNull(RemoteName);"
 
Set rsTables = db.OpenRecordset(sSQL, dbOpenSnapshot)
Do Until rsTables.EOF
    For Each tdf In db.TableDefs
        If tdf.Name = rsTables!TableName Then
            ''Build the dsn-less connection string
            db.TableDefs.Delete rsTables!TableName
            Exit For
        End If
    Next
        strServer = ""
        strDatabase = ""
        strConnect = ""
        strServer = DLookup("[ServerName]", "qryDataLinks", "[TableName] = '" & rsTables!TableName & "'")
        strDatabase = DLookup("[DatabaseName]", "qryDataLinks", "[TableName] = '" & rsTables!TableName & "'")
        strConnect = "ODBC;Driver={SQL Native Client};Server=" & strServer & ";Database=" & strDatabase & ";Trusted_Connection=yes"
        Set tdf = db.CreateTableDef(rsTables!TableName, dbAttachSavePWD, rsTables!RemoteName, strConnect)
        db.TableDefs.Append tdf
    rsTables.MoveNext
Loop
 
MsgBox "All SQL tables relinked", vbOKOnly + vbInformation, "Process Complete"
 
Exit_Sub:
    Exit Function
    
EH:
    Call ErrorManager(Err.Number, Err.Description)
    
    Resume Exit_Sub
 
End Function
Random Solutions  
 
programming4us programming4us