Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone help with this code.

Status
Not open for further replies.

thetambarineman

Technical User
Feb 29, 2000
63
GB
This code was kindly generated by Dougp, the thing is i'm having a little trouble getting it into a sub: - can anyone help me out as i'm hoping to push this app out the door as soon as poss!! Thanks for your time!<br>

<br>

Module code:<br>

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long<br>

<br>

frm level code:<br>

Private Sub Command1_Click()<br>

On Error GoTo Err_Command1_Click<br>

<br>

' Raise the exception<br>

s = 3 / 0<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

Exit_Command1_Click: '<<< Put all this code at the bottom right above the END SUB<br>

Exit Sub<br>

<br>

Err_Command1_Click:<br>

Select Case Err.Number<br>

Case 3021<br>

' No current record<br>

<br>

Case Else<br>

reply = MsgBox("An error has occured, do you wish to send a report to the developers?", vbInformation + vbYesNo, "Error encountered: Send report?")<br>

If reply = vbYes Then<br>

<br>

<br>

'End Select<br>

<br>

Dim stext As String<br>

Dim sAddedtext As String<br>

<br>

stext = "mailto:" & "<A HREF="mailto:paul.mclornan@btinternet.com">paul.mclornan@btinternet.com</A>"<br>

<br>

sAddedtext = sAddedtext & "&Subject=" & "Autospare Code error " & Err.Number & " " & Err.Description<br>

sAddedtext = sAddedtext & "&Body=" & "The following error has been recorded!" & " " & Err.Description & " " & Err.Number & " " & Err.Source<br>

<br>

If Len(sAddedtext) <> 0 Then<br>

Mid$(sAddedtext, 1, 1) = "?"<br>

<br>

stext = stext & sAddedtext<br>

<br>

' launch default e-mail program<br>

If Len(stext) Then<br>

Call ShellExecute(Me.hwnd, "open", stext, vbNullString, vbNullString, SW_SHOWNORMAL)<br>

End If<br>

<br>

End If<br>

Else<br>

End If<br>

<br>

End Select<br>

<br>

End Sub<br>

----------<br>

the thing is that i want it to be called instead of having to lump this code in every time that i want to trap errors!!<br>

<br>

Thanks in advance:<br>

Paul<br>


 
So make a Function out of it<br>
And Pass the parameters<br>
Similar to this:<br>
<br>
Public Function E-mail(MailTo,Subject,Body, ....)<br>
<br>
<br>
Put the above code in here<br>
<br>
Including the Error traps<br>
<br>
End Function<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Here it is<br>
<br>
I have a command button that raises and error and then calls a function <br>
The function Pops up a Error box and then e-mails the error<br>
<br>
---------------------------- This is the Error handler that you call my Function with<br>
Private Sub Command1_Click()<br>
On Error GoTo Err_Command1_Click<br>
<br>
' Raise the exception<br>
s = 3 / 0<br>
<br>
Exit_Command1_Click:<br>
Exit Sub<br>
<br>
Err_Command1_Click:<br>
'Call the Error handler /E-mailer<br>
ErrorTrapEmail Me.hwnd, &quot;<A HREF="mailto:myemail@myweb.com">myemail@myweb.com</A>&quot;, &quot;Autospare Code error &quot; & Err.Number & &quot; &quot; & Err.Description, &quot;The following error has been recorded!&quot; & &quot; &quot; & Err.Description & &quot; &quot; & Err.Number & &quot; &quot; & Err.Source<br>
Resume Exit_Command1_Click<br>
<br>
End Sub<br>
<br>
-------------------------------------------<br>
this is the modified Error handler 'Put this following in Module so it can be called from anywhere in your project<br>
If you create a separate Module called &quot;ErrorEmailer.bas&quot; then you can add it to any future project.<br>
--------------------------------------------<br>
Public Sub ErrorTrapEmail(FormHandle, MailTo, Subject, Body)<br>
<br>
MsgBox &quot;Error # &quot; & Err.Number & &quot; &quot; & Err.Description, vbInformation, &quot;In sub Command1_Click&quot;<br>
<br>
Dim stext As String<br>
Dim sAddedtext As String<br>
<br>
stext = &quot;mailto:&quot; & MailTo<br>
<br>
sAddedtext = sAddedtext & &quot;&Subject=&quot; & Subject '&quot;Error From VB Program &quot; & Err.Number & &quot; &quot; & Err.Description<br>
sAddedtext = sAddedtext & &quot;&Body=&quot; & Body '&quot;Put your Body Text here if needed&quot;<br>
<br>
If Len(sAddedtext) &lt;&gt; 0 Then<br>
Mid$(sAddedtext, 1, 1) = &quot;?&quot;<br>
End If<br>
<br>
stext = stext & sAddedtext<br>
<br>
' launch default e-mail program<br>
If Len(stext) Then<br>
Call ShellExecute(FormHandle, &quot;open&quot;, stext, vbNullString, vbNullString, SW_SHOWNORMAL)<br>
End If<br>
End Sub<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top