Solved

Using IN with an MS Access query

Posted on 2015-01-20
9
101 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 119

Expert Comment

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

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

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 47

Expert Comment

by:Dale Fye (Access MVP)
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
 
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
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 34

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 119

Accepted Solution

by:
Rey Obrero earned 500 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now