Anarach
Anarach

Reputation: 458

Connecting to a Database using VBA

I know its a basic question but no one has asked it so i am asking probably everyone knows it.

Dim cnn As ADODB.Connection.
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
With cnn
    .ConnectionString = "Driver=SQL Server;Server=something;Uid=something;PWD=something!;Database=CS"
    .ConnectionTimeout = 60
    .Open
End With

Ok so here i have a doubt where do we define the actual existence of the database?

i mean how will the code know where is the DB? we need to give some path reference right?

The above code opens the connection and keeps it ready but where is the DB how does the code know?

Upvotes: 1

Views: 3177

Answers (1)

Ali Sheikhpour
Ali Sheikhpour

Reputation: 11045

This connection string is for connecting to SQL server. SQL server database is not a file with certain file path. Knowing the server name and database name (in this example Database=CS) is enough. If you want to connect to Microsoft excel database, the standard connection string would be something like this (search for your proper version):

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;
Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";

Upvotes: 2

Related Questions