ODBC Connect Str

Posted on 2002-03-07
Last Modified: 2012-06-27
I am running an Access to SQL server application.
When setting up the Connect String for pass through queries I am pointing it to the live DB called AdaptData   ie;ODBC;DRIVER=SQL Server;SERVER=DIMENSIONS;UID=scott;PWD=password;DATABASE=AdaptData

I also have a test DB set up called BackUp

Due to me having to change the connect string between Databases so I can test code I am currently having to change the Connect String on Each Pass Through, and there are a few!!

Is there a way I can place a variable in this connect string and change it through code..




Question by:scottsanpedro
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

TheMek earned 100 total points
ID: 6846939
Hi Scott,

I'd try something like this (can't test it here, since I don't have a SQL server):

Public Sub Test()
Dim q As QueryDef
Dim varDBname As String

varDBname = "whateveryouwant"
For Each q In CurrentDb.QueryDefs
  If q.Type = dbQSQLPassThrough Then
    q.Connect = "ODBC;DRIVER=SQL Server;SERVER=DIMENSIONS;UID=scott;PWD=password;DATABASE=" & varDBname
  End If
End Sub

Hope this helps you,
LVL 54

Expert Comment

ID: 6846942
Just make a function using:

function fncSetConnectTest
dim qd as querydef

set qd = currentdb.querydefs("your queryname")
qd.connect = "the connection string you need for test"

end function


function fncSetConnectProd
dim qd as querydef

set qd = currentdb.querydefs("your queryname")
qd.connect = "the connection string you need for prod"

end function

After running this the connection will be saved.


Author Comment

ID: 6846969
Excellant solution....

Works wonderful

Thanks for you time


Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question