Solved

Query criteria from form, multiple values

Posted on 2014-04-29
6
1,047 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

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.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

770 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