Customising multiple error messages for a command button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ezzz
    New Member
    • Jan 2010
    • 28

    Customising multiple error messages for a command button

    I have customised a form to display my own error messages using:-

    Private Sub Form_Error(Data Err As Integer, Response As Integer)
    ' If an error occurs because of duplicate data in a required field
    ' Display own custom error message

    Const conErrDuplicate Key = 3022
    Const conErrZeroKey = 3058

    Select Case DataErr
    Case conErrDuplicate Key
    MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
    Response = acDataErrContin ue

    Case conErrZeroKey
    MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
    Response = acDataErrContin ue

    Case Else

    'Display a standard error message
    Response = acDataErrDispla y
    End Select

    End Sub

    In order to get my "Save" command button (not changed from Command26) to do the same i used:-
    Private Sub Command26_Click ()
    On Error GoTo Err_Command26_C lick

    DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

    Me.[Serial Number].SetFocus
    Me.Command26.Vi sible = False

    Exit_Command26_ Click:
    Exit Sub

    Err_Command26_C lick:

    If Err = 3022 Then
    MsgBox "This Serial Number already exsists: " & mSearch, , "Warning Duplicate Record!"

    Else
    ' Display the error number and the generic Access message.
    MsgBox "Error #: " & Err.Number & " " & Err.Description

    End If
    Resume Exit_Command26_ Click

    End Sub

    But this only works for the one error code. Is there a way to get my "Save" command button to work with multiple error codes like my form?
    PS. my programming skills are very basic at best, the above has been copied and adapted from info found on the net.
    Regards
    Ezzz
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    When you save the record, the Form's Before_Update event is automatically called, and you can use it to check for other errors, give a message, and cancel if necessary. If you set Cancel to True, you should get an error from the save record event. Here is an example straight from one of my projects.
    Code:
    Private Sub cmdSave_Click()
    On Error GoTo ErrorHandler
    
        RunCommand acCmdSaveRecord
                
        If CurrentProject.AllForms("frmViewProjects").IsLoaded Then
            Forms![frmViewProjects].Refresh
            Forms![frmViewProjects].Form_Current
            Forms![frmViewProjects].Update
        End If
                    
        DoCmd.Close acForm, "frmEditActivity"
        
    ExitCode:
        Exit Sub
    ErrorHandler:
        If Err.number = 3022 Then
            MsgBox "That activity already exists.", , "Duplicate Activity"
        Else
            HandleError Err.number, Err.Description, Me.Name & "::cmdSave_Click"
        End If
        Resume ExitCode
    End Sub
    
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    On Error GoTo ErrorHandler
        Dim valid As Boolean
        Dim strCriteria As String
        
        valid = True
    
        If IsNull(txtActivityNumber) Or txtActivityNumber = "" Then
            txtActivityNumber.BorderColor = 255
            valid = False
        Else
            txtActivityNumber.BorderColor = 11050647
        End If
        If IsNull(txtActivityTitle) Or txtActivityTitle = "" Then
            txtActivityTitle.BorderColor = 255
            valid = False
        ElseIf InStr(txtActivityTitle, """") > 0 Or InStr(txtActivityTitle, ";") Then
            MsgBox "Please avoid "" or ; characters for database compatability.", , "Input Error"
            txtActivityTitle.BorderColor = 255
            valid = False
        Else
            txtActivityTitle.BorderColor = 11050647
        End If
        If IsNull(cboProjectLeader) Or cboProjectLeader = "" Then
            cboProjectLeader.BorderColor = 255
            valid = False
        Else
            cboProjectLeader.BorderColor = 11050647
        End If
        
        If valid = False Then
            lblError.Visible = True
            Cancel = True
        End If
      
    ExitCode:
        Exit Sub
    ErrorHandler:
        HandleError Err.number, Err.Description, Me.Name & "::Form_Before_Update"
        Resume ExitCode
    End Sub

    Comment

    • Ezzz
      New Member
      • Jan 2010
      • 28

      #3
      ChipR
      Thanks for the the above, but addapting it to my form is a bit beyond me?
      It also looks like you are coding different entries to your form and i want to code the one entry box [Serial Number] to display multiple error messages.

      I did however adapt part of it by adding an End If and then a new If Err statement see highlighted below:-
      Private Sub Command26_Click ()
      On Error GoTo Err_Command26_C lick


      DoCmd.DoMenuIte m acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

      Me.[Serial Number].SetFocus
      Me.Command26.Vi sible = False

      Exit_Command26_ Click:
      Exit Sub

      Err_Command26_C lick:

      If Err = 3022 Then
      MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"

      End If

      If Err = 3058 Then
      MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"


      Else
      ' Display the error number and the generic Access message.
      MsgBox "Error #: " & Err.Number & " " & Err.Description

      End If
      Resume Exit_Command26_ Click

      End Sub

      This almost solves my problem in that it now displayes the error messages i want it to but it also displayes the default error message for 3022 as well. Any ideas??
      Regards
      Ezzz

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        Please remember to use the code tags [code][/code] around your code. It will make it much easier to read.


        What you need is to use either a Select statement:
        Code:
        Select Case Err.Number
          Case 3022
            MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
        
          Case 3058 
            MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
           
          Case Else
             'Display the error number and the generic Access message.
             MsgBox "Error #: " & Err.Number & " " & Err.Description
        
        End Select
        Another option is to use the elseif statement
        Code:
        If Err.Number= 3022 Then
            MsgBox "This Serial Number already exsists: " & Search, , "Warning Duplicate Record!"
        
        ElseIf Err.Number = 3058 Then
            MsgBox "You Must Enter a Serial Number before you Save the Record: " & Search, , "Warning No Serial Number!"
           
        Else
             'Display the error number and the generic Access message.
             MsgBox "Error #: " & Err.Number & " " & Err.Description
        End If

        A more general suggestion to help yourself, is to give your buttons a sound name.
        Some people like to use for command buttons the prefix cmd (example cmd_Save or cmdSave), I myself prefix all my buttons with btn, btn_Save for example.

        Comment

        • Ezzz
          New Member
          • Jan 2010
          • 28

          #5
          Thats it the ElseIf statement works a treat many thanks.
          Ezzz.

          Comment

          Working...