Rabu, 08 November 2017

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.

www.tek-tips.com
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

www.tek-tips.com
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:


 

Jim,


What happens if you get rid of the first 

DoCmd.OpenReport "rptAccrualSummaryWithReportsTo", acViewReport


Regards,

Duane Hookom

MN




From: MS_Access_Professionals@yahoogroups.com <MS_Access_Professionals@yahoogroups.com> on behalf of luvmymelody@yahoo.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com>
Sent: Thursday, November 2, 2017 1:14 PM
To: 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















__._,_.___

Posted by: Jim Wagner <luvmymelody@yahoo.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (16)

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