ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Runtime error 1004 - how to solve (https://www.excelbanter.com/excel-programming/365520-runtime-error-1004-how-solve.html)

ladyhawke

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)



All times are GMT +1. The time now is 09:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com