Consulting

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,887
    Location
    Debug show vbObjectErrror is a Long

    ?vartype(vbObjectError) 3

    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.
    \


    BUt, .... if you're not in a class module then you probably want to forget about vbObjectError

    Required. Long integer that identifies the nature of the error. Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0–65535. The range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors.

    When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectErrorconstant. For example, to generate the error number 513, assign vbObjectError + 513 to the Number property.


    You could bit map if you didn't have too many errors


    Option Explicit
    
    
    Enum CustomErr
            UserCancel = 1
            UserPause = 2
            ProtectedWorkbook = 4
            CorruptWorkbook = 8
            ShellError = 16
    End Enum
    
    
    
    
    Enum ErrorSeverity
            Mild = 2 ^ 13
            Medium = 2 ^ 14
            Severe = 2 ^ 15
    End Enum
    
    
    
    
    Sub test()
        Dim sErr As String, sLvl As String
        On Error GoTo ErrHandle
    
    
    '    Err.Raise UserCancel + Mild
    '    Err.Raise UserCancel + Medium
    '    Err.Raise UserCancel + Severe
    
    
        Err.Raise ShellError + Medium
    
    
        
        Exit Sub
    
    
    
    
    ErrHandle:
        If (Err.Number And UserCancel) <> 0 Then
            sErr = "User Cancel"
        ElseIf (Err.Number And UserPause) <> 0 Then
            sErr = "User Pause"
        ElseIf (Err.Number And ProtectedWorkbook) <> 0 Then
            sErr = "Protected Workbook"
        ElseIf (Err.Number And CorruptWorkbook) <> 0 Then
            sErr = "Corrupt Workbook"
        ElseIf (Err.Number And ShellError) <> 0 Then
            sErr = "Shell Error"
        End If
    
    
    
    
        If (Err.Number And Mild) <> 0 Then
            sLvl = "Mild"
        ElseIf (Err.Number And Medium) <> 0 Then
            sLvl = "Medium"
        ElseIf (Err.Number And Severe) <> 0 Then
            sLvl = "Severe"
         End If
    
    
        
        MsgBox "Error was " & sErr & " with severity of " & sLvl
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  2. #2
    Quote Originally Posted by Paul_Hossler View Post
    Debug show vbObjectErrror is a Long
    So, not unsigned?

    "The values are not signed Longs, so none of this is about "negative" anything. These values are unsigned and 32 bits long (DWORD)."
    https://www.vbforums.com/showthread....=1#post5538131
    BUt, .... if you're not in a class module then you probably want to forget about vbObjectError
    Question: Is there any drawback to using vbObjectError in a module?

    News flash: My "Custom" source method actually does work. As long as there's no harm in re-using native error numbers, i CAN disambiguate with .Source. Just have to start the Enum at 1, not 0. Which means, my custom errors seem to work fine without using vbObjectError. Am i missing anything?

    So now i have to choose:
    - i can re-use native error numbers, and disambiguate by setting .Source to "Custom"
    - or, avoid native numbers by starting my enum at vbObjectError

    You could bit map...

    ...
            Medium = 2 ^ 14
            Severe = 2 ^ 15
    ...
            CorruptWorkbook = 8
            ShellError = 16
    ...
        On Error Goto ErrHandle    ' DID YOU FORGET THIS?
    ...
        If (Err.Number And UserCancel) <> 0 Then
            sErr = "User Cancel"
    ...
        If (Err.Number And Mild) <> 0 Then
            sLvl = "Mild"
    ...
    wOW, niCE woRK!
    Stealing

    ... if you didn't have too many errors
    How many?

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    I'm going by the MS documentation:


    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.



    Long
    integer that identifies the nature of the error. Visual Basic errors (both Visual Basic-defined and user-defined errors) are in the range 0–65535. The range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors.

    When setting the Number property to your own error code in a class module, you add your error code number to the vbObjectErrorconstant.



    How many?
    65535 = 2^16-1

    I used the 3 high order bits to mark severity, so that leaves 12 or 13 error messages



    On Error Goto ErrHandle ' DID YOU FORGET THIS?
    Second line
    ---------------------------------------------------------------------------------------------------------------------

    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

  4. #4
    Quote Originally Posted by Paul_Hossler View Post
    65535 = 2^16-1
    I used the 3 high order bits to mark severity, so that leaves 12 or 13 error messages
    I would say that's not a useful solution for this application, but still useful for other things.

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
  •