travisbrown
Technical User
- Dec 31, 2001
- 1,016
I'm trying to create an Access db that has linked tables so I can send to client as a front end for a SQL 2005 db.
I'm running into a little problem.
1. I was trying to create an AutoExec module that would load when the db was started. I'm a little unfamiliar with Access. I thought I did it right, but I created a new module - it spawns MS Visual Basic, I stuck in this code and saved as AutoExec. What's the right way to create an autoexec module?
2. Since it doesn't run automatically, when I run the code manually in MSVB Editor, it balks and prompts me for password in a modal with the PW field populated. I copy and paste the same password I use as a variable value and then it executes the rest of the function calls and creates the linked tables in Access. If I msgbox the string - it appears to be using the correct PW even as it prompts me for one.
3. When I try to then open the tables in Access, I get "ODBC -- connection to 'SQLservermyservername.com' failed"
I'm running into a little problem.
1. I was trying to create an AutoExec module that would load when the db was started. I'm a little unfamiliar with Access. I thought I did it right, but I created a new module - it spawns MS Visual Basic, I stuck in this code and saved as AutoExec. What's the right way to create an autoexec module?
2. Since it doesn't run automatically, when I run the code manually in MSVB Editor, it balks and prompts me for password in a modal with the PW field populated. I copy and paste the same password I use as a variable value and then it executes the rest of the function calls and creates the linked tables in Access. If I msgbox the string - it appears to be using the correct PW even as it prompts me for one.
3. When I try to then open the tables in Access, I get "ODBC -- connection to 'SQLservermyservername.com' failed"
Code:
Option Compare Database
'//Name : AttachDSNLessTable
'//Purpose : Create a linked table to SQL Server without using a DSN
'//Parameters
'// stLocalTableName: Name of the table that you are creating in the current database
'// stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'// stServer: Name of the SQL Server that you are linking to
'// stDatabase: Name of the SQL Server database that you are linking to
'// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'// stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
On Error GoTo AttachDSNLessTable_Err
Dim td As TableDef
Dim stConnect As String
For Each td In CurrentDb.TableDefs
If td.Name = stLocalTableName Then
CurrentDb.TableDefs.Delete stLocalTableName
End If
Next
If Len(stUsername) = 0 Then
'//Use trusted authentication if stUsername is not supplied.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Else
'//WARNING: This will save the username and the password with the linked table information.
stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function
AttachDSNLessTable_Err:
AttachDSNLessTable = False
MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description
End Function
Sub LinkTables()
Dim login As String
Dim server As String
Dim password As String
Dim database As String
login = "correct login"
server = "correct server"
password = "correct password"
database = "correct database"
Call AttachDSNLessTable("TBL_PRODUCTS", "TBL_PRODUCTS", server, database, login, password)
Call AttachDSNLessTable("IDX_CATEGORY", "IDX_CATEGORY", server, database, login, password)
Call AttachDSNLessTable("LKUP_CATEGORY", "LKUP_CATEGORY", server, database, login, password)
Call AttachDSNLessTable("LKUP_STATUS", "LKUP_STATUS", server, database, login, password)
Call AttachDSNLessTable("TBL_COMPANY", "LKUP_STATUS", server, database, login, password)
Call AttachDSNLessTable("TBL_CUSTOMERS", "LKUP_STATUS", server, database, login, password)
MsgBox "Tables Loaded"
End Sub