Results 1 to 4 of 4
  1. #1
    sbar1 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    2

    stLinkCriteria with multiple criteria and NULL scenario

    I am currently using the code below, but want to modify it so if one of the three fields below is NULL that it will pull records matching the other two. Any help would be appreciated, I could not figure it out playing around with anything I found online.

    stLinkCriteria = "([BU]='" & Me![buselect] & _
    "') And ([Quarter]='" & Me![quarterselect] & _
    "') And ([Yr]='" & Me![yrselect] & "')"


    DoCmd.OpenForm stDocName, , , stLinkCriteria

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by sbar1 View Post
    I am currently using the code below, but want to modify it so if one of the three fields below is NULL that it will pull records matching the other two. Any help would be appreciated, I could not figure it out playing around with anything I found online.

    stLinkCriteria = "([BU]= '" & Me![buselect] & _
    "') And ([Quarter]='" & Me![quarterselect] & _
    "') And ([Yr]='" & Me![yrselect] & "')"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Build the WHERE clause on-the-fly(something like this):
    Code:
      Dim stLinkCriteria As String
      
      stLinkCriteria = ""
      
      If Not IsNull(Me.buselect) Then
        stLinkCriteria = "[BU]= '" & Me.buselect & "' AND "
      End If
    
      If Not IsNull(Me.quarterselect) Then
        stLinkCriteria = stLinkCriteria & "[Quarter]='" & Me.quarterselect & "' AND "
      End If
    
      If Not IsNull(Me.yrselect) Then
        stLinkCriteria = stLinkCriteria & "[Yr]= '" & Me.yrselect & "' AND "
      End If
    
      If Len(Trim(stLinkCriteria)) > 5 Then
      'remove the last 5 chars
        stLinkCriteria = Left(stLinkCriteria, Len(stLinkCriteria) - 5)
      Else
        'select all records
        stLinkCriteria = Empty
      End If
    
      DoCmd.OpenForm stDocName, , , stLinkCriteria

  3. #3
    sbar1 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2012
    Posts
    2

    Thanks ssanfu!

    Thank you so much! I had been trying for hours sadly, and the closest I got would have worked if I didn't leave out the "End if" code I think.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by sbar1 View Post
    Thank you so much! I had been trying for hours sadly, and the closest I got would have worked if I didn't leave out the "End if" code I think.
    You're welcome

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

Similar Threads

  1. Passing criteria "NULL" or "IS NOT NULL" to a query
    By SgtSaunders69 in forum Forms
    Replies: 1
    Last Post: 12-24-2011, 02:22 AM
  2. Replies: 12
    Last Post: 12-11-2011, 05:04 PM
  3. Replies: 1
    Last Post: 07-13-2011, 11:00 AM
  4. Replies: 1
    Last Post: 02-25-2011, 06:11 PM
  5. Replies: 1
    Last Post: 03-29-2010, 10:23 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