Reputation: 79
I can't figure out which part of this code is written incorrectly.
In my query string, i'm trying to collect a range of records between the values of sFrom and sTo. Basically, I want to make it so that out of all the control numbers that I collect from my database, the only ones that get displayed will be whatever the user inputs for the starting range (sFrom) and the ending range (sTo).
sQuery = "SELECT " & sFields & " FROM " & sTable &
" Where store_id Like 'XXX'" & sFilter &
" Where control_no > sFrom and < sTo " &
order by " & sOrderBy
Upvotes: 0
Views: 1296
Reputation: 24253
The best way to debug this is to see what the resultant SQL statement is and work from there.
Yours currently evaluates to something like
SELECT field, field2 FROM table Where store_id Like 'XXX'FilterString Where control_no > sFrom and < sTo order by field1
sFilter
inclusion is very wrong, but it depends in what the exact value is.sFrom
and sTo
are included as literals, not evaluated (See tcarvin's answer)WHERE
clause needs to be full and complete. You can't omit the field to check (See Darrel's answer)Upvotes: 1
Reputation: 5689
Like 'XXX'" & sFilter
, you have no space between X
' and the next string.sFrom
and sTo
are literal strings. You probably meant to use them as variables.control_no
and sTo
.sFilter
and the surrounding SQL.order by
.Now, put it all together:
sQuery = "SELECT " & sFields & " FROM " & sTable & _
" WHERE store_id LIKE 'XXX'" & _
" AND " & sFilter & _
" AND control_no > " & sFrom & _
" AND control_no < " & sTo & _
" ORDER BY " & sOrderBy
Upvotes: 1
Reputation: 10855
This:
" Where control_no > sFrom and < sTo "
should probably be this:
" Where control_no >= " & sFrom & " and control_no <= " & sTo
(Assuming of course that sFrom and sTo are string variables populated with the numeric input from the user)
Upvotes: 2
Reputation: 142014
You can't drop the control_no before the <
sQuery = "SELECT " & sFields & " FROM " & sTable & " Where store_id Like 'XXX'" & sFilter & " Where control_no > sFrom and control_no < sTo " & order by " & sOrderBy
Upvotes: 1