Return custom error in a class back to the calling routine

I have a class module in which I am raising a custom error and I want to return this error
to the calling program. Currently I have code as shown below. Any suggestions would be of
great help. Thanks - Inayat

'The Form code,in button click
Private Sub Command1_Click()

On Error GoTo Email_Error

Dim cloEmail As AutoEmail

-----
-----
-----
Exit_Error:
    Exit Sub
   
Email_Error:

    MsgBox Err.Description

    Resume Exit_Error

End Sub



'Code found in Class AutoEmail
Public Sub SendEmail()
On error Goto ErrorTrap

dim sPassword as string    
sPassword = LoadFromINIfile("PWD", "Password")

If Len(sPassword) = 0 Then
 Call Err.Raise(vbObjectError + 1001, "AutoEmail", "Password File not found.")
End If

ExitMe:
    Exit Sub

ErrorTrap:

/* Now I want to return this error to the calling procedure Command1_Click()*/

End Sub
Inayat081501Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornSQL Server Data DudeCommented:
In Access VBA this would be err.Raise "Pick a Number", "Pick a source", "Description", "Path of help file if any", "HelpID"
PaulHewsCommented:
>Now I want to return this error to the calling procedure Command1_Click()

Take the On error goto... and errortrap out of the procedure.  The error will bubble up to the caller if it is not handled in the called procedure.
Jim HornSQL Server Data DudeCommented:
Another option is you can assign a range of custom error number, and change your error trap to this...

ErrorTrap:
   Select Case Err.Number
         Case BeginRange to EndRange
                'Custom error, let it be returned to calling proc..
                 Resume ExitMe
         Case Else
                 'Application/DLL/Whatever other error.  Handle it here as you wish.
   End Select
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

PaulHewsCommented:
Jim...
>I want to return this error to the calling program.

If it's handled in the component, it will not be returned to the calling program...  Resume will just cause the  error to disappear.  And if you handle the custom error in the calling app, you may as well handle the other errors there as well.
Jim HornSQL Server Data DudeCommented:
You are correct.

One way to deal with this is to change your sub to a function, with a return value of True (no errors) or False (function did not run, likely due to an error).   If you want the exact number, then change from Boolean to Long, and return 0 for no errors or the number for error occured.  Lots of ways to handle that.
PaulHewsCommented:
That defeats the purpose of using Err.Raise.  Then you would just return some status code instead...  The whole point of using Err.Raise is to bubble the error up to the calling routine.  Remember the Err object has information like description, source, helpfile, etc.  The person who's using the class then has the option of how they want to use that information... log it to a file, pop up a message, etc.
Inayat081501Author Commented:
Hello jimhorn/PaulHews:

Thank you for your suggestions.

Here is my problem. If the length of the string sPassword is zero, the execution would still continue and a dialog box would pop up prompting the user to enter the password. The subsequent line(s) of code do not regard the password being absent as a problem. If it is not found, it tries to get it from the user. And I can't control this behavior. It comes from a library that I am referencing in my class.

Therefore I am trying to avoid this dialog box. I hope I am making sense.

Awaiting to hear from you. Thank you in advance.

- Inayat
Jim HornSQL Server Data DudeCommented:
If you can't edit/control the code in the library you're referencing, then perhaps you should test it before making the function call.

Bad design, I know, but...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulHewsCommented:
This will return the error to the Command1_Click error handler instead of continuing.

Public Sub SendEmail()


  dim sPassword as string    
  sPassword = LoadFromINIfile("PWD", "Password")

  If Len(sPassword) = 0 Then
   Call Err.Raise(vbObjectError + 1001, "AutoEmail", "Password File not found.")
  End If

  ''' other code to send email


End Sub
nffvrxqgrcfqvvcCommented:
Don't know how much help this will be.

Option Explicit
Public eError As String

Public Sub TestErr()
On Error GoTo tError
Dim dBYzero As Integer
dBYzero = 0 \ 0
tError:
eError = err.Number & " " & err.Description
End Sub

Public Sub TestWithoutError(F As Object)
On Error GoTo tError
Dim strEtest As String
strEtest = 10 * 2
F.Caption = strEtest
tError:
eError = vbNullString
Select Case err
    Case 0
    Exit Sub
    Case Else
    eError = err.Number & " " & err.Description
End Select

End Sub


Option Explicit

Dim C As New Class1

Private Sub Form_Load()
'-------- ERROR ---------
C.TestErr
MsgBox C.eError

'-------- NO ERROR BELOW -------
C.TestWithoutError Form1
If C.eError = vbNullString Then
Exit Sub
Else
MsgBox C.eError
End If

End Sub
nffvrxqgrcfqvvcCommented:
What is the name of the DLL. there might be some Error Constants associated with it. The only way to know is to load the reference and look in the object browser.
PaulHewsCommented:
> Don't know how much help this will be.

If you want my opinion, not very much, especially looking at it from a general code-worthiness viewpoint.

-It limits the developer to error number and description... In string format no less, so you would have to parse them apart if you want to use the description in a message box without showing the user the error number.
-It would mean that every function would have to return a string instead of what the function would return.

AND it's essentially the same idea proposed by jimhorn in this comment: http:Q_21482049.html#14379258 , except returning a string instead of a number...  

The spaghetti has a tasty sauce however.




nffvrxqgrcfqvvcCommented:
I LOVE SPAGHETTI. But I prefer pizza.
If LenB(sPassword) = 0 Then

nffvrxqgrcfqvvcCommented:
Makes no sense to assist that comment, atleast assist the comment with code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.