Jumat, 17 November 2017

RE: RE: [MS_AccessPros] Filter report with vba

 

Hi Jim

I'm sorry if I was a little harsh.  I feel your pain, but please work through ALL of the points in my last message, and I promise things will start to work for you.

Briefly (because it's just about midnight here), in response to the error you mention below, it seems you have completely removed the Dim objol As New Outlook.Application line.  If you read my message carefully, I recommended that you remove the "New", not the entire line.  This is why you are getting the "variable not defined" error.

Please try to implement all the changes I have suggested, and then post back (including all your code) if things are still not working.  Pay special attention to the fact (or my suspicion!) that your query to loop through the supervisors is returning ALL the supervisors, not just the ones whose subordinates are selected.

Best wishes,
Graham

 

From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Friday, 17 November 2017 07:43
To: 'Graham Mandeno' graham@mandeno.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Subject: Re: RE: [MS_AccessPros] Filter report with vba

 

 

Graham,

 

Thank You for your help. At first your yelling was a little harsh. Then I realized you are only trying to help me. In my defense, I have been working on this for 2 months and it is becoming a real Debbie downer. The users want the report and I do not have an answer. We just did an organizational restructure and it is causing all kinds of havoc on reports I have created in 10 years. ReportingTo changes who gets reports and who is on the reports.

Back to my current issue. I have went through your suggestions and have tried to correct the structure and syntax. So now that I have done that I am getting different errors. So I will take each error one at a time and hopefully you can point me in the direction I need to go. Otherwise I will go in a totally different direction.

 

Let me start with the changes suggested for the diming outlook.

 

I have the following code as you suggest but get an error 

Dim objmail As MailItem
Set objol = CreateObject("Outlook.Application")

 

 

The error

variable not defined on line

Set objol = CreateObject("Outlook.Application")

 

Outlook is referenced

 

I then looked in the rest of the code and found this in the If Not  Is Null section. I commented it out and it still gave the error again. Do I need to remove the second reference?

 

Set objol = CreateObject("Outlook.Application")

 

Jim Wagner


 

 

On ‎Wednesday‎, ‎November‎ ‎15‎, ‎2017‎ ‎03‎:‎47‎:‎55‎ ‎PM, 'Graham Mandeno' graham@mandeno.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:

 

 

 

Hi Jim

Duane is right – if the RecordSource of the report already returns the correct records and you do not need to filter the report, then there is no need to open the report before calling OutputTo.  If you need to apply a filter to the report and output only the filtered results, then you need to open the report first with a WhereCondition argument.  It is also customary to open the report hidden, so as to avoid having the report window flashing annoyingly on the screen.

SO …

First choose your method.  Clearly you need to filter the records – that is what your original question was about.  The choice you have is whether to:
a) filter the report as you open it, using the WhereCondition argument to specify a filter string (your original method), or
b) filter the records before the report opens by applying a filter string to the SQL of the RecordSource query (Duane's method)

BUT BEFORE YOU DO ANYTHING, CLEAN UP YOUR CODE!!  (Sorry to shout, but it's important J

Problem 1: You are still Dim'ing most of your variables As Variant.  This line:
    Dim RS_1, RS_2 As DAO.Recordset
defines RS_2 as a DAO.Recordset, but RS_1 is a Variant.

I do not know whether VBA is clever enough to take this line:
    Set RS_1 = db.OpenRecordset(strSQL, dbOpenDynaset)
and convert the RS_1 Variant variable into a DAO.Recordset object, but I for one would never trust it to do so!

To define both RS_1 and RS_2 as DAO.Recordset objects, you need this:
    Dim RS_1 As DAO.Recordset, RS_2 As DAO.Recordset
or this:
    Dim RS_1 As DAO.Recordset
    Dim RS_2 As DAO.Recordset

Problem 2: You have variables which are not being declared at all, for example, strFilterString and strOutputFileName

Problem 3: You are declaring many variables that are not being used.  This is not a serious problem, but it makes your code much harder to understand and debug.
Unused variables include: RS_2, ReportsToName, EmailAddress, strSQL1, and strSQL3

Problem 4: For clarity and unambiguity, you should Dim objmail As Outlook.MailItem, not just MailItem

Problem 5: You are Dim'ing objol as New Outlook.Application, and then later Set'ing it to New Outlook.Application.  Technically this will create two instances of Outlook (although I thing Outlook is clever enough to avoid this).  Personally I would not use New to create objects outside my mail project or core Access/VBA, but use CreateObject instead.  this allows you to convert to using late binding at a later stage, which will make your project more robust and portable.  So, use this:
    Dim objmail As MailItem
    Set objol = CreateObject("Outlook.Application")

Problem 6:  Without fully knowing your table structures, I believe this query:
    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'));"
is returning all supervisors, not just the supervisors of the selected employees.  This is why I suggested you add the supervisor names and email addresses into your qryReportsTo query.

Incidentally, you should never use "Not Like" if you can avoid it, and you should not use Like or Not Like unless you are really using wildcards.  The WHERE clause above would be better as:
    & " WHERE (SupervisorTable.[Person Id] <> '1208509350');"
Also, I would avoid hard-coding "meaningless" values like this into a query.  Is there not something else in record 1208509350 which could identify it for exclusion (for example, is this the Top Dog", so ReportsTo is Null?)

I've just seen your latest post about the "Too few parameters" error.  Basically that means there is an error in the above SQL statement and it is revering to three field names which do not exist in the selected tables.  I will share below a piece of code I find very useful for debugging SQL statements in VBA:

Public Sub DebugSQL(sSQL As String)
Const QName = "USys_DebugSQL"
Dim db As DAO.Database
Dim qd As DAO.QueryDef
On Error GoTo ProcErr
  Set db = CurrentDb
  If SysCmd(acSysCmdGetObjectState, acQuery, QName) <> 0 Then
    If MsgBox("The temporary query '" & QName & "' is being used" & vbCrLf _
          & "Do you want to discard its current state?" _
          , vbQuestion Or vbYesNo) <> vbYes Then
      GoTo ProcEnd
    End If
    DoCmd.Close acQuery, QName, acSaveNo
  End If
  Set qd = db.QueryDefs(QName)
  If qd Is Nothing Then
    Set qd = db.CreateQueryDef(QName)
  End If
  qd.SQL = sSQL
  DoCmd.OpenQuery QName, acViewDesign
  RunCommand acCmdSQLView
ProcEnd:
  Set qd = Nothing
  Set db = Nothing
  Exit Sub
ProcErr:
  With Err
    Select Case .Number
      Case 3265 ' Item not found in this collection
        Resume Next
      Case Else
        MsgBox "Error #" & .Number & vbCrLf & .Description, vbExclamation
        Resume ProcEnd
    End Select
  End With
End Sub

Just copy and paste the code into a standard module and, when your code stops at the "Too few parameters" error, type this into the Immediate window:
DebugSQL strSQL

This should open up a query window in SQL view where you can run the query and find out more easily what is wrong with it.

Best wishes,
Graham


From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Thursday, 16 November 2017 10:00
To: MS_Access_Professionals@yahoogroups.com
Subject: Re: RE: RE: RE: [MS_AccessPros] Filter report with vba

 

 

Jim,

 

This variable is not Dim'd. You need to Dim every variable used in your code. This is enforced by Option Explicit in the module declarations.

 

I don't know why you are even opening the report. I expect you only want to save the report to PDF and then attach it to an email. If this is true then only one of these lines should be run. The others need to be commented out.

 

        ' 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"

 

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 15, 2017 2:24 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: RE: RE: RE: [MS_AccessPros] Filter report with vba

 

 

Duane,

 

Now I see another variable error after fixing the quotes

 

now it stops on 

strFilterString

 

DoCmd.OpenReport strReportName, acViewPreview, , strFilterString, acHidden

 

Jim Wagner


 

 

On ‎Wednesday‎, ‎November‎ ‎15‎, ‎2017‎ ‎01‎:‎16‎:‎17‎ ‎PM, Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:

 

 

 

Jim,

 

I see an issue with the types of double-quotes. Notice the first line uses end quotes which is wrong. The second line uses " which is correct for VBA. You typically get errors like this if you copy and paste from a word processor. 

 

strReportName = "rptAccrualSummaryWithReportsTo" 
strQueryName = "qryReportASWRT" 


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: Wednesday, November 15, 2017 2:10 PM
To: Liz Ravenwood liz_ravenwood@beaerospace.com [MS_Access_Professionals]
Subject: Re: RE: RE: RE: [MS_AccessPros] Filter report with vba

 

 

Yes, it is compile error on the strReportName = "rptAccrualSummaryWithReportsTo" line

 

Jim Wagner


 

 

On ‎Wednesday‎, ‎November‎ ‎15‎, ‎2017‎ ‎01‎:‎07‎:‎49‎ ‎PM, Liz Ravenwood liz_ravenwood@beaerospace.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:

 

 

 

When you compile it, does the line get highlighted where it is finding which variable is not being defined?

Respectfully,
[RCEmailSigLogo]
Liz Ravenwood
Information Technology
Interior Systems
(520) 239-4808
Liz_ravenwood@beaerospace.com<mailto:Liz_ravenwood@beaerospace.com>
rockwellcollins.com<http://www.rockwellcollins.com/>

From: MS_Access_Professionals@yahoogroups.com [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Wednesday, November 15, 2017 12:38 PM
To: Duane Hookom duanehookom@hotmail.com [MS_Access_Professionals]
Subject: Re: RE: RE: [MS_AccessPros] Filter report with vba




Duane,

I missed that one. I commented it out but get the same variable not defined error.

Jim Wagner
________________________________


On ‎Wednesday‎, ‎November‎ ‎15‎, ‎2017‎ ‎12‎:‎24‎:‎01‎ ‎PM, Duane Hookom duanehookom@hotmail.com<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> wrote:




Jim,



I see two DoCmd.OpenReport in the code. Is there a reason for this? I would expect the first one would be commented out.



Duane



________________________________
From: MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Wednesday, November 15, 2017 12:55 PM
To: 'Graham Mandeno' graham@mandeno.com<mailto:graham@mandeno.com> [MS_Access_Professionals]
Subject: Re: RE: RE: [MS_AccessPros] Filter report with vba


the error is Variable not defined


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



Jim Wagner
________________________________


On ‎Wednesday‎, ‎November‎ ‎15‎, ‎2017‎ ‎11‎:‎47‎:‎18‎ ‎AM, 'Graham Mandeno' graham@mandeno.com<mailto:graham@mandeno.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> wrote:




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> [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Thursday, 16 November 2017 07:04
To: Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:graham@mandeno.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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> [mailto:MS_Access_Professionals@yahoogroups.com]
Sent: Wednesday, 15 November 2017 08:59
To: Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Tuesday, November 14, 2017 12:11 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Tuesday, November 14, 2017 10:42 AM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Monday, November 13, 2017 5:29 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Monday, November 13, 2017 5:10 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Monday, November 13, 2017 4:09 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Wednesday, November 8, 2017 1:43 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Wednesday, November 8, 2017 12:25 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<https://nam03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com%2Ffaqs.cfm%3Ffid%3D7433&data=02%7C01%7Cduanehookom%40hotmail.com%7C9e607fb7da79439d909008d526d60d97%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636457623145302434&sdata=apdDmElSoPY%2B1ACRQmHAVKgQHyxYl6h0DLyMcwVYZWM%3D&reserved=0>.



If you aren't using the function anywhere else in your application, you can replace the function with the one in the link.

How to Change SQL property of saved query (DAO ...<https://nam03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com%2Ffaqs.cfm%3Ffid%3D7433&data=02%7C01%7Cduanehookom%40hotmail.com%7C9e607fb7da79439d909008d526d60d97%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636457623145302434&sdata=apdDmElSoPY%2B1ACRQmHAVKgQHyxYl6h0DLyMcwVYZWM%3D&reserved=0>

www.tek-tips.com<https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com&data=02%7C01%7Cduanehookom%40hotmail.com%7C0ebd59b609ec424e36a408d52c5a879a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636463689690471564&sdata=Bh57YQp3xhwVO2bsFEYftRH0ozEw%2FlgCfmIJ7mfRGNg%3D&reserved=0>

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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Monday, November 6, 2017 11:29 AM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Friday, November 3, 2017 5:50 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Friday, November 3, 2017 2:29 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Friday, November 3, 2017 12:48 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Thursday, November 2, 2017 5:18 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto: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<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com%2Ffaqs.cfm%3Ffid%3D7433&data=02%7C01%7Cduanehookom%40hotmail.com%7Cbc478f45bf8f40548a7408d5223f9ecc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636452579034775506&sdata=GyAHF9tV6TPo5ComXOQ%2F8X0gd8l%2FVeHYWWlmBzW5NWE%3D&reserved=0>



Duane

How to Change SQL property of saved query (DAO ...<https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com%2Ffaqs.cfm%3Ffid%3D7433&data=02%7C01%7Cduanehookom%40hotmail.com%7Cbc478f45bf8f40548a7408d5223f9ecc%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636452579034775506&sdata=GyAHF9tV6TPo5ComXOQ%2F8X0gd8l%2FVeHYWWlmBzW5NWE%3D&reserved=0>

www.tek-tips.com<https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.tek-tips.com&data=02%7C01%7Cduanehookom%40hotmail.com%7C0ebd59b609ec424e36a408d52c5a879a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636463689690471564&sdata=Bh57YQp3xhwVO2bsFEYftRH0ozEw%2FlgCfmIJ7mfRGNg%3D&reserved=0>

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<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of Jim Wagner luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Thursday, November 2, 2017 1:26 PM
To: Duane Hookom duanehookom@hotmail.com<mailto: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<mailto:duanehookom@hotmail.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> wrote:







Jim,



What happens if you get rid of the first

DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport



Regards,

Duane Hookom

MN



________________________________

From: MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com> <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>> on behalf of luvmymelody@yahoo.com<mailto:luvmymelody@yahoo.com> [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>>
Sent: Thursday, November 2, 2017 1:14 PM
To: MS_Access_Professionals@yahoogroups.com<mailto:MS_Access_Professionals@yahoogroups.com>
Subject: [MS_AccessPros] Filter report with vba





Hello all,



I am trying to filter a report in VBA. The user wants to limit the records being emailed out for an Accruals report. The user wants to select the employees in a table. Then the report is to be emailed to the supervisors. So I have the following code to open the report. The user will open the table and click a Yes/No check box and then I need the report to use the SelectEmployee field to be used to exclude employees with the check box selected. I have tried to add the filter on the docmd.openreport line but it never filters the report.



Thank You for any help.

Jim Wagner



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.OutputTo acOutputReport, "rptAccrualSummaryWithReportsTo", "PDFFormat(*.pdf)", myFileName, False, "", 0, acExportQualityPrint
DoCmd.Close acReport, "rptAccrualSummaryWithReportsTo"
End If



































This email (and all attachments) is for the sole use of the intended recipient(s) and may contain privileged and/or proprietary information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.

[Non-text portions of this message have been removed]

 

 

__._,_.___

Posted by: "Graham Mandeno" <graham@mandeno.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (51)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.


.

__,_._,___

Tidak ada komentar:

Posting Komentar