开发者

Call Single StrConn from Multiple Pages

开发者 https://www.devze.com 2023-01-03 16:38 出处:网络
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 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开发者_StackOverflow the ""?


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" 
0

精彩评论

暂无评论...
验证码 换一张
取 消