View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ladyhawke ladyhawke is offline
external usenet poster
 
Posts: 23
Default Runtime error 1004 - how to solve

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)