Results 1 to 3 of 3
  1. #1
    theosgood is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2012
    Location
    Denver, CO
    Posts
    30

    Query with Dynamic Criteria

    I have a form with source = query

    Query is against a single table with the fields CentEnding(text) and Exclude7(yes/no). Exclude7 will either be Yes or No for all records regardless of value in CentEnding. And unfortunately, CentEnding cannot be changed to numeric if it makes it easier.

    I want to limit the records returned by the source query by setting the criteria for CendEnding to be, if Exclude7 = Yes then <> “97” (exclude 97), if Exclude7 = No then > “0” (all records).

    I’ve tried the following but keep getting a “data type mismatch in criteria” error:
    IIf(Exclude7 = -1, <>”97”, >”0”)
    IIf(Exclude7 = -1, “<>”97””, “>”0””)
    IIf(Exclude7 = -1, <>&”97”, >&”0”)
    IIf(Exclude7 = -1, “<>”&”97”, “>”&”0”)
    IIf(Exclude7 = -1, “<>&”97””, “>&”0””)
    IIf(Exclude7 = -1, &<>&”97”, &>&”0”)

    I could write a VBA module that builds the query dynamically but I can’t find anything on how to use a module as the source for a form.

    Suggestions please.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I could write a VBA module that builds the query dynamically but I can’t find anything on how to use a module as the source for a form.
    If you can create the SQL code for the query in VBA, you can assign that SQL code to a pre-defined query (the one your Form is based on).

    So let's say in your VBA you build the SQL code and assign it to a variable named "mySQL". And your Form is based on a query called "myQuery".
    Then, in VBA, you can update the SQL code of myQuery with the SQL code you just built like this:
    Code:
               CurrentDb.QueryDefs("myQuery").SQL = mySQL
    You could then, in turn, open your Form in the same VBA code immediately after it, like this:
    Code:
        DoCmd.OpenForm "myForm"

  3. #3
    theosgood is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2012
    Location
    Denver, CO
    Posts
    30
    Thanks. Your suggestion worked well for defining the query. However, it is in a subform that is part of a tabbed form and I am having issues getting it to refresh when the tab is selected. I appreciate your help and will post a new thread in forms.

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

Similar Threads

  1. Dynamic Form, Dynamic labels/ captions?
    By JFo in forum Programming
    Replies: 15
    Last Post: 10-12-2011, 08:33 PM
  2. Replies: 1
    Last Post: 07-30-2010, 10:28 AM
  3. Dynamic criteria Query
    By ser01 in forum Queries
    Replies: 7
    Last Post: 02-22-2010, 11:41 AM
  4. Dynamic Query
    By pushpm in forum Queries
    Replies: 0
    Last Post: 04-22-2009, 12:58 PM
  5. Dynamic SQL Query
    By Squeaner in forum Queries
    Replies: 0
    Last Post: 09-25-2008, 02:37 PM

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