Results 1 to 10 of 10
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    If statement


    I'm sure this should be an easy statement, but I can't seem to get it to work. I need to make sure information is put into 4 fields or I want it to give a message and quit. If the criteria is there, open the form. Here's what I tried. It doesn't work.

    If IsNull(cboInciMonth) And IsNull(txtInciYear) And IsNull(cboRptMonth) And IsNull(txtRptYear) Then MsgBox "You must enter a criteria"
    Exit Sub
    Else
    DoCmd.OpenForm "frmMappsSearchResults"

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Your expression requires *all* fields to be Null before displaying th message. You may wish to use OR instead of AND.

  3. #3
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I want it to be so that some of the criteria can be entered. Shouldn't it be AND? This is the error I'm getting:

    Click image for larger version. 

Name:	error.jpg 
Views:	15 
Size:	13.4 KB 
ID:	33166

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The code you posted has no End If at the end.

  5. #5
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Been writing it several times. Forgot that. Still getting the same error. Here's the code:

    If IsNull(cboInciMonth) And IsNull(txtInciYear) And IsNull(cboRptMonth) And IsNull(txtRptYear) Then MsgBox "You must enter a criteria"
    Exit Sub
    Else
    DoCmd.OpenForm "frmMappsSearchResults"
    End If

    End Sub

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try This:
    Code:
    If IsNull(cboInciMonth) Or IsNull(txtInciYear) Or IsNull(cboRptMonth) Or IsNull(txtRptYear) Then
       MsgBox "You must enter a criteria"
    Else
       DoCmd.OpenForm "frmMappsSearchResults"
    End If

  7. #7
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I got the message to appear without the error. But, now all the fields have to entered. So the query has become useless.

  8. #8
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I replaced the OR with AND. It works. I'm not sure what the change was.

  9. #9
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    Looks like the EXIT SUB was causing problems. I had it without it and it still gave me the same error. I'm not sure what happened, but glad it works. Thanks a lot.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Thanks for posting back with your solution and success. Follow the link in my signature to mark this thread as Solved.

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

Similar Threads

  1. IF Then Else VBA statement
    By ui7598h in forum Programming
    Replies: 7
    Last Post: 10-28-2015, 11:43 AM
  2. VBA If Statement
    By BatmanMR287 in forum Access
    Replies: 4
    Last Post: 05-19-2015, 03:13 AM
  3. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  4. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  5. Replies: 7
    Last Post: 08-17-2011, 01:49 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