Results 1 to 11 of 11
  1. #1
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31

    Stop record save after validation fails

    Hi,



    I am using my form's "Before Update" event to perform a bunch of validation on the entries that the user has made in the form. The validation logic is all working fine, however I'm finding that although the validation code works, and alerts the user to their mistakes, the "Before Update" event completes and the next actions that are bound to the save button (that I'm using to trigger the "Before Update") continue and the record is saved regardless.

    I have tried using "End" in my validation code when it detects an error, but this closes the form, which isn't desirable.

    I thought I could pass a return value from my error checking sub to the "On Click" event that saves the record, but you can't return values from a Sub.

    I feel like I'm missing something obvious here - does anyone have any ideas? Thanks!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    In the Beore Update event, use:
    Cancel = True
    to stop the update.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Edgar is online now Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    The Before Update event comes with a Cancel parameter. It's set to a default argument of False. You must set it to true.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If isNull(someControl) Then
            Cancel = True
            Msgbox "Not saved"
        Else
            Msgbox "Saved"
        End if
    End Sub

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Have you thought to lookup the syntax etc for that event?
    https://learn.microsoft.com/en-us/of...reupdate-event
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Thanks all for your replies. I think I'm missing something fundamental though. I have a button on my form who's OnClick Event has the following code in it:

    Code:
    DoCmd.Save
    DoCmd.Close acForm, "frmEnterLineItem" 'close the form
    DoCmd.OpenForm "frmEnterLineItem" 'reopen the form for new data entry
    I now understand that the "DoCmd.Save" command will trigger the form's BeforeUpdate event, and I can build my error-detection code within that.

    My question is though, if the error detection code detects some incorrect data in the form and asks the user to redo it, how do I stop the main "DoCmd.Save" from firing once the BeforeUpdate event code finishes running? Do I need to put the "DoCmd.Save" into a while loop and detect the return value of the BeforeUpdate Sub? If so, how do I do that - I wasn't sure that Subs can return any values.

    As you can tell I'm a bit stumped and hope someone can help.

    Cheers,

    Phil

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Can you describe in simple English WHAT you are doing with the form(s) involved? Sometimes writing the process down will help clarify the requirement and approach. You could show us the code behind the form, or attach a copy of the database with instructions to highlight an issue.

    Oooops I see Paul has responded while I was typing.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Simple enough to test. Try Edgar's code in post 3 and see what happens.

    I thought I could pass a return value from my error checking sub to the "On Click" event that saves the record, but you can't return values from a Sub.
    Put your validation code into a function and return a boolean.

    Then you can use

    Code:
    Cancel = YourFunctionName

    but you can't return values from a Sub.
    You can, sort of, but a function is better suited.

    Code:
    Sub SubOne()
        Dim x As Integer
        x = 12
        SubTwo x
        Debug.Print x
    End Sub
    
    
    Sub SubTwo(ByRef x As Integer)
        x = 100
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Welshgasman - Your insinuation that I haven't searched around on this topic before posting is unjustified. Believe it or not there are quite a few different google searches one can perform on a subject, so if I had missed one it's hardly surprising. I use this forum as a last-resort, not as a first.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    how do I stop the main "DoCmd.Save" from firing once the BeforeUpdate event code finishes running?
    Basic framework could look something like

    Code:
    Private Sub Command2_Click()
    
        If ChkData Then
            MsgBox "Nope, Bad Data"
            Me.Undo
        Else
            DoCmd.Close acForm, Me.Name
        End If
        
    End Sub
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Cancel = ChkData
    End Sub
    
    Private Function ChkData() As Boolean
        'your validation code here
        'if data is valid return false
        'if data is bad return true
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    That looks awesome and exactly what I was looking for moke123 - thank you! I'll have a go at implementing that and let you know how I get on.

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

Similar Threads

  1. Replies: 10
    Last Post: 10-21-2022, 04:38 PM
  2. Replies: 3
    Last Post: 09-13-2019, 06:07 AM
  3. Cancel Save if data validation fails
    By sra2786 in forum Forms
    Replies: 3
    Last Post: 12-16-2018, 11:52 AM
  4. Replies: 11
    Last Post: 08-23-2016, 04:52 PM
  5. Replies: 1
    Last Post: 08-21-2015, 12:03 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