Solved

Using IN with an MS Access query

Posted on 2015-01-20
9
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 38

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 48

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 48

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 38

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

717 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