Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002

    Unanswered: how can i validate the data before saving

    Hi Everybody,
    I had a form with some fields on it. i want to validate the date before updating the record.
    i write a function validateForm() which checks the data validation and returns true or false depending upon the invalid/valid data.
    i m calling the function on Form's BeforeUpdate Event. but when i run the code, and enter the invalid data, it Prompt me with my Custom Message but after that i gives me other msgs too. e.g
    You canceled the Previous Operation(2001).
    and if i try again enter the valid data to same field it gives me error
    Update or CancelUpdate without AddNew or Edit..

    Anybody can help me where is the Problem.
    Here is the BeforeUpdate Event & my Function

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error GoTo ErrorHandler

    Dim strMsg As String
    strMsg = "Data has changed."
    strMsg = strMsg & "Do you wish to save the changes?" & vbCrLf & vbCrLf
    strMsg = strMsg & " Click Yes to Save or No to Discard changes."

    If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then

    '** if invalid data
    If ValidateForm() = False Then
    ' DoCmd.RunCommand acCmdUndo
    Cancel = True

    End If
    End If
    Exit Sub
    MsgBox " Error No. " & Err.Number & ": Description : " & Err.DESCRIPTION
    Resume ErrorhandlerExit

    End Sub

    Private Function ValidateForm() As Boolean

    On Error GoTo ErrorHandler

    Dim strErrors As String

    'Non critical location errors
    If Len(Me.ID & "") = 0 Then strErrors = "Please enter the name of the building" & vbLf

    If Len(Me.Barcode & "") = 0 Then strErrors = strErrors & "Please enter a barcode." & vbLf

    If Len(strErrors) > 0 Then
    MsgBox strErrors, vbCritical, "Invalid Data"
    ValidateForm = False
    ValidateForm = True

    End If
    Exit Function
    MsgBox " Error No. " & Err.Number & ": Description : " & Err.DESCRIPTION
    Resume ErrorhandlerExit
    End Function

    Thanks in Advance


  2. #2
    Join Date
    Mar 2003
    Navarro, CA
    Hey, folks, I know this post is really old, but info on this question seems to be hard to come by. So I'll add my 2-cents-worth, while I'm working on something similar.

    I also put some data validation in the BeforeUpdate event. By putting message boxes before and after suspicious lines of code, I concluded that any DoCmd that tries to run initiates another BeforeUpdate event. In fact, anything that tries to save the record or move to another one - triggering Access to save the record unless you tell it otherwise (try using "Me.Undo" and/or "Me.CancelEvent") - initiates the BeforeUpdate event.

    Then Access backs out the DoCmd and gives you that "You Canceled the Previous Operation" message. The solution is to avoid trying to save any records until you know your code has cleared the error checking routine. Rather than relying on BeforeUpdate alone, take your validation code and put it in its own procedure, minus any commands that move to a new record or save one (or maybe ANY DoCmd, for all I know - haven't tested all possibilities here). Then call the same procedure again from within BeforeUpdate in case you missed validating the data somehow elsewhere; it's redundant, but it gives added security. If you caught the error before you reached BeforeUpdate, you should not ever see "You Canceled the Previous Operation," except I have read there are other possible causes, like a corrupt database or mismatched data types.

    Another example was my Form_Load event that included:
    DoCmd.GoToRecord , , acNewRec

    Even though I expected to see the form open on a new record, in fact the form's dataset first opened on the first record and then the DoCmd tried to move to the new record, triggering the BeforeUpdate event, even before I had changed anything!

    (Sometimes I yearn for the good old days using a simple procedural language...)

Posting Permissions

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