Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default SQL Syntax Error in Query

I have a query which I wnat to run between two dates (yyyy-mm-dd hh.mm.ss
format) which are in two specified cells on a workbook, tried the routine
below but when macro gets to " .Refresh BackgroundQuery:=False" line at
bottom I get Run Time error 1004 - SQL Syntax Error (Full code below).

Please help.

Sub GetData()
'
' GetData Macro
' Macro recorded 30/11/2007 by
'

Worksheets("Push Data").Activate

Dim StartDate As Date, EndDate As Date
StartDate = Range("A1").Value
EndDate = Range("A2").Value

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=DL BATT;UID=sa;PWD=panelview900;APP=Microsoft®
Query;WSID=DGD7K22J;DATABASE=SerialLinks" _
, Destination:=Range("A3"))
.CommandText = Array( _
"SELECT vwOvenData.PushDateTime, vwOvenData.OvenID,
vwOvenData.MaxPush1, vwOvenData.MaxPush2" & Chr(13) & "" & Chr(10) & "FROM
SerialLinks.dbo.vwOvenData vwOvenData" & Chr(13) & "" & Chr(10) & "WHERE
(vwOvenData.PushDateTime={'" & Format(StartDate, "yyyy-mm-dd hh:mm:ss") &
"'} And " _
, _
"vwOvenData.PushDateTime<{'" & Format(EndDate, "yyyy-mm-dd
hh:mm:ss") & "'})" & Chr(13) & "" & Chr(10) & "ORDER BY
vwOvenData.PushDateTime" _
)
.Name = "Query from DL BATT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default SQL Syntax Error in Query

I think you need the ts and the space

from
PushDateTime={'" & Format(StartDate, "yyyy-mm-dd hh:mm:ss")
to
PushDateTime={ts '" & Format(StartDate, "yyyy-mm-dd hh:mm:ss")

from
PushDateTime<{'" & Format(EndDate, "yyyy-mm-dd hh:mm:ss"
to
PushDateTime<{ts '" & Format(EndDate, "yyyy-mm-dd hh:mm:ss"

"jim hardwick" wrote:

I have a query which I wnat to run between two dates (yyyy-mm-dd hh.mm.ss
format) which are in two specified cells on a workbook, tried the routine
below but when macro gets to " .Refresh BackgroundQuery:=False" line at
bottom I get Run Time error 1004 - SQL Syntax Error (Full code below).

Please help.

Sub GetData()
'
' GetData Macro
' Macro recorded 30/11/2007 by
'

Worksheets("Push Data").Activate

Dim StartDate As Date, EndDate As Date
StartDate = Range("A1").Value
EndDate = Range("A2").Value

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=DL BATT;UID=sa;PWD=panelview900;APP=Microsoft®
Query;WSID=DGD7K22J;DATABASE=SerialLinks" _
, Destination:=Range("A3"))
.CommandText = Array( _
"SELECT vwOvenData.PushDateTime, vwOvenData.OvenID,
vwOvenData.MaxPush1, vwOvenData.MaxPush2" & Chr(13) & "" & Chr(10) & "FROM
SerialLinks.dbo.vwOvenData vwOvenData" & Chr(13) & "" & Chr(10) & "WHERE
(vwOvenData.PushDateTime={'" & Format(StartDate, "yyyy-mm-dd hh:mm:ss") &
"'} And " _
, _
"vwOvenData.PushDateTime<{'" & Format(EndDate, "yyyy-mm-dd
hh:mm:ss") & "'})" & Chr(13) & "" & Chr(10) & "ORDER BY
vwOvenData.PushDateTime" _
)
.Name = "Query from DL BATT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default SQL Syntax Error in Query

Thanks a lot thats sorted it!

Jim

"Joel" wrote:

I think you need the ts and the space

from
PushDateTime={'" & Format(StartDate, "yyyy-mm-dd hh:mm:ss")
to
PushDateTime={ts '" & Format(StartDate, "yyyy-mm-dd hh:mm:ss")

from
PushDateTime<{'" & Format(EndDate, "yyyy-mm-dd hh:mm:ss"
to
PushDateTime<{ts '" & Format(EndDate, "yyyy-mm-dd hh:mm:ss"

"jim hardwick" wrote:

I have a query which I wnat to run between two dates (yyyy-mm-dd hh.mm.ss
format) which are in two specified cells on a workbook, tried the routine
below but when macro gets to " .Refresh BackgroundQuery:=False" line at
bottom I get Run Time error 1004 - SQL Syntax Error (Full code below).

Please help.

Sub GetData()
'
' GetData Macro
' Macro recorded 30/11/2007 by
'

Worksheets("Push Data").Activate

Dim StartDate As Date, EndDate As Date
StartDate = Range("A1").Value
EndDate = Range("A2").Value

With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=DL BATT;UID=sa;PWD=panelview900;APP=Microsoft®
Query;WSID=DGD7K22J;DATABASE=SerialLinks" _
, Destination:=Range("A3"))
.CommandText = Array( _
"SELECT vwOvenData.PushDateTime, vwOvenData.OvenID,
vwOvenData.MaxPush1, vwOvenData.MaxPush2" & Chr(13) & "" & Chr(10) & "FROM
SerialLinks.dbo.vwOvenData vwOvenData" & Chr(13) & "" & Chr(10) & "WHERE
(vwOvenData.PushDateTime={'" & Format(StartDate, "yyyy-mm-dd hh:mm:ss") &
"'} And " _
, _
"vwOvenData.PushDateTime<{'" & Format(EndDate, "yyyy-mm-dd
hh:mm:ss") & "'})" & Chr(13) & "" & Chr(10) & "ORDER BY
vwOvenData.PushDateTime" _
)
.Name = "Query from DL BATT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error in VBA in query from Access Winget Excel Discussion (Misc queries) 0 January 21st 10 07:49 PM
Excel VB - WHERE syntax error in Access web query. [email protected] Excel Programming 1 October 20th 06 07:05 PM
MS Query import from Excel - Syntax Error EstherJ Excel Discussion (Misc queries) 1 April 24th 06 09:44 AM
Syntax Error in Excel Query for DATE field James T Excel Discussion (Misc queries) 3 August 31st 05 12:33 PM
Macro syntax error when running query rrmando Excel Programming 0 July 26th 04 10:42 PM


All times are GMT +1. The time now is 09:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"