Results 1 to 4 of 4
  1. #1
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370

    Query criteria from Combo box

    Hello all,

    I have a problem with what I think will be a simple fix.


    In one of my forms, I have an two unbound text boxes, txtStartDate and txtEndDate, and I also have an unbound combobox, cboAdminName.

    I have several forms that will be referring to these unbound text boxes and this combo box.

    The point of these unbound text boxes and combo box is to refine the Select query for the different forms that are calling on these unbound text boxes and combobox in their query criteria.

    The problem I'm having is that if I don't select txtStartDate And txtEndDate And cboAdminName, then my query throws will return blank.

    In my query, lets say MyDateField has the criteria of "Between Forms!MyForm!txtStartDate And Forms!MyForm!txtEndDate." My other field MyAdminField has the criteria "Forms!MyForms!cboAdminName."

    I'd like for it to basically ignore the criteria if I don't select a StartDate, EndDate, or AdminName, instead of putting a "" in the criteria.


    I'm thinking this will have a pretty simple solution.

    Thanks

  2. #2
    lfpm062010 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Location
    US
    Posts
    415
    This is one way to solve your issue.

    Since you use between, it is easier just choose a date like "1/1/1970" for start date when is null and date like "12/31/2099" for the end date is null. I think something like this should work (not tested).

    BETWEEN IIF(ISNULL(Forms!MyForm!txtStartDate), #1/1/1970#, Forms!MyForm!txtStartDate) AND
    IIF(ISNULL(Forms!MyForm!txtEndDate), "12/31/2099", Forms!MyForm!txtEndDate)

    Same apply for the cobmo box, except that you use like and append a "*" as part of the criteria. Again, code is not tested.

    LIKE IIF(ISNULL(Forms!MyForms!cboAdminName), "", Forms!MyForms!cboAdminName) & "*"

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You cant use form boxes in a query if there's nothing in them..so..
    Test all controls for a possible filter then build the where clause.

    Code:
    if not isnull(cboState) then   sWhere = sWhere & " and [state]='" & cboState & "'"
    if not IsNull(txtName) then    sWhere = sWhere & " and [Name]='" & txtName & "'"
    if not IsNull(chkContact) then sWhere = sWhere & " and [Contact]=" & chkContact.value
    
        'remove 1st And
    sWhere= mid(sWhere,5)
      'just use the filter
    me.filter = sWhere
    me.filterOn = true
       'OR   
       'apply the sql to the form
    sSql = "SELECT * FROM tblCompany WHERE " & sWhere

  4. #4
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Thanks for the replies guys, I'll test them out.

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

Similar Threads

  1. Form Combo as Query Criteria
    By SteveApa in forum Forms
    Replies: 1
    Last Post: 01-16-2015, 08:21 AM
  2. Combo box in query criteria
    By nittany77 in forum Queries
    Replies: 19
    Last Post: 08-19-2014, 04:02 AM
  3. Combo Box for Query CRITERIA?
    By TUPJK in forum Queries
    Replies: 5
    Last Post: 06-04-2014, 10:43 AM
  4. Combo Boxes Query Criteria help
    By noaccessguru in forum Queries
    Replies: 2
    Last Post: 04-30-2012, 08:09 PM
  5. Replies: 6
    Last Post: 06-29-2010, 09:56 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