Making error codes/ messages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SusanK4305
    New Member
    • Sep 2010
    • 88

    Making error codes/ messages

    Hi i was reading one of ya'lls thread and thought I might be able to use the first code to do what I need. However, I need it to get the user to click the save command button (i.e. Command165). Once all Errs are corrected then that same save button will save the record. These are the 2 codes I have but I am missing somthing.I don't know much about coding so if you could brake it down for me pleas.


    Code:
    Private Sub Command165_Click()
    On Error GoTo Err_Command165_Click
    
    
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    
    Exit_Command165_Click:
    Exit Sub
    
    Err_Command165_Click:
    MsgBox Err.Description
    Resume Exit_Command165_Click
    
    End Sub
    
    __________________________________________________ ___
    Private Sub Command165_Click()
    Dim strError
    strError = ""
    
    If IsNull(Social_Security_Number.Value) Then strError = strError & vbNewLine & Social_Security_Number
    If IsNull(LastName.Value) Then strError = strError & vbNewLine & LastName
    If IsNull(FirstName.Value) Then strError = strError & vbNewLine & FirstName
    If Len(strError) <> 0 Then
    'Errors encountered
    strError = "The following fields are blank:" & vbNewLine & _
    strError & vbNewLine _
    "Please fill in these fields before submitting"
    End If
    End Sub
    Last edited by Banfa; Sep 16 '10, 11:30 AM. Reason: Added [code]...[/code] tags round the code
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    You can use the Err.Raise statement. (Actually, it's a subroutine call, but who's being picky).

    You can find full details in the documentation. Here's an example from some of my code:
    Code:
        Err.Raise (0 + VBA.Constants.vbObjectError), moduleName, ("Error: No default for field " & fieldName)
    Luck!

    Comment

    • SusanK4305
      New Member
      • Sep 2010
      • 88

      #3
      I don't understand. Like I said I am new to codes. Where do I put this and what does it mean?

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Put it, or something like it wherever you've discovered an error that your code can't deal with.

        What it does is raise an error when a problem is detected.

        Then, callers can catch that error with an On Error statement.

        Does that make sense?

        Comment

        • SusanK4305
          New Member
          • Sep 2010
          • 88

          #5
          I think so. I will try it. Thank you

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            If they don't use On Error, then the message is displayed at the application level. But that's the application programmers' responsibility. Your library is reporting the error in a responsible way.

            Luck!

            Comment

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

              #7
              A couple of things:
              1) This is an access question and you should post it there. (Access VBA also belongs in that forum)
              2) You should ALWAYS name your buttons, before creating code for them. A good name could be cmd_Save (cmd short for command button) or btn_Save (what I personally use)
              3) You should use the code tags arond your code:[code]Code goes here[/code] to increase the readability of it, thats what I did below.
              4) You post 2 pieces of code with the same procedure name. That is bound to give errors in your application.
              5) When doing string concatanation, you must enclose your string literals in double quotes "my string", whereas references to string VARIABLES should not have quotes around them.

              Now, to the question at hand. How to inform the user that he is missing some information. Lets presume we have named the button btn_Save, then it could look something like this:

              Code:
              Private Sub btn_Save_Click()
              'Check that required info is provided and if so, save record
              On Error GoTo Err_btn_Save_Click
              
              'Check info
                  Dim strError as string
                  strError=""
                  If IsNull(Social_Security_Number.Value) Then strError = strError & vbNewLine & "Social Security Number"
                  If IsNull(FirstName.Value) Then strError = strError & vbNewLine & "FirstName"  
                  If IsNull(LastName.Value) Then strError = strError & vbNewLine & LastName
              
              'If errors were found, inform user and exit.
                  If strError & ""="" then
                      Msgbox "Please fill in these fields before submitting:" & strError,vbOkOnly,"Missing Info"
                      GoTo Exit_btn_Save_Click
                  End If
              
              'Save the record
                  DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
              
              Exit_btn_Save_Click:
                  Exit Sub
              
              Err_btn_Save_Click:
                  MsgBox Err.Description
                  Resume Exit_Command165_Click
              
              End Sub

              Comment

              Working...