Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Mar 2003
    Location
    Summerville, South Carolina, USA
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    BeforeUpdate Event (Access 2000)

    I have code on a form that requires the user to enter a field before going to the next record or closing the record. It works fine if I use the X button to close the form and give the message box that I need. However, when I use a command button to close the form, it gives the message box but continues to close. Below is the code. Any idea on how I can make it cancel on the click of the command button? Thanks

    Private Sub Form_BeforeUpdate(Cancel As Integer)

    If IsNull([txtDate] or IsNull([txtRepName]) then
    MsgBox ("You must enter a date")
    Cancel = True
    End If
    End Sub

    Thanks

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: BeforeUpdate Event (Access 2000)

    One solution is to put this code into the code behind the command button, before the code that closes the form. If you already see the message box you don't need coe for another one.


    If IsNull([txtDate] or IsNull([txtRepName]) then
    exit sub
    End If
    Regards
    John



  3. #3
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: BeforeUpdate Event (Access 2000)

    Your BeforeUpdate code is fine. The code in your cmdSave button needs to be something like this:

    On Error goto ErrorHandler:
    If me.dirty then runcommand acCmdSaveRecord
    if me.dirty then exit sub
    me.close
    ExitHere:
    exit sub
    ErrorHandler:
    if err.number=2501 then resume next 'catches error about "runcommand canceled"
    msgbox err.description
    resume ExitHere
    end Sub
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

Posting Permissions

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