views:

13

answers:

1

I have a ocnnection to SQL Server set up in my vba code.

The format is:

strConn = "ODBC;Driver=SQL;Server=SQL1;Database=DB1;Uid=1;Pwd=1"

I have this in 4 sheets, but there will be times when I will want to change it to call from SQL2 or SQL3, and instead of changing the code on each sheet 4 times, I want to change it only once.

Is there a way to set up that line to run by calling it from somewhere else or by passing in a string into the ""?

+1  A: 

Why don't you add a module or class to the project with a function that returns your connection string

Function GetConnection()
  GetConnection= "ODBC;Driver=SQL;Server=SQL1;Database=DB1;Uid=1;Pwd=1"  
End Function

Or a constant

Public Const strConn As String = "ODBC;Driver=SQL;Server=SQL1;Database=DB1;Uid=1;Pwd=1" 
Gratzy
For the function I'd just have to add "GetConnection" where I wanted to pass it in?
Daniel
Add a module to the project and put the function there call GetConnecton when you need to use the connection. The constant is probably better though. Declare it in the module and just use it like a variable, its just read only.
Gratzy