Results 1 to 4 of 4
  1. #1
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727

    If Statement To Check If IsNull field


    I have something I haven't come across before or at least I haven't noticed. I may have to check all my other forms now. Anyway, I have this coded behind a save button:

    Code:
    If Me.Dirty Then    
        If IsNull(Me.txtReason) Then
            MsgBox "Please Enter A Reason.", vbInformation, "Requirement"
            Me.txtReason.SetFocus
            Exit Sub
        End If
            If MsgBox("Save Changes?", vbYesNo, "Save") = vbYes Then            
                DoCmd.RunCommand acCmdSaveRecord
                Me.cmbCustomer.SetFocus
                Exit Sub
            Else
                Me.cmbCustomer.SetFocus
                Exit Sub
            End If
    End if
    So it works fine sometimes. If I don't type anything into the txtReason field and click the save button it will execute as it should and prompt me to make sure to type something into the txtReason field. But if I type into the txtReason field and then backspace but leave the focus on that field, meaning the cursor doesn't leave that field, it will still allow me to save the record even though the txtReason field is blank.

    Does anyone know how to fix this?

  2. #2
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Also found out that this behavior is immune to combo boxes. If I mess with the combo box by saving it with or without text it will always prompt me select one of the values from the drop down list if I leave the combo box blank.

  3. #3
    data808 is offline Noob
    Windows 10 Access 2013 32bit
    Join Date
    Aug 2012
    Posts
    727
    Ok I figured out the problem. I also need to add "" to the code. So it should look like this:

    Code:
    If Me.Dirty Then        
        If IsNull(Me.txtReason) Or Me.txtReason.Value = "" Then
            MsgBox "Please Enter A Reason.", vbInformation, "Requirement"
            Me.txtReason.SetFocus
            Exit Sub
        End If
            If MsgBox("Save Changes?", vbYesNo, "Save") = vbYes Then            
                DoCmd.RunCommand acCmdSaveRecord
                Me.cmbCustomer.SetFocus
                Exit Sub
            Else
                Me.cmbCustomer.SetFocus
                Exit Sub
            End If 
    End if
    

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    IIRC, it is often done as
    If Me.txtReason & vbNullString = "" Then

    That way, if the control is null you're concatenating "" and asking if it contains "", so True. If it's not null then "Because I'm lazy" & vbNullString = "Because I'm lazy" then not True. Careful with that though, because if you concatenate vbNullString on to a numeric value, the value becomes a string.
    Last edited by Micron; 09-01-2023 at 06:36 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 3
    Last Post: 04-03-2021, 01:01 PM
  2. Nested IIf statement with IsNull
    By halnurse in forum Queries
    Replies: 2
    Last Post: 06-29-2018, 04:32 PM
  3. Replies: 5
    Last Post: 06-26-2014, 12:52 PM
  4. Replies: 4
    Last Post: 02-03-2014, 12:16 PM
  5. Using Check boxes for an if statement
    By brow1726 in forum Forms
    Replies: 3
    Last Post: 06-10-2013, 09:50 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