Solved

Query criteria from form, multiple values

Posted on 2014-04-29
6
1,083 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

821 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