Solved

Question on Err.Raise

Posted on 2001-09-07
10
182 Views
Last Modified: 2012-08-14
Attached are two test files: One is COM dll file; One is client exe file. I made a simple dll method "Test" call from client with empty SSN value passing. Why can't the client handle the Err.Raise (not be hit if put a breakpoint at "MsgBox Err.Description" at client)?

'******************************
' comTest - Class1.cls
'******************************
Public Function Test(ByVal SSN As String) As String
    On Error GoTo errh
    If Len(SSN) = 0 Then
        Err.Raise vbError + 1, , "Please input SSN" ' create run time error
        Exit Function
    End If
    Test = "Successful"
    Exit Function
errh:
    Test = Err.Description
End Function


'********************************
'  clientTest - Form1
'********************************
Private Sub cmdTest_Click()
    Dim oTest As Object
    On Error GoTo errh
    Set oTest = CreateObject("comTest.Class1")
    MsgBox oTest.Test(txtSSN)
    Exit Sub
errh:
    MsgBox Err.Description  ' set breakpoint here
End Sub
0
Comment
Question by:DevelHelper
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:DennisL
ID: 6465159
Remove the On Error goto from the Test function.
Your Com control is handling the error, but you want the client to handle it right?

When you're raising the error in Test, it goes to Test=Err.description and the client never even knows an error occured.
0
 

Author Comment

by:DevelHelper
ID: 6465180
Thank you for your help. But this just a simple code. Actually, there are two different errors should be handled. One error is from database connection; another is from VB code. "On Error GOTO errh" is used to handle error from VB code.
0
 
LVL 3

Expert Comment

by:peteyhiggins
ID: 6465201
Handle the error in the dll and then either raise an error or fire an event in that error handler.  That'll pass that error back to the client and you can process it there.  You'll have to use the with events keyword in your dll declaration to use the event method, though
0
 

Author Comment

by:DevelHelper
ID: 6465211
Could you please tell how to make change on the code?
0
 
LVL 3

Expert Comment

by:peteyhiggins
ID: 6465356
In your dll, put:

Public Event ErrorEvent(iMsg as Long, sMsg as String)

In your error handler, put this:

errh:

RaiseEvent ErrorEvent(err.Number, err.Description)

In your form put:

dim WithEvents oTest as comTest.Class1

set oTest = new comTest.Class1

and add the method

private sub oTest_ErrorEvent(iMsg as Long, sMsg as String)

.... do whatever here

end sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:rcjn
ID: 6465560
You wrote:

"Err.Raise vbError + 1, , "Please input SSN" "

For COM, it needs to be VBObjectError + 1.

0
 
LVL 15

Expert Comment

by:ameba
ID: 6465932
It is simple: BEFORE *each* Err.Raise statement add one line:
    On Error GoTo 0
    Err.Raise ...

The Err.Raise will also work correctly without that line, if you don't have error handler in your function.
But, it is good idea to ignore that fact and to add "On Error GoTo 0" statements - this will allow later changes to error handling in your function.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6465951
- you can also remove "Exit Function" - it is never executed, since Err.Raise exits function and returns code execution to the caller.

Public Function Test(ByVal SSN As String) As String
   On Error GoTo errh

   If Len(SSN) = 0 Then
       On Error GoTo 0  ' turn off error handling
       Err.Raise vbError + 1, , "Please input SSN" ' create run time error
   End If
   Test = "Successful"
   Exit Function

errh:
   Test = Err.Description
End Function
0
 

Author Comment

by:DevelHelper
ID: 6470799
Thanks! everyone.

Finally, I find it works correctly if I register the com dll in my computer, and then run the client project. But I don't know why it can't works correctly if run the com within VB project group.
0
 
LVL 15

Accepted Solution

by:
ameba earned 20 total points
ID: 6470887
You use this code:
   Dim oTest As Object
   Set oTest = CreateObject("comTest.Class1")

CreateObject is checking/ using only *registered* components.  When you run VB project in IDE, VB will generate temporary registration key "ProjectName.ClassName" which is not something CreateObject will recognize.

To use such 'temporary' component in VB group, add reference to VB DLL project, and use this code:
   Dim oTest As comTest.Class1
   Set oTest = New comTest.Class1
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 59
VBS file using code from 2nd file (txt or vbs) 4 31
Sending a email via excel using vba 6 72
Excel - Save a copy of work book 13 83
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now