Let me preface this by saying I completely understand and acknowledge that the way this database was built is not efficient. However, my office wants to keep this setup.
I have a reports form (frmReports2) to generate reports based on information selected by the user. frmReports2 also contains a multi-value field list box to choose among specific fields to add to the report. All this works fine. My problem, however, is that 9 of the fields contain financial values. If the user selects one or more of these fields (FY1415, FY1516, FY1617, FY1718, FY1819, FY1920, FY2021, FY2122, FY2223) for the report, I need to have a calculated control on the report for each record that will sum up the values of the fields the user chose to populate on the report. So if the user chose to show values of FY1415, FY1516, FY1617 and FY1718, I would need to add those to the report (which is already done) but also add an additional field to calculate the sum of those (in this instance, 4) fields that the user chose.
The code below takes all of the user input and creates the SQL string. This all works properly.
The code below is the code that twinnyfo provided for my auto report:
I'm assuming the code for the calculated control will need to go into the CreateAutoRepor t module.
Unfortunately, I've taught myself I know in Access, and I've exceeded my knowledge.
I have a reports form (frmReports2) to generate reports based on information selected by the user. frmReports2 also contains a multi-value field list box to choose among specific fields to add to the report. All this works fine. My problem, however, is that 9 of the fields contain financial values. If the user selects one or more of these fields (FY1415, FY1516, FY1617, FY1718, FY1819, FY1920, FY2021, FY2122, FY2223) for the report, I need to have a calculated control on the report for each record that will sum up the values of the fields the user chose to populate on the report. So if the user chose to show values of FY1415, FY1516, FY1617 and FY1718, I would need to add those to the report (which is already done) but also add an additional field to calculate the sum of those (in this instance, 4) fields that the user chose.
The code below takes all of the user input and creates the SQL string. This all works properly.
Code:
Dim filterContract As String Dim filterCounty As String Dim filterPriority As String Dim filterFields As String Dim strFilter As String Dim strSQL As String Dim i As Integer Dim idx As Variant Dim lst As Access.ListBox Dim andor As String 'Put code here to make it an AND or OR If Me.optAndOr = 1 Then andor = " AND " Else andor = " OR " End If 'Check for contract entry Set lst = Me.lstContractType For Each idx In lst.ItemsSelected If filterContract = "" Then filterContract = "'" & lst.ItemData(idx) & "'" Else filterContract = filterContract & ", '" & lst.ItemData(idx) & "'" End If Next idx If filterContract <> "" Then filterContract = "ContractType IN (" & filterContract & ")" & andor End If 'Check for counties section Set lst = Me.lstCounties For Each idx In lst.ItemsSelected If filterCounty = "" Then filterCounty = "'" & lst.ItemData(idx) & "'" Else filterCounty = filterCounty & ", '" & lst.ItemData(idx) & "'" End If Next idx If filterCounty <> "" Then filterCounty = "County.value IN (" & filterCounty & ")" & andor End If 'Check for priority category Set lst = Me.lstPriorityCategory For Each idx In lst.ItemsSelected If filterPriority = "" Then filterPriority = "'" & lst.ItemData(idx) & "'" Else filterPriority = filterPriority & ", '" & lst.ItemData(idx) & "'" End If Next idx If filterPriority <> "" Then Debug.Print filterPriority filterPriority = "PriorityCategory.value IN (" & filterPriority & ")" & andor End If Set lst = Me.lstFields For Each idx In lst.ItemsSelected If filterFields = "" Then filterFields = lst.ItemData(idx) temptotals = lst.ItemData(idx) strSQL = "SELECT * from tblmain " Else filterFields = filterFields & ", " & lst.ItemData(idx) temptotals = temptotals & " + " & lst.ItemData(idx) End If Next idx If filterFields <> "" Then Debug.Print filterFields strSQL = "SELECT PayeeName, ContractNum, ContractType, County, PriorityCategory, " & filterFields & " FROM tblmain " End If strFilter = filterContract & filterCounty & filterPriority If strFilter <> "" Then strFilter = " WHERE " & strFilter strFilter = Left(strFilter, Len(strFilter) - Len(andor)) strSQL = strSQL & strFilter End If Debug.Print strSQL CreateAutoReport (strSQL) End sub
Code:
Option Compare Database Option Explicit Private appAccess As New Access.Application Public Sub CreateAutoReport(strSQL As String) Dim rpt As Access.Report Dim rptReport As Access.Report Dim strCaption As String Dim lblReport As Access.Label Dim txtReport As Access.TextBox CurrentDb.QueryDefs("qryDummy").SQL = strSQL 'Set appAccess = CreateObject("Access.Application") appAccess.OpenCurrentDatabase (CurrentDb.Name) ' Open dummy query to invoke NewObjectAutoReport command on it ' Put the report created to design view to make properties editable With DoCmd .OpenQuery "qryDummy", acViewNormal .RunCommand acCmdNewObjectAutoReport .Close acQuery, "qryDummy" .RunCommand acCmdDesignView End With ' Get reference to just created report For Each rpt In Reports If rpt.RecordSource = "qryDummy" Then Set rptReport = rpt Next With rptReport ' Create title control With CreateReportControl(.Name, acLabel, _ acPageHeader, , "Title", 0, 0) .FontBold = True .FontSize = 12 .SizeToFit End With ' Create timestamp on footer CreateReportControl .Name, acLabel, _ acPageFooter, , Now(), 0, 0 ' Create page numbering on footer With CreateReportControl(.Name, acTextBox, _ acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", _ .Width - 1000, 0) .SizeToFit End With ' Detach the report from dummy query .RecordSource = strSQL ' Set the report caption to autogenerated unique string strCaption = GetUniqueReportName If strCaption <> "" Then .Caption = strCaption End With DoCmd.RunCommand acCmdPrintPreview Set rptReport = Nothing End Sub
I'm assuming the code for the calculated control will need to go into the CreateAutoRepor t module.
Unfortunately, I've taught myself I know in Access, and I've exceeded my knowledge.
Comment