bestofthelousy
New member
- Local time
- Today, 06:35
- Joined
- Feb 1, 2013
- Messages
- 5
I am pretty new to Access. Most of what I have learned about VBA has come from this site, so thanks to all contributors.
I have a form where dates are entered, and I want to generate a report and send to the stakeholders a report with their records between that date range.
Currently the recordset is combing for the stakeholders included between those dates and sends them a report with ALL of their records. I can't figure out how to have it only send those reports between the dates entered.
qryNotifyStakeholders is "SELECT tblEvent.luPrimaryLocation, tblEvent.luSubLocation, tblEvent.listConditionFound, tblEvent.listConditionLeft, tblEvent.dateFound, tblEvent.timeFound, tblEvent.txtNotes, tblEvent.luOfficer, tblEvent.luStakeholder, tblStakeholders.txtEmail, tblStakeholders.StakeholdersID
FROM tblStakeholders INNER JOIN tblEvent ON tblStakeholders.StakeholdersID = tblEvent.luStakeholder;"
Any help would be great. Thanks!
I have a form where dates are entered, and I want to generate a report and send to the stakeholders a report with their records between that date range.
Currently the recordset is combing for the stakeholders included between those dates and sends them a report with ALL of their records. I can't figure out how to have it only send those reports between the dates entered.
Code:
Private Sub btnSendReports_Click()
If IsNull([txtboxBeginDate]) Or IsNull([txtboxEndDate]) Then
MsgBox "You must enter both beginning and ending dates."
DoCmd.GoToControl "txtboxBeginDate"
ElseIf [txtboxEndDate] - [txtboxBeginDate] > 30 Then
MsgBox "Cannot send more than 30 days per report."
DoCmd.GoToControl "txtboxBeginDate"
Else
If [txtboxBeginDate] > [txtboxEndDate] Then
MsgBox "Ending Date must be greater than Beginning Date."
DoCmd.GoToControl "txtboxBeginDate"
End If
If [txtboxBeginDate] < [txtboxEndDate] Or [txtboxBeginDate] = [txtboxEndDate] Then
'Building Report from Recordset and send to email below
Dim MyDB As Database, RS As Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long
Dim strTo As String
Dim stEmailAddress As String
Dim rpt As Report
Dim zWhere As String
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Me!txtProgress = Null
stDocName = "rptAreaSecurity"
Set RS = MyDB.OpenRecordset("SELECT DISTINCT [StakeholdersID], [txtEmail] FROM qryNotifyStakeholders WHERE dateFound BETWEEN #" & [Forms]![frmReports]![txtboxBeginDate] & "# And #" & [Forms]![frmReports]![txtboxEndDate] & "#")
lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
MsgBox "No reports to send.", vbInformation
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
lngCount = lngCount + 1
lblStatus.Caption = "Writing Message " & CStr(lngCount) _
& " of " & CStr(lngRSCount) & "..."
'Output to report Currently Prints Report with all records- need to limit records by email
DoCmd.OpenReport stDocName, acViewDesign
Set rpt = Reports(stDocName)
'Fitler
rpt.Filter = "[StakeholdersID] =" & RS!StakeholdersID
rpt.FilterOnLoad = True
'must save the fitler
DoCmd.Save acReport, stDocName
DoCmd.Close acReport, stDocName
' Now prepare the Email
'this chooses the emial address from the recordset
stEmailAddress = RS!txtEmail
'Chr(10) goes to next line
stESubject = "Area Security Report"
'now send the email and loop to the next one
DoCmd.SendObject acReport, stDocName, "PDFFormat (*.pdf)", stEmailAddress, , , stESubject, "Area Security Report Attached", False
RS.MoveNext
Loop
End If
Exit_btnSendReports_Click:
RS.Close
Set RS = Nothing
Set rpt = Nothing
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close
Me!txtProgress = "Sent " & CStr(lngRSCount) & " emails."
lblStatus.Caption = "Email disconnected"
MsgBox "Done sending reports. ", vbInformation, "Done"
lblStatus.Caption = "Idle..."
Exit Sub
End If
End If
End Sub
qryNotifyStakeholders is "SELECT tblEvent.luPrimaryLocation, tblEvent.luSubLocation, tblEvent.listConditionFound, tblEvent.listConditionLeft, tblEvent.dateFound, tblEvent.timeFound, tblEvent.txtNotes, tblEvent.luOfficer, tblEvent.luStakeholder, tblStakeholders.txtEmail, tblStakeholders.StakeholdersID
FROM tblStakeholders INNER JOIN tblEvent ON tblStakeholders.StakeholdersID = tblEvent.luStakeholder;"
Any help would be great. Thanks!