From: Bruce on
Hello,

I know this must be something simple I'm overlooking but I can't get
err.raise inside a class to return anything but 440 - automation
error. For example, if I create the following test class:

Option Compare Database
Option Explicit

Private Sub Class_Initialize()

On Error GoTo Errhandler

Err.Raise vbObjectError + 70004, "User.Initialize"

Exit Sub

Errhandler:

Err.Raise Err.Number, Err.Source, Err.Description

End Sub

and then use the following code to test it:

Sub testitout()

Dim x As clsTest

On Error GoTo Errhandler

Set x = New clsTest

Exit Sub

Errhandler:

Err.Raise Err.Number, Err.Source, Err.Description

End Sub

When I call testitout() from the debug window I get an error message
saying 'run time error 440: automation error' instead of my custom
error. What do I need to do to have the caller of my class raise my
custom error instead of the generic 440 automation error? Thanks.

Bruce

From: storrboy on

Bruce wrote:
> Hello,
>
> I know this must be something simple I'm overlooking but I can't get
> err.raise inside a class to return anything but 440 - automation
> error. For example, if I create the following test class:
>
> Option Compare Database
> Option Explicit
>
> Private Sub Class_Initialize()
>
> On Error GoTo Errhandler
>
> Err.Raise vbObjectError + 70004, "User.Initialize"
>
> Exit Sub
>
> Errhandler:
>
> Err.Raise Err.Number, Err.Source, Err.Description
>
> End Sub
>
> and then use the following code to test it:
>
> Sub testitout()
>
> Dim x As clsTest
>
> On Error GoTo Errhandler
>
> Set x = New clsTest
>
> Exit Sub
>
> Errhandler:
>
> Err.Raise Err.Number, Err.Source, Err.Description
>
> End Sub
>
> When I call testitout() from the debug window I get an error message
> saying 'run time error 440: automation error' instead of my custom
> error. What do I need to do to have the caller of my class raise my
> custom error instead of the generic 440 automation error? Thanks.
>
> Bruce


I see that you are trying to raise an error 3 times in your code, but
you are not trying to do anything with it.
Your error number would be -2147151500 which I don't think is a
recognized number and since you are not doing anything with that
number ( ex. no MsgBox statement ) Access applies the Automation Error
message. The class is in effect an automation object and there was an
error in it. You need to spend a little more time reading into some of
the tips and tricks of error handling.
You example should look more like

---------------------------------------------------------------
Private Sub Class_Initialize()
On Error GoTo Errhandler

Err.Raise vbObjectError + 70004, "User.Initialize"

exit_here:
Exit Sub

Errhandler:
MsgBox Err.Description, vbOKCancel, Err.Number & " in " &
Err.Source
'Err.Raise Err.Number, Err.Source, Err.Description 'Commented
out
Resume exit_here
End Sub

-------------------------------------------------

Sub testitout()

Dim x As clsTest

On Error GoTo Errhandler

Set x = New clsTest

exit_here:
Set x = Nothing
Exit Sub

Errhandler:
Resume exit_here
'Err.Raise Err.Number, Err.Source, Err.Description 'Commented
out
End Sub