Tim F Tim F - 6 months ago 59
SQL Question

Excel VBA to parse SQL Connection Strings

I have an Excel VBA that connects to a SQL DB using the connection strings, but I would like to know if it's possible to parse into the connection string from a cell? For instance, if I have

SQLSVR1\Instance1
in cell
B2
and
Databas1
in
B3
can i get that to be put into the connection strings like the following:

objMyConn.ConnectionString = "Provider=sqloledb; Data Source=**"B2"**; Initial Catalog=**"B3"**; Integrated Security=SSPI;"


Is that possible?

Thanks in advance.

Answer

Totally possible:

objMyConn.ConnectionString =  "Provider=sqloledb; Data Source=" & range("B2").value & "; Initial Catalog=" & range("B3").value &"; Integrated Security=SSPI;"

This basically concatenates your string with the values in those cells.

Range.Value documents.

Comments