SQL issue Syntax error missing operator

carlleese24

Board Regular
Joined
Mar 15, 2005
Messages
108
Hi all

Hope someone can help me with the below as I will be very grateful if they did

The below is an sql code I run on VBA in excel and it keeps giving me a syntax error missing operator but unsure where to put this.
What I am trying to do is pull data between 04/12/2022 to 10/12/2022 only for JDA and JDA2

1648257119377.png

VBA Code:
sSQLSting = "SELECT *"

sSQLSting = sSQLSting & "FROM [" & table & "]" & ""


sSQLSting = sSQLSting & "WHERE ((" & field2 & ")= '" & (Criteria3) & "' AND"
sSQLSting = sSQLSting & "[" & field & "]Between # " & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & " OR"
sSQLSting = sSQLSting & "[" & field2 & "]= '" & (Criteria3) & "' AND"
sSQLSting = sSQLSting & "[" & field & "]Between # " & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & "#)"


Thank you in advance
 

Attachments

  • 1648257054219.png
    1648257054219.png
    110 KB · Views: 5

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try with these changes:
VBA Code:
sSQLSting = "SELECT * "

sSQLSting = sSQLSting & "FROM [" & table & "]" & " "


sSQLSting = sSQLSting & "WHERE ((" & field2 & ")= '" & (Criteria3) & "' AND "
sSQLSting = sSQLSting & "[" & field & "] Between #" & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & "# OR "
sSQLSting = sSQLSting & "[" & field2 & "]= '" & (Criteria3) & "' AND "
sSQLSting = sSQLSting & "[" & field & "]Between #" & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & "#)"
 
Upvote 0
Try with these changes:
VBA Code:
sSQLSting = "SELECT * "

sSQLSting = sSQLSting & "FROM [" & table & "]" & " "


sSQLSting = sSQLSting & "WHERE ((" & field2 & ")= '" & (Criteria3) & "' AND "
sSQLSting = sSQLSting & "[" & field & "] Between #" & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & "# OR "
sSQLSting = sSQLSting & "[" & field2 & "]= '" & (Criteria3) & "' AND "
sSQLSting = sSQLSting & "[" & field & "]Between #" & Format(Criteria, "yyyy-mm-dd") & "# AND #" & Format(Criteria2, "yyyy-mm-dd") & "#)"
Thank you for this it worked. Can't believe 1 asterisk caused me 2 hours of frustration.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top