Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    If then statement (2002)

    Hi all -
    I have if then statement that I cannot get to work the way I want it to. After the message box pops up and the user clicks OK, I want the focus to stay on that field.

    Private Sub AHrs_BeforeUpdate(Cancel As Integer)
    If ENO > 1 And ahrs > 11 Then
    MsgBox "Too Many Hours"
    Forms!frmcard!ahrs.SetFocus
    End If
    End Sub

    I tried the set focus method and the go to control method. I get a run time error 2108, you must save the field before you execute the gotocontrol action or the setfocus method.

    How can I do what I want to do???
    Thanks in advance.
    John

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If then statement (2002)

    You didn't cancel the update event, so the focus moved on. Try this:

    Private Sub AHrs_BeforeUpdate(Cancel As Integer)
    If ENO > 1 And ahrs > 11 Then
    MsgBox "Too Many Hours"
    Cancel = True
    End If
    End Sub
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: If then statement (2002)

    Thats exactly what I needed. Thank you.
    John

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    284
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If then statement (2002)

    I'm trying to do my own message box to pop up when nothing is entered in a required field. I've used the following code in the BeforeUpdate property:
    Private Sub InvDate_BeforeUpdate(Cancel As Integer)

    If InvDate = Null Then
    MsgBox "Oops, you forgot to put a date in."
    Cancel = True
    End If

    End Sub

    But this code doesn't work. When I have this code in, none of my fields check for errors. Even when I get rid of my code, my fields still don't check for errors. My fields are set as required. Those fields were showing the regular Access error message when I tried to skip past one of those fields without putting something in, but now that doesn't even work. What did I do???

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: If then statement (2002)

    Hi Jennifer,

    Null is rather weird. Since it represents a missing value, you can't really test for = Null. Use the IsNull function instead:

    If IsNull(InvDate) Then

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    284
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If then statement (2002)

    I tried that, but it's still not working right. Could it be that I have it attached to the BeforeUpdate property? It doesn't work on my date field when I don't enter anything and press enter to go on to the next field.

  7. #7
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: If then statement (2002)

    Do you mean the BeforeUpdate event of the form, or of the control? If it's the control, the event won't fire if you don't enter anything. Would it not be simpler to make it a required field from a validation point of view, either at the table or the form level?
    Wendell

  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    284
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If then statement (2002)

    Yes, I mean the control. I do have the field setup as required, but I wanted to put my own message if the user doesn't enter anything. That's where the problem lies. How do I do that?

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: If then statement (2002)

    You can handle errors at the form level, by writing an On Error event handler for the form. If you leave a required field empty, error number 3314 occurs. You could do something like this:

    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 3314 Then ' Required field empty
    MsgBox "You left a required field blank. Please fill it in.", vbCritical
    Response = acDataErrContinue
    Else ' Let Access handle other errors
    Response = acDataErrDisplay
    End If
    End Sub

    If there are other errors you want to handle at the form level, you can use a Select Case statement instead of If ... Then ... Else ... End If

  10. #10
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Canada
    Posts
    284
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If then statement (2002)

    Thanks, Hans! It works great. You are as brilliant as always.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •