Solved

Query criteria from form, multiple values

Posted on 2014-04-29
6
1,246 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
[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
  • 3
  • 2
6 Comments
 
LVL 85
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 48

Expert Comment

by:Dale Fye
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
Dale Fye 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 48

Expert Comment

by:Dale Fye
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

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.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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