Hi all,
I have this piece of vba-code which doesn't work for me.
When I try to execute this piece of code it returns a error: "Runtime error '1004' General ODBC error". The code works fine without variables in the SQL-string.
Could you please help me solving this issue, thanks.
I have this piece of vba-code which doesn't work for me.
Code:
Sub HentNavisionData()
Dim KtnNr As String, StartDato As Date, SlutDato As Date
KtnNr = "' " & Range("B5").Value & "'"
StartDato = Range("B6").Value
SlutDato = Range("B7").Value
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=xaldb;Description=xaldb;UID=xaldb;;;;DATABASE=xaldb", Destination:=Range("G19"))
.CommandText = Array("SELECT LEDTRANS.TXT, LEDTRANS.DATE_, LEDTRANS.AMOUNTMST" & Chr(13) & "" & Chr(10) & "FROM xaldb.dbo.LEDTRANS LEDTRANS" & Chr(13) & "" & Chr(10) & "WHERE (LEDTRANS.DATE_>={ts '" & StartDato & "'} And LEDTRANS.DATE_<={ts '" & SlutDato & "'}) AND (LEDTRANS.ACCOUNTNUMBER=" & KtnNr & ")" & Chr(13) & "" & Chr(10) & "ORDER BY LEDTRANS.DATE_ DESC")
.Name = "Navision data"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
When I try to execute this piece of code it returns a error: "Runtime error '1004' General ODBC error". The code works fine without variables in the SQL-string.
Could you please help me solving this issue, thanks.