Results 1 to 2 of 2
  1. #1
    Phlee is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2011
    Posts
    4

    How to make multiple criteria query when sometimes the criteria can be blank

    Hi,



    I am pretty new to Access (first project)

    I know how to make a query by form where I can query using selected information on a form.

    I have 2 specific questions:

    1. How do I do this with multiple criteria? Also, if I add multiple criteria, is it okay if sometimes some of those criteria are left blank?!

    2. How can I make a date range a criteria?

    Thanks!

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    I wouldn't put the criteria on the query. I would put it in the event which opens the form (like the click event of a button), and you can use it like this (which will just ignore it if blank):
    Code:
    Dim strWhere As String
     
    ' For Numeric Datatypes
    If Len(Me.Control1NameHere & vbNullString) > 0 Then
       strWhere = "[Field1NameHere] = " & Me.Control1NameHere & " AND "  ' this assumes a numeric datatype otherwise Chr(34) is needed to add double-quote)
    End If
     
    ' For text datatypes
    If Len(Me.Control2NameHere & vbNullString) > 0 Then
       strWhere = strWhere & "[Field2NameHere] = " & Chr(34) & Me.Control2NameHere & Chr(34) & " AND "  ' this example shows that Field2NameHere is a text datatype so we added the Chr(34) code
     
    ' For date range
    If Len(Me.Control3NameHere & vbNullString) > 0 And Len(Me.Control4NameHere & vbNullString) > 0 Then
       strWhere = strWhere & "[DateField] Between #" & Format(Me.Control3NameHere, "mm\/dd\/yyyy") & "# AND #" & Format(Me.Control4NameHere, "mm\/dd\/yyyy") & " AND "  ' Dates use # for delimiters and the format is set to make sure regional settings don't affect the SQL
    End If
     
    ' If the start date is left blank
    If Len(Me.Control3NameHere & vbNullString) = 0 And Len(Me.Control4NameHere & vbNullString) > 0 Then
       strWhere = strWhere & "[DateField] <= #" & Format(Me.Control4NameHere, "mm\/dd\/yyyy") & " AND "
    End If
     
    ' If the ending date is left blank
    If Len(Me.Control3NameHere & vbNullString) > 0 And Len(Me.Control4NameHere & vbNullString) = 0 Then
       strWhere = strWhere & "[DateField] >= #" & Format(Me.Control3NameHere, "mm\/dd\/yyyy")  & " AND "
    End If
     
    ' we remove the ending AND
    If strWhere <> vbNullString Then
       strWhere = Left(strWhere, Len(strWhere) - 5)
    End If
     
    ' if filtering the form this code is on then
    Me.Filter = strWhere
    Me.FilterOn = True
     
    ' if filtering a different form by opening:
    ' DoCmd.OpenForm "FormNameHere", WhereCondition:=strWhere

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

Similar Threads

  1. Replies: 9
    Last Post: 05-05-2011, 02:05 PM
  2. Search By Criteria - Flexible Criteria Fields
    By lilanngel in forum Access
    Replies: 0
    Last Post: 03-16-2011, 06:25 PM
  3. Replies: 0
    Last Post: 04-08-2010, 12:22 PM
  4. Query multiple field & criteria
    By fua in forum Access
    Replies: 2
    Last Post: 11-04-2009, 08:22 PM
  5. Multiple criteria query
    By DJ-Specter in forum Queries
    Replies: 1
    Last Post: 09-23-2009, 04:47 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