Results 1 to 2 of 2
  1. #1
    wrandyrice is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    1

    Text String from Form Query - Multiple Choices

    Been a while since I put hands on access. Working with a very simple database.



    Working with 1 table, 1 form, 1 query.

    Query has several fields. Field of interest contains names of counties.

    In query design view I can type, "CountyA" Or "CountyB" or "CountyC" in the criteria and the desired results are returned.

    On form I've created a text box and an open report button. The report I'm trying to open uses the query as the data source. I reference the text box on the form as the criteria for the query as follows:
    [Forms]![frm_LMIBasicReport]![txtCounties][Forms]![frm_LMIBasicReport]![txtCounties]

    If I enter the the name of a single county into the form it executes perfectly. If I try to enter multiple counties it fails. This is true whether I just enter the county names or replicate the exact criteria string I use in the source query. For example
    CountyA works and returns desired values for County A
    CountyA Or CountyB or CountyC fails. The report opens but no records are returned
    "CountyA" Or "CountyB" or "CountyC" = exactly how it is entered in the query if I'm not using the form opens report but no records are returned

    Please advise, how can I pass multiple values from a single text box to the query.

    Thank you,

    wrandyrice

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    While you can type "CountyA" Or "CountyB" or "CountyC" into the criteria in query design view, that is not the SQL it generates. If you look at the query in SQL view, you will see that the SQL looks something like : (field1 = "CountyA") Or (field1 = "CountyB") or (field1 = "CountyC")

    There is an easy other way to do what you need to do.

    First, remove the reference to the form field from the query criteria.

    Then, in your form, put the required criteria into a string variable so that it looks like a WHERE clause, but without the "where", using the proper syntax as I pointed out above.

    Then open the report with the Docmd.openreport, with the string variabe (criteria) as the 4th parameter:

    Dim SQL as string
    SQL = "Field1 = 'County a' or field1 = 'County B' or Field1 = 'County C' "
    Docmd.OpenReport ReportName,,,SQL

    You will need some VBA to create the proper string format if the user enters "A" or "B" or "C"

    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with form - list of choices
    By lios1984 in forum Access
    Replies: 11
    Last Post: 02-05-2012, 12:32 PM
  2. Replies: 3
    Last Post: 08-19-2011, 09:25 AM
  3. Query to Limit Combo Box Choices
    By jimrockford in forum Queries
    Replies: 2
    Last Post: 04-23-2011, 10:24 PM
  4. Replies: 4
    Last Post: 04-07-2011, 03:16 PM
  5. Multiple Choices for Parameters with VBA and Queries
    By weasel7711 in forum Programming
    Replies: 3
    Last Post: 02-02-2011, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums