Solved

Using IN with an MS Access query

Posted on 2015-01-20
9
105 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 35

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 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 35

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 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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