Solved

Error Trapping

Posted on 2002-06-11
4
370 Views
Last Modified: 2012-05-04
I need to trap for a Runtime error 426 and when this happens I need an email send to test@test.com with the error code in the message.  Can some on send me some sample code to me with this this in it.

Thanks...
0
Comment
Question by:mtbuford
  • 3
4 Comments
 

Accepted Solution

by:
VoiceOfDissent earned 50 total points
ID: 7071142
That's fairly straightforward, assuming you have the code to send the email.  That code is available elsewhere, so I won't rip another's code off for you.  But here's the basics.

Sub SendEmail(strSubjText as String, strBodyText as String, strRecipient as String)

(email code goes here)

End Sub


In the procedure to trap for the error 426...

On Error Goto Error_handler
(your code here)


Error_handler:
if Err.Num = 426 then
     Call SendEmail("Error 426 in MyProg", Err.Description, "test@test.com")

Else
     response = msgbox(Err.Description, vbOkOnly, "Error " & Err.Num)

end if

Resume Next (or Resume (line name))

Hope this helps.  Good luck.


0
 
LVL 5

Expert Comment

by:rpai
ID: 7071148
The following lines of code uses Microsoft OutLook Library to send an email.

In you function where you want to Trap the Error 426 implement this

Public Sub SendEmailOnError()

On Error Goto ErrHandler
.
.
.
.
Exit Function
SendMessage
ErrHandler:

End Function


Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

On Error Resume Next

Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String

strSubject = "Error 426"
strRecip = "test@test.com "
strMsg = "Runtime error 426 generated"

' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
If Len(strRecip) = 0 Then
    strMsg = "You must designate a recipient."
    MsgBox strMsg, vbExclamation, "Error"
    Exit Function
End If

' Assume success
fSuccess = True

' Here's where the real Outlook Automation takes place
If GetOutlook = True Then
    Set mItem = mOutlookApp.CreateItem(olMailItem)
    mItem.Recipients.Add strRecip
    mItem.Subject = strSubject
    mItem.Body = strMsg
   
    ' This code allows for 1 attachment, but with slight
    ' modification, you could provide for multiple files.
    If Len(strAttachment) > 0 Then
        mItem.Attachments.Add strAttachment
    End If
   
    mItem.Save
    mItem.Send
End If

' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False
SendMessage = fSuccess
End Function


Private Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
    Err.Clear
    Set mOutlookApp = CreateObject("Outlook.application")
       
    If Err.Number > 0 Then
        MsgBox "Could not create Outlook object", vbCritical
        fSuccess = False
        Exit Function
    End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")
   
If Err.Number > 0 Then
    MsgBox "Could not create NameSpace object", vbCritical
    fSuccess = False
    Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
   
End Function
0
 
LVL 5

Expert Comment

by:rpai
ID: 7071285
Ooops!
Should be :-
On Error Goto ErrHandler
.
.
.
.
Exit Function

ErrHandler:
SendMessage
End Function

And the rest of the code as posted above should work.
0
 
LVL 5

Expert Comment

by:rpai
ID: 7084132
Please maintain your open questions.
Questions Asked 83
Last 10 Grades Given A A A A C A B A A A  
Question Grading Record 66 Answers Graded / 66 Answers Received
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

20 Experts available now in Live!

Get 1:1 Help Now