bit of VBA code wrong

Duritz

Board Regular
Joined
Apr 28, 2005
Messages
226
Can someone with more VBA ability than my humble self have a quick peek at this and tell me where I've mucked it up? The error message is below: (this is querying an Access database)

Code:
sql_st = "Select * FROM data Where cat_dist = " & dist & " AND where reliability_best_now = " & reliability & " AND where EP_THEN_FINAL_MARK = '" & form & "' AND where cat_fitness = '" & fitness & "'"

and the error message says there's a Syntax error (missing operator)

cat_dist and reliability_best_now are both numbers, the other two are both text
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
Hi Duritz, it looks like you have too many WHERE operators. Try
Code:
sql_st = "Select * FROM data Where cat_dist = " & dist & " AND reliability_best_now = " & reliability & " AND EP_THEN_FINAL_MARK = '" & form & "' AND cat_fitness = '" & fitness & "'"
If that doesn't do the trick, try also using parentheses:
Code:
sql_st = "Select * FROM data Where ((cat_dist = " & dist & ") AND (reliability_best_now = " & reliability & ") AND (EP_THEN_FINAL_MARK = '" & form & "') AND (cat_fitness = '" & fitness & "'))"
Denis
 

Duritz

Board Regular
Joined
Apr 28, 2005
Messages
226
Ah yes the Where operators, of course. Removing that has done it. Thanks Denis.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Duritz

Just to clarify this is not really a VBA error, it's an error in the syntax of the SQL statement.:)
 

Forum statistics

Threads
1,137,202
Messages
5,680,145
Members
419,886
Latest member
Pulpboi

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
Top