[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error Trapping

Posted on 2002-06-11
4
Medium Priority
?
380 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 100 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

831 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