Solved

Query criteria from form, multiple values

Posted on 2014-04-29
6
986 Views
Last Modified: 2014-04-30
Lets say I have a query with a field called "Letter". I know how to use criteria from form, but this time I want to use a criteria with multiple values and I struggle. In my criteria form, called frmCriteria, I have a field called txtLetterCriteria. I use checkboxes and vba to build the value in this field so it could be something like this: In ('A';'C';'K').

When I use the text: In ('A';'C';'K') Directly in the criteria field of the query it workes fine, but when I use [Forms]![frmCriteria]![txtLetterCriteria] in the criteria field of the query it does not work.

Appreciate any tips or hint on the syntax I need to use to make this work.
0
Comment
Question by:hallpett
  • 3
  • 2
6 Comments
 
LVL 84
ID: 40029485
The correct syntax is this:

IN ('A','B','C')

You're using semicolons, where you should be using commas.

Also - can you show the SQL of that query that is referring to your form?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40029510
If you are trying to do something like:

SELECT yourTable.*
FROM yourTable
WHERE [SomeField] & [Forms]![frmCriteria]![txtLetterCriteria]

That will not work.  You could try:

WHERE INSTR([Forms]![frmCriteria]![txtLetterCriteria], "'" & [SomeField] & "'") > 0

But that will not be very efficient.  If you do that you don't need to include the "In" predicate or the ( ) around the values, all you would need in [Forms]![frmCriteria]![txtLetterCriteria] is the values: 'A', 'B', 'C'.

The other way to do this is to simply build the entire SQL string in your code and set the SQL property of the query to that, something like:

strSQL = "SELECT yourTable.* FROM yourTable " _
             & "WHERE [SomeField] IN ('A', 'B', 'C')"
currentdb.querydef("queryname").SQL = strSQL
0
 

Author Comment

by:hallpett
ID: 40029575
I was hoping I could avoid building the entire sql string. (About the semicolons; in norwegian version commas don't work)

This work:
This work
Not this (even if the string txtLetterCriteria is: In ("A";"C";"K") ):
Not this
Table look like this:
Table
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40029626
Did you try the option using instr?

You would set it up:
Felt: Instr([forms]![frmCriteria]![txtLetterCriteria], "'" & [Letter] & "'")
tabell: Test
Vilkar: > 0
0
 

Author Closing Comment

by:hallpett
ID: 40031289
Worked like a charm when I removed the "'", correct syntax:
Instr([forms]![frmCriteria]![txtLetterCriteria], [Letter])
Thank you, would not have found that solution by my own.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40031597
Glad I could help.

The method you indicated above will only work if the [Letter] field is actually a single character.   If your example is literally what your data looks like, and what you are building from the options selected, it should work fine.  

But if your example is just that, and the "Letters" are multiple characters, then you really need to add the single quotes around the [Letter] field.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Concatenating data within ACCESS table 3 20
Max per month 3 15
iSeries DB2 SQL - Request user input 12 7
Minus first query 1 6
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

744 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

9 Experts available now in Live!

Get 1:1 Help Now