Question : QueryTable.Connection error

Excel 2003 VBA

I have the following string
gs_Conn = "Provider=SQLOLEDB;Persist Security Info=False;Initial Catalog=DBNamex;User ID=Userx;Pwd=passwordx;Data Source=Serverx"

which works successfully as an ADO Connection but returns an error when used as a QueryTable Connection.

All variables have been set appropriately and the code works using a DSN connection gs_Conn="ODBC;DSN=DSNx;UID=Userx;PWD=passwordx;WSID=Serverx;Database=DBNamex;"

Any help appreciated
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
...
gs_Conn = "Provider=SQLOLEDB;Persist Security Info=False;Initial Catalog=DBNamex;User ID=Userx;Pwd=passwordx;Data Source=Serverx"
 
'This works
    Set conn = New ADODB.Connection
    conn.Open gs_Conn
    ...
 
'This returns a Error 1004 Application-defined or object-defined error
    ...
    Set qryTable = wksQuery.QueryTables.Add( _
            Connection:=gs_Conn, _
            Destination:=wksQuery.Cells(1, 1), _
            Sql:=strSQL)

Answer : QueryTable.Connection error

Try using:

Set qryTable = wksQuery.QueryTables.Add( _
            Connection:="OLEDB;" & gs_Conn, _
            Destination:=wksQuery.Cells(1, 1), _
            Sql:=strSQL)

Regards,
Rory
Random Solutions  
 
programming4us programming4us