locked
[VBA + Access 2k3] DoCmd.CancelEvent not working in Select Case RRS feed

  • Question

  • Hi,

    I have added a couple of rules in my save button OnClick event, and this is an exemple:

    Select Case (IsNull(Me![product]) Or Me![product] = "")
            Case True
                MsgBox "Please choose product", , "Data entry required..."
                DoCmd.CancelEvent
                Me.product.SetFocus
            case false
    **SAVE THE STUFF**
    End Select
    

    but, it's saving anyway...

    How do I fix it, please?

    Regards

    Wednesday, October 19, 2011 7:14 PM

Answers

  • You have to place the code in the BeforeUpdate Event of your Form, so you are able to Cancel the Record before saving it.

    Also little adjust in the code, and it will work fine, see below code:

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    Select Case IsNull(Me.product)
    
    Case True
    
    MsgBox "Please choose product", , "Data entry required..."
    ' cancel saving record
    Cancel = True
    ' setfocus on the product field
    Me.product.SetFocus
    
    Case Else
    
    MsgBox "We are all good, record saved!"
    ' if Bound it will save automaticly
    'DoCmd.Save
    
    End Select
    
    End Sub
    

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    • Proposed as answer by danishani Wednesday, October 26, 2011 1:58 AM
    • Marked as answer by danishani Wednesday, January 11, 2012 8:32 PM
    Wednesday, October 19, 2011 8:44 PM