I have the following code working, probably not the best but my first time in VB. This runs a query, makes a table, writes the report to disk, opens the table and checks for up to 5 eamil addresses, then emails the report as an attachment. This worked fine until the user decided they didn't want to have to type in the ServiceCenter, we have 62 Service Centers, number and wanted the button to create the reports for each service center and email them to up to 5 people without having to type the ServiceCenter number in. Is there a way to clean up my code and/or create a table sorted by ServiceCenter and loop through this code to create and email the individual Service Center reports. Or some way to dynamically create the reports and email by ServiceCenter without human intervention?
Help Please,
Gerald
The code I have now:
Private Sub CheckRegisterByDR_Click()
On Error GoTo Err_CheckRegisterByDR_Click
Dim stDocName As String, stPath As String
Dim strEmail As String
Dim dbs As Database, rs As Recordset
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application"
Set MailOutLook = appOutLook.CreateItem(olMailItem)
stDocName = "CheckRegisterbyDateRangeforEmail"
stPath = "C:\ImportDataforZurich\CheckRegisterEmail.RTF"
'Run Query
DoCmd.OpenQuery stDocName, acNormal, acEdit
'Save Report to Disk
DoCmd.OutputTo ObjectType:=acOutputReport, _
objectName:=stDocName, _
OutputFormat:=acFormatRTF, _
OutputFile:=stPath, _
AutoStart:=False
'Setup String for Email Addresses
Set rs = dbs.OpenRecordset("CheckRegisterEmail", dbOpenDynaset)
rs.MoveFirst
If Not IsNull(rs!Email1) Then
strEmail = strEmail & rs!Email1
End If
If Not IsNull(rs!Email2) Then
strEmail = strEmail & ";" & rs!Email2
End If
If Not IsNull(rs!Email3) Then
strEmail = strEmail & ";" & rs!Email3
End If
If Not IsNull(rs!Email4) Then
strEmail = strEmail & ";" & rs!Email4
End If
If Not IsNull(rs!Email5) Then
strEmail = strEmail & ";" & rs!Email5
End If
'Email Reports
With MailOutLook
.To = strEmail
.Subject = "Check Register Report for " & rs!ServiceCenter & " on " & Date
.Body = "Attached is the Check Register Report for your Service Center." _
& (Chr$(13)) & (Chr$(10)) & (Chr$(13)) & (Chr$(10)) & (Chr$(13)) & (Chr$(10)) _
& "Thank you," & (Chr$(13)) & (Chr$(10)) & "Robin"
.Attachments.Add stPath
.Send
End With
rs.Close
Set rs = Nothing
Set dbs = Nothing
Set MailOutLook = Nothing
appOutLook.Quit
Exit_CheckRegisterByDR_Click:
Exit Sub
Err_CheckRegisterByDR_Click:
MsgBox Err.Description
Resume Exit_CheckRegisterByDR_Click
End Sub
The SQL for the query is:
SELECT Payment.*, ServiceCenters.Email1, ServiceCenters.Email2, ServiceCenters.Email3, ServiceCenters.Email4, ServiceCenters.Email5 INTO CheckRegisterEmail
FROM Payment LEFT JOIN ServiceCenters ON Payment.ServiceCenter = ServiceCenters.ServiceCenter
WHERE (((Payment.ServiceCenter)=[Enter Service Center Number]) AND ((Payment.PostDate) Between [Start Post Date] And [End Post Date]))
ORDER BY Payment.ServiceCenter, Payment.CheckNumber, Payment.InvoiceKey;
Help Please,
Gerald
The code I have now:
Private Sub CheckRegisterByDR_Click()
On Error GoTo Err_CheckRegisterByDR_Click
Dim stDocName As String, stPath As String
Dim strEmail As String
Dim dbs As Database, rs As Recordset
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application"
Set MailOutLook = appOutLook.CreateItem(olMailItem)
stDocName = "CheckRegisterbyDateRangeforEmail"
stPath = "C:\ImportDataforZurich\CheckRegisterEmail.RTF"
'Run Query
DoCmd.OpenQuery stDocName, acNormal, acEdit
'Save Report to Disk
DoCmd.OutputTo ObjectType:=acOutputReport, _
objectName:=stDocName, _
OutputFormat:=acFormatRTF, _
OutputFile:=stPath, _
AutoStart:=False
'Setup String for Email Addresses
Set rs = dbs.OpenRecordset("CheckRegisterEmail", dbOpenDynaset)
rs.MoveFirst
If Not IsNull(rs!Email1) Then
strEmail = strEmail & rs!Email1
End If
If Not IsNull(rs!Email2) Then
strEmail = strEmail & ";" & rs!Email2
End If
If Not IsNull(rs!Email3) Then
strEmail = strEmail & ";" & rs!Email3
End If
If Not IsNull(rs!Email4) Then
strEmail = strEmail & ";" & rs!Email4
End If
If Not IsNull(rs!Email5) Then
strEmail = strEmail & ";" & rs!Email5
End If
'Email Reports
With MailOutLook
.To = strEmail
.Subject = "Check Register Report for " & rs!ServiceCenter & " on " & Date
.Body = "Attached is the Check Register Report for your Service Center." _
& (Chr$(13)) & (Chr$(10)) & (Chr$(13)) & (Chr$(10)) & (Chr$(13)) & (Chr$(10)) _
& "Thank you," & (Chr$(13)) & (Chr$(10)) & "Robin"
.Attachments.Add stPath
.Send
End With
rs.Close
Set rs = Nothing
Set dbs = Nothing
Set MailOutLook = Nothing
appOutLook.Quit
Exit_CheckRegisterByDR_Click:
Exit Sub
Err_CheckRegisterByDR_Click:
MsgBox Err.Description
Resume Exit_CheckRegisterByDR_Click
End Sub
The SQL for the query is:
SELECT Payment.*, ServiceCenters.Email1, ServiceCenters.Email2, ServiceCenters.Email3, ServiceCenters.Email4, ServiceCenters.Email5 INTO CheckRegisterEmail
FROM Payment LEFT JOIN ServiceCenters ON Payment.ServiceCenter = ServiceCenters.ServiceCenter
WHERE (((Payment.ServiceCenter)=[Enter Service Center Number]) AND ((Payment.PostDate) Between [Start Post Date] And [End Post Date]))
ORDER BY Payment.ServiceCenter, Payment.CheckNumber, Payment.InvoiceKey;