That error doesn't get much clearer. You need to Dim your variables before you use them.
Dim strReportName as String
Sent: Wednesday, November 15, 2017 12:55 PM
To: 'Graham Mandeno' graham@mandeno.com [MS_Access_Professionals]
Subject: Re: RE: RE: [MS_AccessPros] Filter report with vba
Private Sub cmdEmailAccrualSummaryDirectReport_Click()
Dim db As DAO.Database
Set db = CurrentDb
Dim RS_1, RS_2 As DAO.Recordset
Dim strMsg As String
Dim ReportsToName, EmailAddress, SupervisorEmail, ImmedSup As String
Dim myPath, myFileName As String
Dim strSQL, strSQL1, strSQL2 As String
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Dim strQueryName As String
Dim strPrevSQL As String
Dim strSQL3 As String
Dim strReportName As String
strReportName = "rptAccrualSummaryWithReportsTo"
strQueryName = "qryReportASWRT"
DoCmd.SetWarnings False
'DoCmd.OpenQuery "qryDeleteAccrualsForReportWithReportsTo"
strSQL = "SELECT DISTINCT Nz([Asc/Ast Dir],Nz([Dept Head],'Top Dog')) AS ImmedSup" _
& " FROM SupervisorTable INNER JOIN AccrualsForReport ON SupervisorTable.[Person Id] = AccrualsForReport.Emplid" _
& " WHERE (((SupervisorTable.[Person Id]) Not Like '1208509350'));"
' Open the record set containing all the supervisors
Set RS_1 = db.OpenRecordset(strSQL, dbOpenDynaset)
RS_1.MoveFirst
' Set the path to the folder which stores all the reports
myPath = "W:\ADMINISTRATIVE SERVICES DATABASES\MANAGEMENT SUPPORT SERVICES T-A-P\ABSENCE DATABASE\Department Reports\DirectReports\"
' For all entries, loop through, generate, store and email the report
Do While Not RS_1.EOF
' This routine is divided into two parts - 1) generate and store the report and 2) email the report
' We generate the report for all supervisors and store them in the designated folder.
'**********************************************************************************************************************
' Section 1 - generate and store reports
'**********************************************************************************************************************
' Obtain the supervisor's name
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. It originally is SupervisorName from the value that is in the Supervisor field from the strSQL above in the code, which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf" 'the SupervisorName is the value of the Supervisor field from the strSQL above in the code, which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
strSQL2 = "SELECT * FROM qryReportsTo WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL2)
Debug.Print strSQL2 ' allows you to view the SQL statement
' this works DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, "", "(SelectEmployee=True) AND (ImmedSup=" & """" & ImmedSup & """)", acNormal
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
' this works DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.OpenReport strReportName, acViewPreview, , strFilterString, acHidden
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strOutputFileName
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
'**********************************************************************************************************************
' Section 2 - Email reports
'**********************************************************************************************************************
' Obtain the email address for the supervisor, set the subject and body accordingly
' Attach the report and send the mail.
If Not IsNull(ImmedSup) Then
' this works
SupervisorEmail = DLookup("[Asu Email Addr]", "[R&D-CURRENTEMPLOYEES]", "[Person Nm]='" & ImmedSup & "'")
strSQL = "SELECT DISTINCT SupervisorID, SupervisorName, SupervisorEmail FROM qryReportsASWRT;"
Set RS_1 = db.OpenRecordset(strSQL, dbOpenSnapshot)
'strSQL1 = "SELECT [R&D-CURRENTEMPLOYEES].[Asu Email Addr] FROM [R&D-CURRENTEMPLOYEES] WHERE [R&D-CURRENTEMPLOYEES].[Person Nm]='" & ImmedSup & "'"
' Set RS_2 = db.OpenRecordset(strSQL1, dbOpenDynaset)
' RS_2.MoveFirst
' SupervisorEmail = RS_2![Asu Email Addr]
strMsg = "<html><body><p>Attention " & ImmedSup & "" _
& "</p><p></br></br> Attached is your Direct Reports Accrual Summary Report." _
& "</p><p></br></br> Please review the report and contact the Time and Attendance Team, if you have any questions regarding this information.</body>" _
& "<p>" _
& "<p>" _
& "</br></br></br><p></br></br></br></br></br></br></br></br></br></br></br></br></p>" _
& "</br></br></br><p></br></br></br></br></br></br></br></br></br></br></br> Becky Loomis, CPP</br>" _
& "<br></br>Arizona State University" _
& "<br></br>Accounting Manager, Payroll, Time and Attendance" _
& "<br></br>Facilities Development and Management" _
& "<br></br>(---)--------" _
& "<br></br>Fax (xxx) xxx-xxxx" _
& "</body></html>"
Set objmail = objol.CreateItem(olMailItem)
With objmail
.BodyFormat = olFormatHTML
.To = SupervisorEmail
.Subject = "Accruals Report for " & ImmedSup
.HTMLBody = strMsg '"Hello. Attached is your Direct Reports Accrual Summary Report. If you have any questions, please contact the Time and Attendance Team." _
& vbCrLf & vbCrLf & _
" - Thank You - " 'strMsg
.NoAging = True
.Attachments.Add myFileName
.Display
End With
SendKeys "%{s}", True
End If
RS_1.MoveNext
Loop
DoCmd.SetWarnings True
MsgBox "Reports have been emailed to the Direct Reports Employees."
End Sub
Hello Jim
There is nothing obviously wrong with that line of code, now that you have removed the "Set". What is the compile error?
Would you mind posting the code for the entire procedure?
Best wishes
Graham
From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Thursday, 16 November 2017 07:04
To: Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Subject: Re: RE: [MS_AccessPros] Filter report with vba
Thank You Liz but now I am getting another compile error on
strReportName = "rptAccrualSummaryWithReportsTo"
Jim Wagner
On Wednesday, November 15, 2017 10:18:51 AM, Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Graham,
I am a little confused.
I added the suggestions and I am getting a compile error
Object Required
this is on strReportName
I have the following below but it stops
Dim strReportName As String
Set strReportName = "rptAccrualSummaryWithReportsTo"
Jim Wagner
On Tuesday, November 14, 2017 07:03:26 PM, 'Graham Mandeno' graham@mandeno.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Hi Jim
Pardon me for jumping in, but I've just been reviewing your thread and I see you are making a common error in your Dim statements:
Dim RS_1, RS_2 As DAO.Recordset
Dim ReportsToName, EmailAddress, SupervisorEmail, ImmedSup As String
Dim myPath, myFileName As String
Dim strSQL, strSQL1, strSQL2 As String
In all of these statements, every variable except the last in each line is being declared as a Variant (the default type). VB(A) is not like some other languages where:
Declare a, b, c As TypeX
will declare all three variables as TypeX. In VB(A), every variable needs to have "As <type>" after it to avoid having it declared implicitly as a Variant.
This explains why you were getting a ByRef argument type mismatch error when passing strSQL to an argument that was declared "As String" – you were actually passing a Variant.
Getting back to your original question, I think the reason you were getting all employees is that you were not mentioning SelectEmployee in your filter string. You had:
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, "", "ImmedSup=" & """" & ImmedSup & """", acNormal
... where you should have had:
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, "", "(SelectEmployee=True) AND (ImmedSup=" & """" & ImmedSup & """)", acNormal
In general, this code should create a PDF from a filtered report:
DoCmd.OpenReport strReportName, acViewPreview, , strFilterString, acHidden
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strOutputFileName
(note the two adjacent commas in the first line)
I agree with Duane that it's a good idea to base your report's RecordSource on a pre-filtered copy of qryReportsTo (filtered on SelectEmployee=True) UNLESS the same report can be usefully reused with an unfiltered RecordSource. HOWEVER, I would be wary of modifying the SQL of that query on the fly, because you risk leaving it in an unknown state, filtered by some random ImmedSup value.
Another problem is that it appears you are getting ALL supervisors in your outer loop, whether or not they have employees selected. In fact, this may well explain the blank report problem.
To get the supervisor names and email addresses for your loop, I suggest you add [R&D-CURRENTEMPLOYEES] and SupervisorTable (I hope I've understood your structure correctly!) into qryReportsTo, and include the fields [Person ID] AS SupervisorID, [Person Nm] AS SupervisorName and [Asu Email Addr] AS SupervisorEmail.
Then, for your outer loop, you can do a SELECT DISTINCT on your RecordSource query:
strSQL = "SELECT DISTINCT SupervisorID, SupervisorName, SupervisorEmail FROM qryReportsASWRT;"
Set RS_1 = db.OpenRecordset(strSQL, dbOpenSnapshot)
That way, you have everything about the supervisor you need – name, ID and email address – without resorting to further Recordsets or DLookups.
I do hope I haven't further confused matters for you!
Best wishes,
Graham Mandeno [Access MVP 1996-2015]
From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Wednesday, 15 November 2017 08:59
To: Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Subject: Re: [MS_AccessPros] Filter report with vba
Duane,
I removed the strSQL1 recordset and put the below code as you suggested.
SupervisorEmail = DLookup("[Asu Email Addr]", "[R&D-CURRENTEMPLOYEES]", "[Person Nm]='" & ImmedSup & "'")
Jim Wagner
On Tuesday, November 14, 2017 12:39:30 PM, Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
I did notice if I open the report after the process has run, the report has no records because the record source is the qryReportASWRT and it has none of the fields for the detail section or the ImmedSup Header
Jim Wagner
On Tuesday, November 14, 2017 12:09:44 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
I thought your code was designed to loop through a recordset of supervisors and create an individualized report of their subordinates.
strSQL identifies each unique supervisor. qryReportASWRT is updated for each of these supervisors and a PDF report is created which gets emailed to the supervisor.
I don't know why you are using a recordset to get the email address of the ImmedSup:
strSQL1 = "SELECT [R&D-CURRENTEMPLOYEES].[Asu Email Addr] FROM [R&D-CURRENTEMPLOYEES] WHERE [R&D-CURRENTEMPLOYEES].[Person Nm]='" & ImmedSup & "'"
Set RS_2 = db.OpenRecordset(strSQL1, dbOpenDynaset)
RS_2.MoveFirst
SupervisorEmail = RS_2![Asu Email Addr]
It seems to me you could use:
SupervisorEmail = DLookup("[Asu Email Addr]", "[R&D-CURRENTEMPLOYEES]","[Person Nm]='" & ImmedSup & "'")
or add the email table/field to strSQL so it's available without creating another recordset.
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Tuesday, November 14, 2017 12:11 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane,
I wish I could share the database. Because of the FERPA laws here at the university and other confidential information we are not allowed to share data.
I understand that the qryReportASWRT as the filter for the qryReportsTo query. I understand that the loop is looping through the records using the qryREportASWRT as the filter. The issue is that I see that when the query is changed to use the qryReportASWRT the name changes in the Criteria line.
I have been working on this project for 2 months. I think that I have hit the end of my patience for the project and so have the users waiting for me to finish it.
Jim Wagner
On Tuesday, November 14, 2017 11:03:04 AM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Jim,
Step through the code and figure out what is going on. Without having a copy of your application, I can't be of much more assistance.
Did you look at qryReportASWRT to see if it makes sense?
Regards,
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Tuesday, November 14, 2017 10:42 AM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane,
It is actually sort of working. It is filtering the records correctly but it is now sending all of the supervisors with attached reports with no records except the one that I selected one employee.
Jim Wagner
On Monday, November 13, 2017 04:37:06 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
The SQL of qryReportASWRT should be changed with every record in RS_1.
I would place a breakpoint in the code and step through it to make sure every line is being run.
Duane Hookom
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Monday, November 13, 2017 5:29 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
the sql of the qryReportASWRT is
SELECT *
FROM qryReportsTo
WHERE (((qryReportsTo.SelectEmployee)=True));
Yes, I was hoping that I could get the report filtered on what users select in the table. I see what you are doing. with the code. I must be missing something.
Jim Wagner
On Monday, November 13, 2017 04:18:30 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Jim,
The SQL should be an SQL statement, not vba. Please view the SQL of qryReportASWRT and report back.
The code basically takes the current Supervisor and inserts them into the where clause of the report's record source. The report should then be filtered by the current supervisor which is what I thought you needed.
Duane Hookom
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Monday, November 13, 2017 5:10 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
the source of the report is indeed qryReportASWRT
the sql section is
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. It originally is SupervisorName from the value that is in the Supervisor field from the strSQL above in the code, which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf" 'the SupervisorName is the value of the Supervisor field from the strSQL above in the code, which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
strSQL2 = "SELECT * FROM qryReportsTo WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL2)
Debug.Print strSQL2 ' allows you to view the SQL statement
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
Jim Wagner
On Monday, November 13, 2017 03:24:59 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
What is the record source of rptAccrualSummaryWithReportsTo and what is its SQL?
I would expect them to be:
Record Source: qryReportASWRT
SQL: "SELECT * FROM qryReportsTo WHERE ImmedSup="Some Immediate Supervisor value";
Regards,
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Monday, November 13, 2017 4:09 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane,
So I am still getting all of the records. It is not filtering the recordset. I chose 2 records to make it easier and the process emails every record.
Jim Wagner
On Wednesday, November 8, 2017 01:37:11 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Hi Jim,
First you need to change the Dim of RW_1 which your code is not necessarily creating a DAO.Recordset
Dim RS_1 As DAO.Recordset, RS_2 As DAO.Recordset
I assume the record source of your report is qryReportASWRT.
Change the "strSQL" to "strSQL2" in the third line below and add a debug.print
If Not IsNull(ImmedSup) Then
strSQL2 = "SELECT * FROM qryReportsTo WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL2)
debug.print strSQL2 ' allows you to view the SQL statement
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
Duane Hookom
MN
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Wednesday, November 8, 2017 1:43 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Private Sub cmdEmailAccrualSummaryDirectReport_Click()
Dim db As DAO.Database
Set db = CurrentDb
Dim RS_1, RS_2 As DAO.Recordset
Dim strMsg As String
Dim ReportsToName, EmailAddress, SupervisorEmail, ImmedSup As String
Dim myPath, myFileName As String
Dim strSQL, strSQL1, strSQL2 As String
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Dim strQueryName As String
Dim strPrevSQL As String
Dim strSQL3 As String
strQueryName = "qryReportASWRT"
DoCmd.SetWarnings False
strSQL = "SELECT DISTINCT Nz([Asc/Ast Dir],Nz([Dept Head],'Top Dog')) AS ImmedSup" _
& " FROM SupervisorTable INNER JOIN AccrualsForReport ON SupervisorTable.[Person Id] = AccrualsForReport.Emplid" _
& " WHERE (((SupervisorTable.[Person Id]) Not Like '1208509350'));"
' Open the record set containing all the supervisors
Set RS_1 = db.OpenRecordset(strSQL, dbOpenDynaset)
RS_1.MoveFirst
' Set the path to the folder which stores all the reports
myPath = "W:\ADMINISTRATIVE SERVICES DATABASES\MANAGEMENT SUPPORT SERVICES T-A-P\ABSENCE DATABASE\Department Reports\DirectReports\"
' For all entries, loop through, generate, store and email the report
Do While Not RS_1.EOF
' This routine is divided into two parts - 1) generate and store the report and 2) email the report
' We generate the report for all supervisors and store them in the designated folder.
'**********************************************************************************************************************
' Section 1 - generate and store reports
'**********************************************************************************************************************
' Obtain the supervisor's name
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. It originally is SupervisorName from the value that is in the Supervisor field from the strSQL above in the code, which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf" 'the SupervisorName is the value of the Supervisor field from the strSQL above in the code, which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
strSQL2 = "SELECT * FROM qryReportsTo WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL)
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
'**********************************************************************************************************************
' Section 2 - Email reports
'**********************************************************************************************************************
' Obtain the email address for the supervisor, set the subject and body accordingly
' Attach the report and send the mail.
If Not IsNull(ImmedSup) Then
strSQL1 = "SELECT [R&D-CURRENTEMPLOYEES].[Asu Email Addr] FROM [R&D-CURRENTEMPLOYEES] WHERE [R&D-CURRENTEMPLOYEES].[Person Nm]='" & ImmedSup & "'"
Set RS_2 = db.OpenRecordset(strSQL1, dbOpenDynaset)
RS_2.MoveFirst
SupervisorEmail = RS_2![Asu Email Addr]
Set objmail = objol.CreateItem(olMailItem)
With objmail
.BodyFormat = olFormatHTML
.To = SupervisorEmail
.Subject = "Accruals Report for " & ImmedSup
.HTMLBody = "Hello. Attached is your Direct Reports Accrual Summary Report. If you have any questions, please contact the Time and Attendance Team." _
& vbCrLf & vbCrLf & _
" - Thank You - " 'strMsg
.NoAging = True
.Attachments.Add myFileName
.Display
End With
SendKeys "%{s}", True
End If
RS_1.MoveNext
Loop
DoCmd.SetWarnings True
MsgBox "Reports have been emailed to the Direct Reports Employees."
End Sub
Module
Option Compare Database
Function fChangeSQL(pstrQueryName As String, strSQL As String) As String
'=============================================================
' basQueryDefs.ChangeSQL
'-------------------------------------------------------------
' Purpose : update the SQL property of a saved query
' Copyright: Duane Hookom
' Author : Duane Hookom
' Notes :
'-------------------------------------------------------------
' Parameters
'-----------
' pstrQueryName (String) Name of saved query
' strSQL (String) SQL Statement
'-------------------------------------------------------------
' Returns: the previous SQL statement
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
' 07-09-2001 DKH:
'=============================================================
' End Code Header block
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Set db = CurrentDb
Set qd = db.QueryDefs(pstrQueryName)
fChangeSQL = qd.SQL
qd.SQL = strSQL
Set qd = Nothing
Set db = Nothing
End Function
Jim Wagner
On Wednesday, November 8, 2017 12:22:16 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Hi Jim,
Can you share your current code for the function as well as your cmdEmailAccrualSummaryDirectReport_Click?
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Wednesday, November 8, 2017 12:25 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane
I replaced the module code and I am still getting a ByRef argument type mismatch error. at the strSQL in the below line
strPrevSQL = fChangeSQL(strQueryName, strSQL)
Jim Wagner
On Monday, November 6, 2017 12:23:08 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Hi Jim,
That function is old. I was thinking it was the one at http://www.tek-tips.com/faqs.cfm?fid=7433.
If you aren't using the function anywhere else in your application, you can replace the function with the one in the link.
There are times when the easiest method of creating complex queries with multiple filters is to change the SQL property of a saved query. This works well when you ... |
Regards,
Duane Hookom
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Monday, November 6, 2017 11:29 AM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane
I am now getting a new error on
strPrevSQL = fChangeSQL(strQueryName, strSQL)
the message states the following
Compile error;
ByRef Argument Type mismatch
here is the module code
Function fChangeSQL(pstrQueryName As String, strQueryName As String, strSQL As String) As String
'=============================================================
' basQueryDefs.ChangeSQL
'-------------------------------------------------------------
' Purpose : update the SQL property of a saved query
' Copyright: Duane Hookom
' Author : Duane Hookom
' Notes :
'-------------------------------------------------------------
' Parameters
'-----------
' pstrQueryName (String) Name of saved query
' strSQL (String) SQL Statement
'-------------------------------------------------------------
' Returns: the previous SQL statement
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
' 07-09-2001 DKH:
'=============================================================
' End Code Header block
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Set db = CurrentDb
Set qd = db.QueryDefs(pstrQueryName)
fChangeSQL = qd.SQL
qd.SQL = strSQL
Set qd = Nothing
Set db = Nothing
End Function
Jim Wagner
On Friday, November 3, 2017 09:27:45 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Jim,
You are setting the SQL property of a query to select from itself. This is like having [query1] with a SQL statement of "SELECT * FROM [Query1]". It's not possible in Access or possibly other query languages.
1) Create a new query named "qryReportASWRT" that has a sql statement of
"SELECT * FROM qryReportsTo;"
2) Set the Recordsource of rptAccrualSummaryWithReportsTo to qryReportASWRT.
3) Change
strQueryName = "qryReportsTo"
to
strQueryName = "qryReportASWRT"
4) Try again
Regards,
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Friday, November 3, 2017 5:50 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Here is the sub procedure below. It is crashing on the highlighted strSQL
Private Sub cmdEmailAccrualSummaryDirectReport_Click()
Dim db As DAO.Database
Set db = CurrentDb
Dim RS_1, RS_2 As DAO.Recordset
Dim strMsg As String
Dim ReportsToName, EmailAddress, SupervisorEmail, ImmedSup As String
Dim myPath, myFileName As String
Dim strSQL, strSQL1 As String
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Dim strQueryName As String
Dim strPrevSQL As String
Dim strSQL3 As String
strQueryName = "qryReportsTo"
DoCmd.SetWarnings False
strSQL = "SELECT DISTINCT Nz([Asc/Ast Dir],Nz([Dept Head],'Top Dog')) AS ImmedSup" _
& " FROM SupervisorTable INNER JOIN AccrualsForReport ON SupervisorTable.[Person Id] = AccrualsForReport.Emplid" _
& " WHERE (((SupervisorTable.[Person Id]) Not Like '1208509350'));"
' Open the record set containing all the supervisors
Set RS_1 = db.OpenRecordset(strSQL, dbOpenDynaset)
RS_1.MoveFirst
' Set the path to the folder which stores all the reports
myPath = "W:\ADMINISTRATIVE SERVICES DATABASES\MANAGEMENT SUPPORT SERVICES T-A-P\ABSENCE DATABASE\Department Reports\DirectReports\"
' For all entries, loop through, generate, store and email the report
Do While Not RS_1.EOF
' This routine is divided into two parts - 1) generate and store the report and 2) email the report
' We generate the report for all supervisors and store them in the designated folder.
'**********************************************************************************************************************
' Section 1 - generate and store reports
'**********************************************************************************************************************
' Obtain the supervisor's name
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. It originally is SupervisorName from the value that is in the Supervisor field from the strSQL above in the code, which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf" 'the SupervisorName is the value of the Supervisor field from the strSQL above in the code, which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
strSQL = "SELECT * FROM qryReportsTo WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL)
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
'**********************************************************************************************************************
' Section 2 - Email reports
'**********************************************************************************************************************
' Obtain the email address for the supervisor, set the subject and body accordingly
' Attach the report and send the mail.
If Not IsNull(ImmedSup) Then
strSQL1 = "SELECT [R&D-CURRENTEMPLOYEES].[Asu Email Addr] FROM [R&D-CURRENTEMPLOYEES] WHERE [R&D-CURRENTEMPLOYEES].[Person Nm]='" & ImmedSup & "'"
Set RS_2 = db.OpenRecordset(strSQL1, dbOpenDynaset)
RS_2.MoveFirst
SupervisorEmail = RS_2![Asu Email Addr]
Set objmail = objol.CreateItem(olMailItem)
With objmail
.BodyFormat = olFormatHTML
.To = SupervisorEmail
.Subject = "Accruals Report for " & ImmedSup
.HTMLBody = "Hello. Attached is your Direct Reports Accrual Summary Report. If you have any questions, please contact the Time and Attendance Team." _
& vbCrLf & vbCrLf & _
" - Thank You - " 'strMsg
.NoAging = True
.Attachments.Add myFileName
.Display
End With
SendKeys "%{s}", True
End If
RS_1.MoveNext
Loop
DoCmd.SetWarnings True
MsgBox "Reports have been emailed to the Direct Reports Employees."
End Sub
Jim Wagner
On Friday, November 3, 2017 12:43:48 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
In the sub
Dim strQueryName as String 'name of the saved query that is your report's record source
strQueryName = "qryReportsTo"
Do While Not RS_1.EOF
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Friday, November 3, 2017 2:29 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane
in the function or the sub procedure for the button
Jim Wagner
On Friday, November 3, 2017 12:18:42 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Sorry, there should have been another line to use strQueryName. Place this line anywhere after the Dims.
strQueryName = "qryReportsTo"
Regards,
Duane
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Friday, November 3, 2017 12:48 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
I am getting a compile error
Variable not defined
strPrevSQL = fChangeSQL(qryReportsTo, strSQL)
Jim Wagner
On Thursday, November 2, 2017 09:23:50 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Jim,
Try:
' For all entries, loop through, generate, store and email the report
' this code depends on having the function fChangeSQL() in a standard module in your Access file
Dim strSQL as String
Dim strPrevSQL as String
Dim strQueryName as String 'name of the saved query that is your report's record source
Do While Not RS_1.EOF
' This routine is divided into two parts - 1) generate and store the report and 2) email the report
' We generate the report for all supervisors and store them in the designated folder.
'**********************************************************************************************************************
' Section 1 - generate and store reports
'**********************************************************************************************************************
' Obtain the supervisor's name
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. _
It originally is SupervisorName from _
the value that is in the Supervisor _
field from the strSQL above in the code, _
which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf"
'the SupervisorName is the value of the Supervisor field _
from the strSQL above in the code, _
which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
strSQL = "SELECT * FROM YourBaseQuery WHERE ImmedSup=""" & ImmedSup & """"
'Change the report recordsource query to filter by ImmedSup
strPrevSQL = fChangeSQL(strQueryName, strSQL)
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
Duane Hookom
MN
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Thursday, November 2, 2017 5:18 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane
thank you. I am not sure how to implement that into my code to open the report with that filter I need
' For all entries, loop through, generate, store and email the report
Do While Not RS_1.EOF
' This routine is divided into two parts - 1) generate and store the report and 2) email the report
' We generate the report for all supervisors and store them in the designated folder.
'**********************************************************************************************************************
' Section 1 - generate and store reports
'**********************************************************************************************************************
' Obtain the supervisor's name
ImmedSup = RS_1!ImmedSup 'the Supervisor is being renamed here. It originally is SupervisorName from the value that is in the Supervisor field from the strSQL above in the code, which is a list of all the supervisor name
myFileName = myPath & "Accruals Report - " & ImmedSup & ".pdf" 'the SupervisorName is the value of the Supervisor field from the strSQL above in the code, which is a list of all the supervisors names
' Open the report with the filter and export it as PDF to the designated folder. Close the report.
If Not IsNull(ImmedSup) Then
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport
DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, "", "ImmedSup=" & """" & ImmedSup & """", acNormal
'DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport, , "ImmedSup=" & """" & ImmedSup & """", acNormal
DoCmd.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If
Jim Wagner
On Thursday, November 2, 2017 02:24:47 PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Jim,
I actually prefer to change the SQL of the query that is the report's record source. I use a little DAO code as described on this Tek-Tips FAQ http://www.tek-tips.com/faqs.cfm?fid=7433
Duane
There are times when the easiest method of creating complex queries with multiple filters is to change the SQL property of a saved query. This works well when you ... |
From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of Jim Wagner luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Thursday, November 2, 2017 1:26 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: [MS_AccessPros] Filter report with vba
Duane
the report still wants to do all of the employees. I only selected 2 employees and it emailed all of them.
Jim Wagner
On Thursday, November 2, 2017 11:20:01 AM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote: