Results 1 to 20 of 34

Thread: vbObjectError Not Working as Expected

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,739
    Location
    Did you have any error handling specified (like in examples below)?


    Post #2 at

    https://www.vbforums.com/showthread....what-is-it-for

    has good writeup. Old but still valid as far as I know

    It seems the intent by MS was to not let things get confusing

    Because Microsoft has reserved the error numbers from 1 to 2147221504 for their use and the reason why you add 2147221504=vbObjectError to what ever error number you want to raise is because things don't get confusing. Lets say you have X number of error traps within your code and you have numbered them from 1 to X without the addition of the vbObjectError constant. Then you die for some reason or are no longer working at the place you designed this code and your customer recieves error number 11 with your description but when your customer looks this error number up on MS's web site it will come back as a division by zero error.

    Option Explicit
    
    
    'https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/number-property-visual-basic-for-applications
    
    
    Sub ForceError()
        Dim x As Long, y As Long
        Dim o As clsDivide
        
        Set o = New clsDivide
        
        x = Application.InputBox("Enter X as numerator")
        y = Application.InputBox("Enter Y as denominator")
        
        On Error GoTo ErrHandler
        
        MsgBox o.DivideXY(x, y)
        
        On Error GoTo 0
    
    
    
    
        Exit Sub
    
    
    ErrHandler:
    
        ' My error
        If (Err.Number - vbObjectError) = 515 Then
            MsgBox (Err.Number - vbObjectError) & " -- " & Err.Description, vbCritical, Err.Source
         
        'another error  
        Else
            MsgBox Err.Number & " -- " & Err.Description, vbCritical, Err.Source
        End If
    
    
    
    End Sub
    
    
    Sub ForceError1()
        Dim x As Long, y As Long
        
        x = Application.InputBox("Enter X as numerator")
        y = Application.InputBox("Enter Y as denominator")
        
        On Error GoTo ErrHandler
        
        If y = 0 Then Err.Raise 515, "Force Error 1", "Can't divide by zero"
        
        On Error GoTo 0
    
    
    
    
        Exit Sub
    
    
    ErrHandler:
        
        MsgBox (Err.Number) & " -- " & Err.Description, vbCritical, Err.Source
    
    
    End Sub


    Class clsDivideXY

    Option Explicit
    
    
    Private x As Long, y As Long
    
    
    Function DivideXY(x As Long, y As Long) As Long
    
    
        If y = 0 Then Err.Raise vbObjectError + 515, "DivideXY", "Can't divide by zero"
    
    
        DivideXY = x / y
    
    
    End Function
    My thoughts, and you can experiment with the attachment, but I don't think it really makes a lot of difference
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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