[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1323
  • Last Modified:

Access - Understaning DataErr

Please explain the logic when using DataErr from start to finish.  Do you try to inclue all errors in the Form_Error section that might be encountered somewhere in the application or in a form?  Where does the code reference or activate a specific DataErr such as the one listed below?  

Private Sub Form_Error(DataErr As Integer, response As Integer)
    response = acDataErrContinue
    Select Case DataErr
                 Case 3022
            MsgBox "Account Number already exist for this REGU."
0
SharonInGeorgia
Asked:
SharonInGeorgia
  • 2
1 Solution
 
mbizupCommented:
- DataErr is the error code returned if an error occurs in your form.  
- Checking the value of DataErr against certain expected errors allows you to create custom error messages instaeac of having the user see the system generated messages. It also allows you to gracefully handle error consitions through code.

>Do you try to inclue all errors in the Form_Error section ...
Include those errors that are reasonable to expect in your code's execution, and include a Case Else to generically handle other errors.

0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<Do you try to inclue all errors in the Form_Error section that might be encountered somewhere in the application or in a form?>>

  No not typically.  You'd do something more like this:

    Select Case DataErr
   
    Case 3022
            MsgBox "Account Number already exist for this REGU."
            response = acDataErrContinue
    Case Else
           MsgBox "Unexpected error " & DataErr & " encountered."
           If DebugMode() then Stop
    End Select

  I use that type of logic to indicate that I have code in the OnError event.  Once I place code there to trap for an error, I always include a case else.  I check the DebugMode() for true (a flag I set to indicate I'm in development mode) and STOP so I can debug and determine why I'm not trapping an error.  At runtime, you'd just get the message box.

<<Where does the code reference or activate a specific DataErr such as the one listed below? >>

  It varies, but typically errors occur and the OnError event gets triggered for bound forms and most are related to data operations.  A bound forms functions are built into Access.  Internally it's performing some operation for you (ie. a record save) and if an error occurs, OnError is Access's way of giving you a chance to trap the error before it pops up its own message.

JimD.


0
 
SharonInGeorgiaAuthor Commented:
So do I put the DataErr code in the  Private Sub Form_Error for each form?  
0
 
SharonInGeorgiaAuthor Commented:
Also, I'm getting the message "Record Saved' before I get the "Duplicate Error Message".  Where do I need to put the error code to get the Duplicate message first?

Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim response As vbmsgboxresult

    If IsNull([GL Account]) Or [GL Account] = "" Then
    MsgBox "Account Number Is A Required Field", vbCritical + vbOKOnly + vbDefaultButton1, "Missing Data"
    Cancel = True
Exit Sub
   
  Else
    response = MsgBox("Do you want to save this Account Code", vbQuestion + vbYesNo, "Save Account Code")
    If response = vbYes Then
    MsgBox "Account Code has been added", vbOKOnly, "Account Added"
    Else
    MsgBox "This Account Code will not be saved", vbOKOnly, "Unsaved"
   
    End If
End If

End Sub
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now