Runtime error 1004 - how to solve

L

ladyhawke

I have a very basic excel macro:

Sub CreateQT()

On Error GoTo ErrorHandling

Dim sConn As String
Dim sSql As String
Dim oQt As QueryTable

sConn = "{SQL Native Client};"
sConn = sConn & "Server=.SERVER\SQLExpress;"
sConn = sConn & "AttachDbFilename=testExcel.mdf;"
sConn = sConn & "Database=dbname;Trusted_Connection=Yes;"


sSql = "SELECT dataField "
sSql = sSql & "FROM testTable"

Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn,
Destination:=Range("a1"), Sql:=sSql)

oQt.Refresh
GoTo No_Error
'End Sub

ErrorHandling:
MsgBox Err.Number & " " & Err.Description

No_Error:
End Sub


but this fails:
Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn,
Destination:=Range("a1"), Sql:=sSql)

with a 1004 Application-define or object-defined error...

How do I find/debug the couse and get the query to run? (sorry newbie
in this stuff - maybe very basic question)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top