Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using IN with an MS Access query

Posted on 2015-01-20
9
Medium Priority
?
110 Views
Last Modified: 2015-01-20
I am using MS access as a database for a small website. I'm working on building a search page. Can you use IN as part of a query in Access on a filed that ins a number? If so, is there an example? My current statement looks like this:
SELECT * FROM Stats WHERE 1=1 And PosID IN (2,5) And Hits > 50

That does not return anything. The PosID field is a number. It will work on other fields that are text.
0
Comment
Question by:dodgerfan
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40560411
what about this as a test

SELECT * FROM Stats WHERE PosID IN (2,5)
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40560474
Yes, you can use IN() in a query, however, you can't make it variable.  In most queries, the criteria can be supplied at runtime - Select .. From .. Where somefield = Forms!yourform!somefield
But,
Select .. From .. Where somefield  In(Forms!yourform!somefield)  -- Will NOT WORK

To use the In() clause, you will need to build the SQL as a string using VBA and use the resulting string to replace a RowSource or RecordSoucre.  Or in the case of an action query, run the string using the .execute method.
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 40560482
Assuming that PosId and Hits are numeric, that syntax is correct, although i wonder what the purpose  is of the

1=1

Portion of the criteria is.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40560591
@dale 1=1 is a stand sql where clause technique used to allow a  where clause to be simply extended without considering if this is the first "variable" condition being implemented.... e.g.  clauses can be generated as AND x = ..... without logic being required to add the actual WHERE

hth
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 40560663
@Lowfatspread,

I guess I've been doing it the hard way all of these years, creating a varWHERE variant variable that is separate from the main strSQL and then appending the two.   Then, when I add parameters to the criteria I test

if Len(me.txt_SomeParameter) > 0 Then
    varWHERE = (varWHERE + " AND ") & " [SomeField] = " & me.txt_SomeParameter
End If

Then, after adding all parameters, I would use:

strSQL = strSQL & (" WHERE " + varWHERE)
0
 

Author Comment

by:dodgerfan
ID: 40560673
Thanks for the help. I tried a different version of Access. Using 2007 is giving me the problem. Using 2010 returns records with this statement: SELECT * FROM Stats WHERE PosID IN (2,5)
Is there some difference in versions I should know?
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40560723
If PosID is numeric, the results should be the same.  If it is not numeric, then you might run into differences as each version may process an invalid statement differently.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 40560756
using
SELECT * FROM Stats WHERE PosID IN (2,5)

returns records

then PosID is Definitely Number Data Type

there must be something wrong with your A2007 version.
try to uninstall the office 2007, reboot your pc then install office 2007.

note: do not install two version of access in one PC


.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40560949
also try:

SELECT * FROM Stats WHERE PosID IN (2,5) or IsNull(PosID)

The following is also a good way to check your data quality:

SELECT Vartype(PosID) As Data_Type, IsNull(PosID,"IsNull) As NullExists FROM Stats

to make sure all are 2 number not 8.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question