Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Creating a parameter query
Pierre-Jean Tessier 
    
4 years ago
Hi, I want to create a query that will prompted me with the choice of data to enter separated by a comma. (parameter prompt that accepts multiple values)

Exemple: Prompt box opens, I type 11444,1443,14455 and then the query shows the results for these 3 inputs only... Sometimes I have to input 2 numbers, 4 numbers, etc...it's never the same amout.
I want the values typed to be separated by a comma.

I tryied this examble below using the INSTR function but Access is saying that the syntax is not valid

InStr([Enter number separated by commas],[Numbers])>"0"

I am using Access 365

Any ideas to solve this problem would be appreciated
Thanks!
Adam Schwanz  @Reply  
           
4 years ago
Try doing Replace([Enter Number Seperated By Commas],","," or ")
Adam Schwanz  @Reply  
           
4 years ago
That doesn't appear to work hmm. This is the code I've made before to do it on a textbox in a search form. This is with VBA, much more advanced, but might give you an idea.

If QuestionType <> "" Or QuestionType <> 0 Then
        If Wh <> "" Then Wh = Wh & " " & "AND" & " "
        
        If InStr(1, QuestionType, ",") Then
            Dim QType
            QType = Split(QuestionType, ",", 5)

            If UBound(QType) = 4 Then
                Wh = Wh & "QuestionType LIKE '*" & QType(4) & "*' or (QuestionType Like '*" & QType(3) & "*' or QuestionType Like '*" & QType(2) & "*' or QuestionType Like '*" & QType(1) & "*' or QuestionType Like '*" & QType(0) & "*')"
            ElseIf UBound(QType) = 3 Then
                Wh = Wh & "QuestionType LIKE '*" & QType(3) & "*' or (QuestionType Like '*" & QType(2) & "*' or QuestionType Like '*" & QType(1) & "*' or QuestionType Like '*" & QType(0) & "*')"
            ElseIf UBound(QType) = 2 Then
                Wh = Wh & "QuestionType LIKE '*" & QType(2) & "*' or (QuestionType Like '*" & QType(1) & "*' or QuestionType Like '*" & QType(0) & "*')"
            ElseIf UBound(QType) = 1 Then
                Wh = Wh & "QuestionType LIKE '*" & QType(1) & "*' or (QuestionType LIKE '*" & QType(0) & "*')"
            ElseIf UBound(QType) = 0 Then
                Wh = Wh & "QuestionType LIKE '*" & QType(0) & "*'"
            End If
        Else
            Wh = Wh & "QuestionType LIKE '*" & QuestionType & "*'"
        End If
        
    End If
Kevin Yip  @Reply  
     
4 years ago
Any attempt to manipulate the parameter won't work because Access will always sees the parameter as one single value. For instance, if your query is:

    SELECT * FROM MyTable WHERE MyField IN ([user inputs])

If the user enters 1,2,3,4 , thinking the query will become:

    SELECT * FROM MyTable WHERE MyField IN (1,2,3,4)

But Access will see it as:

    SELECT * FROM MyTable WHERE MyField IN ("1,2,3,4")

which will likely return no result, maybe an error too.

To accomplish the original goal, you need multiple parameters:

    SELECT * FROM MyTable WHERE MyField IN ([input1], [input2], [input3], [input4])

Or you can use VBA to construct the SQL using string concatenation, with the user entering the inputs on a form or something.
Kevin Robertson  @Reply  
          
4 years ago
Good code Adam but limited to 5 elements.
I would use a loop so more elements can be added without having to modify the code.
This is what I came up with based on your code above.

Dim QuestionType As String, wh As String
Dim QType As Variant, X As Long
QuestionType = "1,2,3,4,5"
    
If QuestionType <> "" Or QuestionType <> 0 Then
    If wh <> "" Then wh = wh & " " & "AND" & " "
    If InStr(QuestionType, ",") Then
        QType = Split(QuestionType, ",")
        For X = 0 To UBound(QType)
            wh = Trim(wh & "QuestionType LIKE '*" & QType(X) & "*' or ")
            wh = Left(wh, Len(wh) - 2)  'remove the trailing 'or'
        Next
    End If
End If

Adam Schwanz  @Reply  
           
4 years ago
Nice. Yea, in my use ,they wanted the limit to always be 5 so made it easier :P.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 3:25:41 PM. PLT: 1s