Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query criteria from form, multiple values

Posted on 2014-04-29
6
Medium Priority
?
1,478 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 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 49

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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 49

Accepted Solution

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 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