MS Access VBA change connection string of SQL Pass Through Table

deer777
deer777 used Ask the Experts™
on
Create VBA code that will changes the connection string of the SQL Pass Through table (DB_A) to the sandbox table that gets created by DB_B
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
What version of Access are you using?  Are you using Office 365?

I'm not familiar with O365, but a normal connection string for a pass-through query would look something like:

ODBC;DRIVER=DriverName;SERVER=ServerName;Trusted_Connection=Yes;APP=SSMA;DATABASE=DatabaseName

Where Drivername might be on of:
SQL Server Native Client 12.0
SQL Server Native Client 11.0
SQL Server Native Client 10.0
SQL Server

ServerName could either be the name of the Server (in your case, they may be the same) or an IP address

Database name would refer to the name of the database that the query applies to.  In my case, I use:
AppName_Prod
AppName_Dev

the VBA to change the connection string would look like:
strConn = "ODBC;DRIVER=DriverName;SERVER=ServerName;Trusted_Connection=Yes;APP=SSMA;DATABASE=DatabaseName"
currentdb.Querydefs("PassThruQueryName").Connect = strConn

Open in new window

Author

Commented:
I am using Access 2010
Distinguished Expert 2017

Commented:
The version of Access doesn't affect the connection string.  It is the version of the driver and the server that you need to be concerned with.  Here is a relinker I made that lets you swap between various production and staging libraries.  I used part of the server instance name to define what needed to be swapped.  At the end, all the pass-through queries are relinked.
RelinkODBC.accdb

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial