Page 1 of 2 12 LastLast
Results 1 to 40 of 66

Thread: vbObjectError: what is it for?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    195

    vbObjectError: what is it for?

    Hello everybody. I hear that you should or your own error messages with vbObjectError when generated within a class module. But I don't understand why. If I write a dll raising an error and the client uses it, she should be able to find out the line that generated the error, so tracing it down to my dll. For what reason should I add a constant to make it clear that the error comes from a class module? The client would find out that this is the case all the same ...

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: vbObjectError: what is it for?

    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. Now you have MSGOD pissed at you and the customer because this error number and your description do not match. Now, if you had previously left your place of employment on good terms and had been previously getting good references from them, it could all change when this customer calls up your old company very angry complaing about this program with the incorrect information. Then your old boss would have to reassign someone from their normal duties to look through the meager documentation you left behind and who knows how many man hours would be waisted on tracing the error down and what exactly it means. Then when you get that call from the dream job you have always wanted that says your interview has been canceled, you will know why and no, this is not from personal experience. Just a possible outcome...
    Option Explicit should not be an Option!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    195

    Re: vbObjectError: what is it for?

    I'm still confused...
    1) vbObjectError is negative, -2147221504.
    2) according to MSDN, Microsoft error numbers are in the range 1-512, and vbObjectError+1-vbObjectError+512, so in principle if I raise error #20000 (without vbObjectError), there would be no clashes.
    3) they say one should use vbObjectError for errors raised from within class modules. What if I wanna raise an error from within a standard module?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: vbObjectError: what is it for?

    A reason to use vbObjectError is to classify the error as an "Automation Error" (a DLL/Component is an Automation Server), rather than an "Application Defined" error.

    Run these statements and note the different wording of the message

    err.raise 20000
    err.raise 20000 + vbobjecterror

    But really it is just a matter of choice. If you want to raise error 20000, then raise it. The important part of an error message from a system is the Source, Description and documentation.

  5. #5
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: vbObjectError: what is it for?

    Just for giggles....

    And yes this will take a long time to run...
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
    On Error GoTo Form_LoadError
    
    Dim FName1 As String, FName2 As String
    Dim FNumb1 As Integer, FNumb2 As Integer
    Dim ForLoopCounter As Long, OutPutString As String
    
    Me.Show
    
    FNumb1 = FreeFile
    FName1 = "C:\z\Predefined Errors.txt"
    Open FName1 For Output As #FNumb1
    
    FName2 = "C:\z\Undefined Errors.txt"
    FNumb2 = FreeFile
    Open FName2 For Output As #FNumb2
    
    For ForLoopCounter = vbObjectError To (vbObjectError * -1)
      Err.Raise ForLoopCounter
      Me.Caption = ForLoopCounter
      DoEvents
    Next ForLoopCounter
    
    Close #FNumb1
    Close #FNumb2
    
    Exit Sub
    Form_LoadError:
    
    If Err.Description <> "Application-defined or object-defined error" _
      And Err.Description <> "Automation error" Then
      
      OutPutString = Err.Description
      OutPutString = Replace(OutPutString, vbNewLine, " ")
      OutPutString = Replace(OutPutString, vbCr, " ")
      OutPutString = Replace(OutPutString, vbLf, " ")
      Print #FNumb1, Format(Err.Number, "000000000000000") & " : " & OutPutString
      
    Else
      
      OutPutString = Err.Description
      OutPutString = Replace(OutPutString, vbNewLine, " ")
      OutPutString = Replace(OutPutString, vbCr, " ")
      OutPutString = Replace(OutPutString, vbLf, " ")
      Print #FNumb2, Format(Err.Number, "000000000000000") & " : " & OutPutString
      
    End If
    
    Resume Next
    
    End Sub
    and you will see just how many error codes have already been predifened by MS and just how many are available for Automation Errors or Application-defined or object-defined errors.

    Good Luck
    Option Explicit should not be an Option!

  6. #6
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by brucevde View Post
    A reason to use vbObjectError is to classify the error as an "Automation Error" (a DLL/Component is an Automation Server), rather than an "Application Defined" error.

    Run these statements and note the different wording of the message

    err.raise 20000
    err.raise 20000 + vbobjecterror
    For me:
    err.raise 20000: "Application-defined or object-defined error" - That sounds like a custom, non-native error.
    err.raise 20000 + vbobjecterror: "Automation error" - That sounds like a native VB error.

    But it seems you're saying the opposite.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    Also see the ancient article Q129941: PRB:Unexpected Results When Raise Method Propogates OLE Errors. There are several other similar articles.

    Basically &H80040000 through &H80040200 are reserved, don't use them. There are also case where the reserved range includes &H80040201 through &H800403FF, so &H80040400 really seems to be the base value to use for your own exceptions.

    I think there are even other reserved ranges in different scenarios, so I always begin at a base of &H80044700 myself.

    These are not "negative numbers" but unsigned values. By rights you should not be doing ay VB6 Long arithmetic on them at all. Instead use Or and not +, as in vbObjectError Or &H4700& for example.

    Most of the relevant MS KB articles are dated 1999 to 2001 as problems kept arising from users of VB5 and VB6 who never read the rules of the road.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    Here's another one, Q194418: PRB: CallByName Fails to Return the Correct Error Information.

    As I said, Microsoft tried repeatedly to get this message across.

  9. #9
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    "Microsoft has reserved the error numbers from 1 to 2147221504 for their use"
    "The range 513 - 65535 is available for user defined errors"
    Those two statements seem contradictory.


    "the reason why you add 2147221504=vbObjectError to what ever error number you want to raise is because things don't get confusing. "
    You say vbObjectError = 2147221504. VBA says vbObjectError = -2147221504


    "you should OR your own error messages with vbObjectError when generated within a class module."
    Three points of confusion: Do i add, or OR? Do i use vbObjectError ONLY when generated within a class module? What about regular modules?


    dilettante wrote:
    Basically &H80040000 through &H80040200 are reserved, don't use them. There are also case where the reserved range includes &H80040201 through &H800403FF, so &H80040400 really seems to be the base value to use for your own exceptions. I think there are even other reserved ranges in different scenarios, so I always begin at a base of &H80044700 myself.
    Ok, so dilettante agrees that Microsoft isn't clear about WHICH ranges are reserved. Unclear whether to use decimal or hex, whether the values are negative or unsigned, and whether to use OR or +. dilettante's recommendations seem to contradict the Microsoft documentation.

    For a moment, let's mentally truncate the "-21472" prefix on all numbers, so we can more easily read and compare the numbers. dilettante's "top" of range as int (&H80040200 = -----20992) is smaller than his "bottom" of range (&H80040000 = -----21504), suggesting we are talking about negative numbers. The number he recommends, &H80044700, is even lower than that, -----03328. -----03328 is seventeen-thousand numbers away from the closest known native error (according to dilettante). Why such a vast safety-margin? And what then is the available range? -----03328 to what? to 0? to 65535?


    I'm no less confused.
    Last edited by johnywhy; Sep 29th, 2021 at 02:31 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    The values are not signed Longs, so none of this is about "negative" anything. These values are unsigned and 32 bits long (DWORD).

    See: HRESULT

  11. #11
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by dilettante View Post
    The values are not signed Longs, so none of this is about "negative" anything.
    So in unsigned values, 2,147,220,992 is larger than 2,147,221,504 ?

    Your HRESULT link doesn't mention "unsigned", so that doesn't make it clearer to me.

    This vid says unsigned numbers are all positive, and signed numbers use the last bit as the sign bit. Ok, that seems clear. So why do all the numbers above start with a `-`? Why are we saying that 992 is larger than 1,504?

    This MS page says vbObjectError is a negative number, not unsigned
    "the constant vbObjectError is a very large negative number"
    https://docs.microsoft.com/en-us/off...r-applications

    Still no idea what the usable range is.
    Last edited by johnywhy; Sep 29th, 2021 at 03:02 PM.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    VB6 doesn't have unsigned integers, except for the special case of Byte. So of course the constant vbObjectError is Long, it was all they had to work with. Surprise, surprise this has never been a problem.

  13. #13
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by dilettante View Post
    VB6 doesn't have unsigned integers, except for the special case of Byte. So of course the constant vbObjectError is Long, it was all they had to work with. Surprise, surprise this has never been a problem.
    Ok.... so it's not unsigned.

    Still no idea what the available numbers are

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    Of course it is unsigned. That's how we represent a DWORD in VB6, by stuffing it into a Long.

    You seem to be confusing the interpretation of a bit sequence with the sequence itself. I'm not sure you are ever going to catch that ball.

    I can't wait to see the hilarity that ensues if you ever have to deal with naked pointers.

  15. #15
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    sounds like you're saying that we can think of it like an unsigned, even tho it's stored as a long. Ok fine. This doesn't get us closer to answers on the unanswered questions.

    Quote Originally Posted by dilettante View Post
    I can't wait to see the hilarity that ensues if you ever have to deal with naked pointers.
    are you here for laughs and to insult people, or to help out?

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: vbObjectError: what is it for?

    You gravedug and hijacked an old thread. We've given you tons of information. More than you need in fact. You came back with a lot of mocking.

  17. #17
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by dilettante View Post
    You gravedug and hijacked an old thread. We've given you tons of information. More than you need in fact. You came back with a lot of mocking.
    If you think this thread has no relevance to VBA today, or if you think the question was already answered, then thanks for your input, maybe it will help.

    We still haven't an answer the unanswered questions, particularly: "What is the available range for custom errors?"

    i never "mocked" anyone, sorry if you thought so.
    Last edited by johnywhy; Sep 29th, 2021 at 09:27 PM.

  18. #18
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by vb5prgrmr View Post
    Just for giggles....And yes this will take a long time to run...
    Here's an alternate way to list all the errors. This prints to sheet instead of external file. Not sure if it's faster or slower than yours. Since it seems most of these number aren't native errors, this seems a very inefficient way to check the numbers.

    Update: Modified, so it only logs an error if it's different than the previous error, so it runs much faster, and only lists the unique errors.

    When it hits the end of a column, it restarts at the next column.

    Code:
    Sub ListErrs()
              Dim lRow As Long, lCol As Long, lErr As Long, sError As String
              lCol = 1
              For lErr = vbObjectError To (-1 * vbObjectError)
                        Application.StatusBar = lErr
                        On Error Resume Next
                        Err.Raise lErr
                        If (Error <> sError) Then
                                  lRow = lRow + 1
                                  If lRow > 1048576 Then
                                            lRow = 1
                                            lCol = lCol + 1
                                  End If
                                  sError = Error
                                  Cells(lRow, lCol) = lErr & " : " & sError
                                  Windows(1).ScrollRow = lRow
                        End If
                        On Error GoTo 0
                        DoEvents
              Next lErr
    End Sub

    This one let's you check any number, one at a time.
    Code:
    Sub spotcheck()
              Dim dNum As Double, sInput As String
              On Error Resume Next
              Do
                        sInput = InputBox("Err?", "Errors", sInput)
                        If (sInput = "") Then Exit Do
                        
                        dNum = 0
                        dNum = CDbl(sInput)
                        Err.Raise dNum
                        MsgBox dNum & vbNewLine & Error
                        Err.Clear
              Loop
              On Error GoTo 0
    End Sub
    Last edited by johnywhy; Sep 30th, 2021 at 05:40 AM.

  19. #19
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: vbObjectError: what is it for?

    Quote Originally Posted by johnywhy View Post
    Three points of confusion: Do i add, or OR?
    Doesn't matter because vbObjectError has lowest 16 bits cleared so if your "custom error code" is within 0-65535 you are safe to OR. I personally use vbObjectError + MyCustomCode but using Or is not wrong.

    Quote Originally Posted by johnywhy View Post
    Do i use vbObjectError ONLY when generated within a class module? What about regular modules?
    Doesn't matter as routines (Subs/Functions) in a standard modules actually return HRESULTs too. Err.Raise MyNumber on a routine with no custom (local) error handler is equivalent to return MyNumber in C/C++.

    Quote Originally Posted by johnywhy View Post
    Ok, so dilettante agrees that Microsoft isn't clear about WHICH ranges are reserved. Unclear whether to use decimal or hex, whether the values are negative or unsigned, and whether to use OR or +. dilettante's recommendations seem to contradict the Microsoft documentation.
    There are no reserved ranges per se -- it's all by convention. I personally use vbObjectError + 0 (i.e. don't offset) to signify any error which is particularly explained in the error description. I don't expect anyone to trap the error and meaningfully branch on error number but just to log the error description and return failure on it's turn upstream.

    The numbers/literals are in hex. When you see something like 8004000 this is in hex and the first (highest) bit is set which is assigned to mean 1 = Error, 0 = Success.

    By conincidence this highest bit for signed integers (As Long in VB6) is assigned for the sign to mean 1 = Negative, 0 = Positive number and that is why in VB6 you often see something like If hResult < 0 Then GoTo HandleError.

    Please don't use decimal literals with HRESULT like 2,147,221,504 -- this is ridiculous and actually hides everything! Anyone with little understanding of HRESULT can glance the hex literal and figure out if it's an error, what the facility code is and what the more detailed error code is.

    You can use certutil.exe on the command prompt to get error description like this

    Code:
    C:\Users\wqw>certutil.exe /error -2147221504
    0x80040000 (-2147221504 OLE_E_OLEVERB) -- 2147745792 (-2147221504)
    Error message text: Invalid OLEVERB structure
    CertUtil: -error command completed successfully.
    This works with hex and decimal (negative numbers too).

    cheers,
    </wqw>

  20. #20
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    It will take me some time to digest your reply.

    Meantime, I ran the proc above for EVERY number from -2 million to +1 million, and for many (not all) numbers between -2 billion (vbObjectErr) and -2 million. I didn't run for all numbers between -2 billion (vbObjectErr) and -2 million, because that would take about a week (maybe someone out there can make it faster).

    Here's what i found. These results seem to disagree with the info on bettersolutions.

    -2147221504 Automation error
    -2147221230 ActiveX component can't create object
    -2147221229 Automation error
    -2147221164 ActiveX component can't create object
    -2147221163 Automation error
    -2147221021 ActiveX component can't create object
    -2147221020 Automation error
    -2147221018 File name or class name not found during Automation operation
    -2147221017 Automation error
    -2147221014 File name or class name not found during Automation operation
    -2147221013 Automation error
    -2147221006 Automation error
    -2147221005 ActiveX component can't create object
    -2147221004 Automation error
    -2147221003 ActiveX component can't create object
    -2147221002 Automation error
    -2147220994 ActiveX component can't create object
    -2147220993 Automation error
    -2000000 Automation error
    0 Invalid procedure call or argument
    1 Application-defined or object-defined error
    3 Return without GoSub
    4 Application-defined or object-defined error
    5 Invalid procedure call or argument
    6 Overflow
    7 Out of memory
    8 Application-defined or object-defined error
    9 Subscript out of range
    10 This array is fixed or temporarily locked
    11 Division by zero
    12 Application-defined or object-defined error
    13 Type mismatch
    14 Out of string space
    15 Application-defined or object-defined error
    16 Expression too complex
    17 Can't perform requested operation
    18 User interrupt occurred
    19 Application-defined or object-defined error
    20 Resume without error
    21 Application-defined or object-defined error
    28 Out of stack space
    29 Application-defined or object-defined error
    35 Sub or Function not defined
    36 Application-defined or object-defined error
    47 Too many DLL application clients
    48 Error in loading DLL
    49 Bad DLL calling convention
    50 Application-defined or object-defined error
    51 Internal error
    52 Bad file name or number
    53 File not found
    54 Bad file mode
    55 File already open
    56 Application-defined or object-defined error
    57 Device I/O error
    58 File already exists
    59 Bad record length
    60 Application-defined or object-defined error
    61 Disk full
    62 Input past end of file
    63 Bad record number
    64 Application-defined or object-defined error
    67 Too many files
    68 Device unavailable
    69 Application-defined or object-defined error
    70 Permission denied
    71 Disk not ready
    72 Application-defined or object-defined error
    74 Can't rename with different drive
    75 Path/File access error
    76 Path not found
    77 Application-defined or object-defined error
    91 Object variable or With block variable not set
    92 For loop not initialized
    93 Invalid pattern string
    94 Invalid use of Null
    95 Application-defined or object-defined error
    96 Unable to sink events of object because the object is already firing events to the maximum number of event receivers that it supports
    97 Can not call friend function on object which is not an instance of defining class
    98 A property or method call cannot include a reference to a private object, either as an argument or as a return value
    99 Application-defined or object-defined error
    321 Invalid file format
    322 Can't create necessary temporary file
    323 Application-defined or object-defined error
    325 Invalid format in resource file
    326 Application-defined or object-defined error
    380 Invalid property value
    381 Invalid property array index
    382 Set not supported at runtime
    383 Set not supported (read-only property)
    384 Application-defined or object-defined error
    385 Need property array index
    386 Application-defined or object-defined error
    387 Set not permitted
    388 Application-defined or object-defined error
    393 Get not supported at runtime
    394 Get not supported (write-only property)
    395 Application-defined or object-defined error
    422 Property not found
    423 Property or method not found
    424 Object required
    425 Application-defined or object-defined error
    429 ActiveX component can't create object
    430 Class does not support Automation or does not support expected interface
    431 Application-defined or object-defined error
    432 File name or class name not found during Automation operation
    433 Application-defined or object-defined error
    438 Object doesn't support this property or method
    439 Application-defined or object-defined error
    440 Automation error
    441 Application-defined or object-defined error
    442 Connection to type library or object library for remote process has been lost. Press OK for dialog to remove reference.
    443 Automation object does not have a default value
    444 Application-defined or object-defined error
    445 Object doesn't support this action
    446 Object doesn't support named arguments
    447 Object doesn't support current locale setting
    448 Named argument not found
    449 Argument not optional
    450 Wrong number of arguments or invalid property assignment
    451 Property let procedure not defined and property get procedure did not return an object
    452 Invalid ordinal
    453 Specified DLL function not found
    454 Code resource not found
    455 Code resource lock error
    456 Application-defined or object-defined error
    457 This key is already associated with an element of this collection
    458 Variable uses an Automation type not supported in Visual Basic
    459 Object or class does not support the set of events
    460 Invalid clipboard format
    461 Method or data member not found
    462 The remote server machine does not exist or is unavailable
    463 Class not registered on local machine
    464 Application-defined or object-defined error
    481 Invalid picture
    482 Printer error
    483 Application-defined or object-defined error
    735 Can't save file to TEMP
    736 Application-defined or object-defined error
    744 Search text not found
    745 Application-defined or object-defined error
    746 Replacements too long
    747 Application-defined or object-defined error
    65536 Invalid procedure call or argument

  21. #21
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    What in God's name are you looking for?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  22. #22
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by Niya View Post
    What in God's name are you looking for?
    i've explained in detail. You'll have to be more specific.

  23. #23
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    Never mind. Dilettante and wqweto already answered your question but you just didn't understand.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  24. #24
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by Niya View Post
    Never mind. Dilettante and wqweto already answered your question but you just didn't understand.
    That's certainly possible. I've gotten seeming conflicting info from other sources. I'm still trying to understand this.

    If you'd like to offer some additional insight, great! But you just seem to be trolling.

  25. #25
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: vbObjectError: what is it for?

    Yeah, there seems to be a fair amount of that in this thread. I'm inclined to say that it should be abandoned, and a new thread started with a different subject line. It sounds like the question you are at, by now, is not "what is vbObjectError for". Personally, I found the thread kind of informative, though it's information I'll never take advantage of.
    My usual boring signature: Nothing

  26. #26
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: vbObjectError: what is it for?

    Quote Originally Posted by johnywhy View Post
    Ok.... so it's not unsigned.

    Still no idea what the available numbers are
    Try these API in the debugger:
    Code:
    Private Const ERROR_SUCCESS As Long = 0
    Private Const ERROR_FAILURE As Long = 40036
    Private Declare Function apiEbGetErrorInfo Lib "vba6.dll" Alias "EbGetErrorInfo" (ByVal errNumber As Long, ByVal u2 As Long) As Long
    Private Declare Function apiEbShowError Lib "vba6.dll" Alias "EbShowError" (ByVal errFail As Long) As Long
    Use EbGetErrorInfo to check error numbers, waiting for the return value of 0 for success. Pass error number or ERROR_FAILURE to EbShowError.
    The list I see goes up to 63036, and then repeats.

    Maybe you want to avoid using similar numbers?
    999 Stop statement encountered
    1260 No timer available
    1282 No foreign application responded to a DDE initiate
    1285 Foreign application won't perform DDE method or operation
    1286 Timeout while waiting for DDE response
    1287 User pressed Escape key during DDE operation
    1288 Destination is busy
    1290 Data in wrong format
    1293 DDE Method invoked with no channel open
    1294 Invalid DDE Link format
    1295 Message queue filled; DDE message lost
    1296 PasteLink already performed on this control
    1297 Can't set LinkMode; invalid LinkTopic
    1298 System DLL '1' could not be loaded
    1320 Can't use character device names in file names: '1'
    1321 Invalid file format
    1325 '1' is not a valid resource file
    1326 Resource with identifier '1' not found
    1327 Data value named '1' not found
    1328 Illegal parameter. Can't write arrays
    1329 Illegal parameter. Can't write user-defined type.
    1330 Illegal parameter. Can't write object because it does not support persistence.
    1331 Invalid property name.
    1335 Could not access system registry
    1336 Component not correctly registered
    1337 Component not found in registered location
    1338 Component could not successfully create requested object
    1339 Component '1' or one of its dependencies not correctly registered: a file is missing or invalid
    1340 Control array element '1' doesn't exist
    1341 Invalid control array index
    1342 Not enough room to allocate control array '1'
    1343 Object not an array
    1344 Must specify index for object array
    1345 Reached limit: cannot create any more controls for this form
    1360 Object already loaded
    ...
    58198 Invalid outside Enum
    58199 Could not execute '~'
    59200 Duplicate resource with the same type and name
    59201 Can't find DLL entry point ~ in ~

  27. #27
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by TTn View Post
    Try these API in the debugger:...
    Thx for that, @TTn. I'll give that a whirl.

  28. #28
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by TTn View Post
    Try these API in the debugger:
    Code:
    Private Const ERROR_SUCCESS As Long = 0
    Private Const ERROR_FAILURE As Long = 40036
    Private Declare Function apiEbGetErrorInfo Lib "vba6.dll" Alias "EbGetErrorInfo" (ByVal errNumber As Long, ByVal u2 As Long) As Long
    Private Declare Function apiEbShowError Lib "vba6.dll" Alias "EbShowError" (ByVal errFail As Long) As Long
    Use EbGetErrorInfo to check error numbers, waiting for the return value of 0 for success. Pass error number or ERROR_FAILURE to EbShowError.
    Why did you recommend these API? Are they related to vbObjectError?
    The list I see goes up to 63036, and then repeats.
    Starting at 0?

    Maybe you want to avoid using similar numbers?
    These are Excel native error numbers? i thought Excel error numbers go into the negatives.

    Yep, i'm still totally confused. I pointed out that there are native errors above vbObjectError, so if i add vbObjectError to my custom numbers, i'll bump into the native errors. If someone posted a solution to that, i didn't understand.

  29. #29
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Here's a proposed solution, which i THINK might resolve this.

    This solution doesn't CARE if my custom error numbers overlap native error numbers. It enables me to have a custom error-numbering strategy without fear of conflicting with native errors (or errors generated by other workbooks or applications.)

    The solution exploits the .Source property of Err.

    Table on worksheet called "tblCustomErrors"
    Code:
    ErrNum	ErrDescription
    11	User cancelled the process
    53	User clicked pause
    Code:
    Sub Test_Handler()
              On Error GoTo HANDLER
              
              Debug.Print 1 / 0            ' 11
              Kill "C:\Doesn't-Exist.txt"         ' 53
              Err.Raise 11, "Custom"
              Err.Raise 53, "Custom"
              
              Exit Sub
    HANDLER:
              GlobalHandler
              Resume Next
    End Sub
    
    
    Sub GlobalHandler()
              Dim sDesc As String
              
              Select Case Err.Source
                        Case "Custom"
                                  ' get description from worksheet
                                  sDesc = WorksheetFunction.VLookup(Err.Number, [tblCustomErrors], 2, False)
                        Case Else
                                  sDesc = Err.Description
              End Select
              
              MsgBox sDesc
    End Sub
    Who knows, this is might be how .Source was intended to be used.

  30. #30
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: vbObjectError: what is it for?

    After all the confusion stages you finally come up with some plausable model of reality that HRESULT errors and ranges are all designated by convention.

    There is nothing that prevents you from "reusing" an error from some obscure system library (like OS provided BCrypt DLL) for your own purposes.

    You can do the same with VB's runtime errors too, provided that you can disambiguate the error numbers (in your case on .Source property).

    cheers,
    </wqw>

  31. #31
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by wqweto View Post
    After all the confusion stages you finally come up with some plausable model of reality that HRESULT errors and ranges are all designated by convention. There is nothing that prevents you from "reusing" an error from some obscure system library (like OS provided BCrypt DLL) for your own purposes.You can do the same with VB's runtime errors too, provided that you can disambiguate the error numbers (in your case on .Source property).
    I think you mean, my solution works. Correct?

  32. #32
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Re-using Native Numbers

    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?

    vbObjectError

    Starting my numbering at vbObjectError also works. In that case, i don't have to bother with setting .Source="Custom".

    Code:
    Enum CustomError
              UserCancel = vbObjectError
              UserPause
              ProtectedWorkbook
              CorruptWorkbook
              ShellError
    End Enum
    Two Options

    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

    Number Type

    Since vbObjectError = -2147221504 , that means my numbering will start at -2147221504 and go up from there. No, i'm wrong, it's not a negative number :/

    Quote Originally Posted by dilettante View Post
    These are not "negative numbers" but unsigned values. By rights you should not be doing ay VB6 Long arithmetic on them at all. Instead use Or and not +, as in vbObjectError Or &H4700& for example.
    Does my enum above violate your recommendation? I'm not adding anything, and using "=" for the first value. But letting the enum increment the other values. @dilettante, does letting the enum increment the other values violate your recommendation?

    If it's unsigned, why is a negative symbol shown?
    Code:
    ?vbObjectError
    -2147221504
    Last edited by johnywhy; Dec 7th, 2021 at 11:27 PM.

  33. #33
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    News flash: My "Custom" source method actually does work. Just have to start the Enum at 1, not 0. Details and questions in previous reply.

  34. #34
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    Quote Originally Posted by johnywhy View Post
    If it's unsigned, why is a negative symbol shown?
    You're still don't not getting it. At this point it I think it would be prudent to ask if you have any understanding of how computers generally represent numbers. So do you?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  35. #35
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by Niya View Post
    You're still don't not getting it. At this point it I think it would be prudent to ask if you have any understanding of how computers generally represent numbers. So do you?
    "Represent"? You mean display? Or store?

  36. #36
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    Ok if I showed you a piece of code like this:-
    Code:
    Dim I As Integer = 200
    Do you have an understanding of how the computer represents that value of 200 internally?

    Or what if I showed you this:-
    Code:
    Dim I As Integer = &HFF00
    Do you understand what is happening under the hood?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  37. #37
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by Niya View Post
    Ok if I showed you a piece of code like this:-
    Code:
    Dim I As Integer = 200
    If you showed me a piece of code like that, i would say you're going to get a compile error. You can't declare variables like that in VB. Maybe you're thinking of VB.NET? (this is the "VB6 and Earlier" forum)

    In VB, it would be:

    Code:
    Dim I As Integer
    I = 200
    The first line of that code, `Dim I As Integer`, will reserve space in RAM (memory) to store whatever number you later set the variable `I` to.

    Computers store everything as binary (base two, ie 1's and 0's). Integer datatype supports the range -32,768 to +32,768. That's 65,536 total numbers, which requires 16 bits of storage (two 8-bit words). For integers, the left-most MSB (most significant bit) is used to represent sign. I've read 0 means positive and 1 means negative, but not sure. (Also, just learned about two's complement -- if the computer is storing as two's complement, then the MSB indicates negative-MSB, not negative.)

    Do you have an understanding of how the computer represents that value of 200 internally?
    In binary, 200 is 11001000, which fits into 8 bits (one byte). As i mentioned above, i think the left-most bit will be set to 0 to represent positive. Since the computer reserved two bytes, 0 will be stored in the unused bits:
    Code:
    0000000011001000
    ^
    +
    Or what if I showed you this:-
    Code:
    Dim I As Integer 
    I = &HFF00
    Do you understand what is happening under the hood?
    "&HFF00" is how a hex number is notated in VB. "&H" is just a VB prefix that tells VB "this is a hex number". So the hex part is just "FF00". You're setting I equal to a hex value. The computer must convert that to binary in order to store it.

    I'm not fluent in hex, but Excel tells me FF00 is 65280
    Code:
    ?[HEX2DEC("FF00")]
     65280
    65,280 is a Long, not Integer. It's too large to fit into an Integer variable, but your assignment doesn't raise an overflow error, as the following will:
    Code:
    Dim I As Integer 
    I = 65280
    So i'm guessing that VB figures out it has to convert FF00 to an integer. VB tells me FF00, when converted to Integer, is -256
    Code:
    ?CInt(&HFF00)
    -256
    On the worksheet,

    =DEC2BIN(-256)
    returns
    1100000000

    If we assume the left-most bit indicates negative, then the remaining bits should equal 256. That seems correct:
    =BIN2DEC("1100000000")
    returns
    -256
    and
    =BIN2DEC("100000000")
    returns
    256

    Therefor, the MSB 1 must indicate sign
    1100000000
    ^
    -

    But your Dim as Integer reserves more bits than that, so i'm guessing that, under the hood, the computer shifts that sign indicator to the MSB, which is the 16th bit, and stores FF00 in an Integer variable as
    1000000100000000
    ^
    -

    So, if someone says "unsigned integer", i'm guessing that means the same two bytes of storage, but no negatives. Therefor, the MSB can be used as part of the number, instead of sign. That gives you a range of 0 to 65,535.

    I'm further guessing that if someone says a number that starts with a minus-sign is an "unsigned integer", that means that minus sign actually indicates a 1 in the MSB's place, and the person didn't bother to convert it to a 0-to-65535 range, because they were lazy or they like to make things opaque. For example,
    -256
    as binary (with sign-indicator in MSB) is
    =DEC2BIN(-256)
    1100000000

    But
    1100000000
    is (assuming MSB is not the sign-indicator)
    768, not -256, according to:
    https://www.rapidtables.com/convert/...o-decimal.html

    So if someone says "-256 is an unsigned integer", they really mean 768.

    i'm confused why on the worksheet:
    =BIN2DEC("1100000000")
    returns
    -256
    since external bin-to-dec converters like rapidtables.com returns 768. Maybe the worksheet function should have been called BIN2INT?

    Also people have mentioned HRESULT, which i guess is C structure or constant? I'm not familiar with using HRESULT in VB (maybe with the Windows API?).

    Yep, still confused!
    Last edited by johnywhy; Dec 8th, 2021 at 02:53 PM.

  38. #38
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    Ok. You seem to mostly understand what's going on but I don't think you fully understand how negative numbers actually work. I'm going to do a follow up post explaining how they actually work and hopefully it will clear things up for you.

    BTW the Dim code I posted was VB.Net syntax. For the purposes of this the language didn't matter. C, VB, Java, it's the same everywhere. VB.Net syntax is just what I happen to be most comfortable with.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  39. #39
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: vbObjectError: what is it for?

    I got the sense from your post that you think signed numbers are just numbers where the most significant bit represents the sign. Well yes and no. It's a little more complicated than that. I'm sure you heard of two's complement before. In case you haven't, I'll make it very simple. To get the two's complement of a number, you invert all the bits and add 1 and overflows are discarded.

    So let's say you have a number stored in 4 bits, let's say it's 2. The bit pattern would look like this:-
    Code:
    0010
    To get the two's complement of that number, you first invert the bits:-
    Code:
    1101
    and then you add 1:-
    Code:
    1110
    What you have here is how modern computers would actually represent -2 using 4 bits.

    Now let's do an actual data type. Let's say you're using a language with a signed integer data type like VB6's Integer type. When you do this:-
    Code:
    Dim I As Integer
    I=200
    Well this is what happens:-
    Code:
    0000000011001000
    Now this is what you'd expect. When you math that out by summing the powers of two where the ones are you get 128+64+8 which is equal to 200. This is normal. However, remember that VB6's Integer type is a signed data type. What this means is that it can store negative numbers too. So what happens when you do -200 instead of 200? Well, it would take the two's complement of the binary representation of 200 which as we said above you can get by just inverting all the bits and adding 1 to the entire thing, discarding any overflow bits. This is how -200 would actually be stored in memory for a signed 16 bit integer data type:-
    Code:
    1111111100111000
    Now you might be wondering, why all of this convolution? This way of representing negative numbers avoids a a lot of problems like having positive and negative zero. But the real value here is that you can perform addition, subtraction and multiplication with negative numbers without having to care about signs. It will work out to the correct values.

    Now if you are looking at a bit pattern and want to know if it represents a negative number or a positive number you need to know one very important detail, is the data type a signed integer data type. VB6 has 3 integer data types and two of them are signed and one is unsigned. The two signed ones are Long and Integer and the unsigned one is Byte. With that understanding, lets say you have a bit pattern from an VB6 Integer variable is:-
    Code:
    0000000000010010
    What number is that? Well the first thing you do is look at the most significant bit, which in this case is 0. This means the number is positive. From there you just do what you normally do to convert a number from binary to decimal. In the case of the above the value is 18.

    Now lets say you have this bit pattern:-
    Code:
    1000000000000011
    Look at the most significant bit. It is 1 which means this is a negative number. But what is it's value? Remember, negative numbers are stored in two's complement form. So how do we convert it back? Easy, we just do two's complement on it again. We start by flipping all the bits, which gives us:-
    Code:
    0111111111111100
    Then we add 1 to that which gives us:-
    Code:
    0111111111111101
    Now you just do your normal binary to decimal conversion keeping in mind that whatever the result, it is actually negative. So let's sum up the powers of two where all the 1s are. We get 16381+8192+4096+2048+1024+512+256+128+64+32+16+8+4+1 which is equal to 32762. But remember the most significant bit of the original bit pattern was 1 so it means the number is negative so the actual value is -32762.

    This is how signed data types actually work in modern computers.

    As for hex. The reason we use hexadecimal because it allows us to easily target specific bytes inside integer variables. Every two digits of hexadecimal represents exactly 1 byte of data. So lets say we have an VB6 Integer variable again. Remember the Integer type in VB6 is 16 bits long which is 2 bytes. Let's say you want to set the most significant byte in an Integer variable to 255 and the least significant byte to 0, we can do this:-
    Code:
        Dim i As Integer
        i = &HFF00
    If we were to look at the bit pattern in memory we would see this:-
    Code:
    &HFF00
    1111111100000000
    Now recall that an Integer in VB6 is signed so that means numbers it stored can be positive or negative. So what value does this variable store? Well the most significant bit is 1 so it's negative. So we take the 2's compliment by first inverting all the bits which gives us this:-
    Code:
    0000000011111111
    Now we add one to this binary number which gives us this:-
    Code:
    0000000100000000
    When you perform a conversion you get the value 256 but recall that the original pattern had the most significant bit set to 1 which means it's actually negative so it's actually -256 which is what you get in VB6.

    Hopefully this has been able to clarify any confusion over how signed numbers work in VB6. Let me know if this was helpful.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  40. #40
    Member
    Join Date
    Jan 2011
    Posts
    41

    Re: vbObjectError: what is it for?

    Quote Originally Posted by Niya View Post
    Hopefully this has been able to clarify any confusion over how signed numbers work in VB6. Let me know if this was helpful.
    Thx, that was helpful, altho your explanation differs a bit from this explanation, according to which the MSB counts as the negative of it's own value, and not just a sign-bit.
    https://youtu.be/4qH4unVtJkE?t=734

    VB6 has 3 integer data types and two of them are signed and one is unsigned. The two signed ones are Long and Integer and the unsigned one is Byte.
    Ok, then i'm guessing two's complement, used to support negative (ie "signed" numbers), applies to VBA signed integer types: Long and Integer. Correct?

    And unsigned int's must be stored in the unsigned integer type, which you said is Byte, correct?

    So, when someone says error numbers are "unsigned integers", i guess they mean VBA's unsigned integer type: Byte. Correct? In which case, while i appreciate your great explanation, it seems that it doesn't squarely address the confusion about error numbers, which are unsigned, and therefore aren't stored using two's complement. Correct?

    i guess the reason i'm seeing negative numbers when i try to loop thru error-numbers is because i'm using a Long type (lErr As Long), when i should be use Byte (bytErr as Byte), correct?
    https://www.vbforums.com/showthread....=1#post5538246

    If error numbers are unsigned int's, then it would always be incorrect to refer to "negative" error numbers, as @dilletant mentioned.

    If error numbers are byte, then they would be limited to a range of 0 to 255, correct? But i believe there are VB errors higher than 255. Is it because error numbers are based on the much bigger C or C++ unsigned ints (__int8, __int16, etc)?

    If error numbers are unsigned int, then what does this mean?
    Code:
    ?vbObjectError
    -2147221504
    Is it because i need to convert that to unsigned int, by adding 2147221504 to it? Or by applying a two's complement conversion to it?

    If error numbers are unsigned int, then why does the .Raise method expect a Long for error number? Is it because VB doesn't have an unsigned int that's as big as C's unsigned int, so Long is the next best thing? Even if that's so, why doesn't VB use the same range as C, instead of storing as negative?

    Thx!
    Last edited by johnywhy; Dec 8th, 2021 at 09:40 PM.

Page 1 of 2 12 LastLast

Posting Permissions

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



Click Here to Expand Forum to Full Width