Email report with records between specified dates (1 Viewer)

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.

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!
 

KevlarKev

Registered User.
Local time
Today, 04:35
Joined
Jan 16, 2013
Messages
26
Hi bestofthelousy,

Just a suggestion as i am no VBA expert. Cant you simply tell the query that the report it based on to reference the form? That way you wont need to clever code the VBA? All you will have to do is create the VBA to send the report?

I have used this technique more than once and it works.

In the query, find the date field you want to set the date range in and type:

between [reference your textbox date 'from' field on your form] and [reference to your textbox date 'to' field on your form]

That way when the report opens its underlying query looks at these two textboxes and uses them as the date range.

Word of warning here though. You will need to make sure that neither of the text boxes have focus or the query will be unable to accept the range. Make sure that the focus is set to another object prior to running the report.

Let me know.

Kev.
 

bestofthelousy

New member
Local time
Today, 06:35
Joined
Feb 1, 2013
Messages
5
Sorry for the delayed response- was away from the computer for a few days.

I had tried that query adjustment before posting, and the function was doa with that change. I can't seem to find an answer why. I tried it again just now and was hoping that your tip about making sure that it isn't the focus was going to be what I needed to have it run but it didn't take.

The query runs like I would want it to, but I get nothing when I try to run it along with this other code for emailing custom reports. :banghead:
 

KevlarKev

Registered User.
Local time
Today, 04:35
Joined
Jan 16, 2013
Messages
26
Sorry bud, all out of ideas then :( it looks very much like VBA is to blame here and my knowledge is limited...
 

bestofthelousy

New member
Local time
Today, 06:35
Joined
Feb 1, 2013
Messages
5
Thanks for giving it a look KevlarKev! Any new ideas are much appreciated.
 

bestofthelousy

New member
Local time
Today, 06:35
Joined
Feb 1, 2013
Messages
5
Anybody else have any ideas on this? Going to be working on it again soon, so any tips on which direction to go would be great. Thanks!
 

Users who are viewing this thread

Top Bottom