Solved

Using IN with an MS Access query

Posted on 2015-01-20
9
99 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

10 Experts available now in Live!

Get 1:1 Help Now