I am relatively new to access and I have been tasked to improve the navigation of the app.
I have a form which is used to open the report.
The record source of the report is as follows,
SELECT
Projects.Project_Number,
Projects.Customer,
Projects.End_User,
Projects.Engineering_Company,
[Merged Ship and Delivery Dates].Sched_Next_Delivery_Date,
[Merged Ship and Delivery Dates].Sched_Next_Ship_Date,
Projects.QC_Pack_Req,
Projects.Target_QC_Pack_Date,
Projects.Invoice_QC_Pack
FROM
Projects LEFT JOIN [Merged Ship and Delivery Dates]
ON Projects.Project_Number = [Merged Ship and Delivery Dates].Project_Number
WHERE
(((Projects.Project_Number = [Project Number] )))
ORDER BY Projects.Project_Number;
I am trying to get the report to open up without prompting me every time. There are instances where I need to refresh the report or open it from other forms.
I have tried to use
DoCmd.OpenReport "Project Control Sheet", _
acViewReport, , _
"[Project_Number]=" & Me.ProjectNumber, , _
"[Project_Number]=" & Me.ProjectNumber
It is still unable to pass the parameters to the record source. Is there anyway for me to pass the parameters to the recordsource(query)?
I have tried to use
Forms![formName]ProjectNumber
in the where statement but this only works for a single form and I have other forms which opens up this report.
The purpose of refreshing the form is to allow users to view the changes made to the report after they have updated it.
If you remove the WHERE clause from the report's Record Source query, you can later filter the rows it returns with the WhereCondition option of the DoCmd.OpenReport method.
Dim strWhereCondition As String
strWhereCondition = "[Project_Number]=" & Me.ProjectNumber.Value
Debug.Print strWhereCondition ' <- view this in Immediate window; Ctrl+g will take you there
'DoCmd.OpenReport "Project Control Sheet", acViewReport, , strWhereCondition
DoCmd.OpenReport ReportName:="Project Control Sheet", _
View:=acViewReport, WhereCondition:=strWhereCondition
If you want a different approach, you could use a TempVar, since your Access version is 2007.
TempVars.Add "ProjectNumber", Me.ProjectNumber.Value
And change the Record Source query to use the TempVar ...
WHERE Projects.Project_Number = [TempVars]![ProjectNumber]
Related
I'm attempting to create a master report that I can use to combine multiple other reports into one. Ideally with the end result being a single PDF output.
I've been attempting to use a table to store the names of the reports I would like to populate into the master report and use those to define the SourceObject for the sub reports on load.
Ideally would like the option to have a variable number of sub-reports, so I've been attempting to use the Grouping function to accomplish that. So far the only result I'm getting is having the same report in all groups. I've tried putting the following code into OnLoad and OnCurrent - where txtPageReport is a textbox that is storing the report name.
subReportName = Me.txtPageReport
Me.subReport.SourceObject = subReportName
Any suggestions would be appreciated. Maybe I'm going about this the wrong way completely.
Thanks!
Consider the following two options which does not require table storage of subreports to be called dynamically by master report.
Grouped Report
Insert all subreports into the Group Header section (zero-length Detail section). Then, set each subreport Format property to Can Shrink = Yes and have report page break by Employee Group using Force New Page = Before Section. You can now open report in Print Preview and save as PDF using option on Ribbon.
Non-Grouped Report
Insert all subreports into Detail section (no grouping). Then, dynamically filter report using DoCmd.OpenReport method. Use Where Condition argument to filter by EmployeeID.
Below is a VBA loop to iterate through all employees and then output each individual employee report to PDF. Code can be used for either above grouped or non-grouped report.
Dim db as Database
Dim rst as Recordset
Dim strFile As String
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT EmployeeID FROM Employees", dbOpenDynaset)
Do While Not rst.EOF
' OPEN FILTERED REPORT IN PRINT PREVIEW '
DoCmd.OpenReport "ReportName", acViewPreview, , "EmployeeID = " & rst!EmployeeID
' OUTPUT REPORT INTO PDF '
strFile = "C:\Path\To\Output\Folder\Employee_" & rst!EmployeeID & ".pdf"
DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, strFile, False
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set db = Nothing
I have a quick question about reports. I have a report that has two unbound subreports (the data is from two different databases and do not share any information. I usually opened these reports individually by using a simple UI to select different options and generate a string where and open the report ie,
DoCmd.OpenReport str_rptname, acViewReport, , strWhere
strWhere is made by the UI
Now that I have combined them into one report the string where does not work. Is it possible to pass a where string to a subform? If so how would I go about doing so? Is there another/better way to do this?
Thank you!
as far as I can see you pinpointed the problem correctly as you wrote in your comment:
I believe it is because the string where opens the main report and
sets the filter there, but the sub reports are not bound so open
unfiltered
Because the subreport are unbound you need to give them a complete recordset and not just a WHERE clause. Like this:
dim whereClause As String
dim strRecordset as String
'***Build your where clause
strRecordset = _
"SELECT yourColumns " & _
"FROM yourTables " & _
"WHERE " & whereClause
Me.subreport.recordset = strRecordset
Me.subreport.Requery
Requery might be optional.
Still a beginning access programmer here. Trying to get the report to work. Here is what I am doing
I first created a report using the Report Wizard using the following query as input
SELECT EmployeeId, Project, StartDate
FROM Tasks;
I have a form wherein I select the employee-id. I want to filter the report based on the employee id selected. Here is what I have for invoking the report
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = " & strempid
qryEmpReport is the name of the query that holds the report query that I mentioned above. The strempid holds the value that was selected in the form. However when I get around to execute this, it prompts me to enter the employee id again. Any ideas as to why I am getting this? I have validated to make sure that the strempid does contain the value selected earlier.
I'll guess Tasks.EmployeeId is text datatype. If my guess is correct, add quotes around the value you supply for EmployeeId:
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = '" & strempid & "'"
Based on our trouble-shooting exercise in the comments, I think you should give yourself an opportunity to examine the actual string value you're giving to OpenReport for its WhereCondition argument. (It's better to view the actual string instead of trying to imagine what it looks like.)
Dim strWhereCondition As String
strWhereCondition = "EmployeeId = '" & strempid & "'"
Debug.Print "strWhereCondition ->" & strWhereCondition & "<-"
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", strWhereCondition
View the output from Debug.Print in the Immediate window; Ctrl+g will take you there.
I have a form which allows the user to edit the properties of a filter via some combo boxes, then open a report. The report is opened with
DoCmd.OpenReport rptName, acViewReport, , whereClause, acWindowNormal
'whereClause = "Building = '005'" for instance
Some reports open fine, by which I mean they populate with the filtered info. Others, however, even though they are based off the same in-house template, IGNORE the filter all together and display a report based on ALL data (not the filtered data).
Why would a report ignore the filter? When I edit the reports in design mode after opening them with the form:
Working Report | Non Working Report
Filter: Building = '005' | Filter:
Filter On Load: No | Filter On Load: No
What could be causing the non-working report to not register the filter argument? There's no On Load VBA, nor any VBA, in these reports (except for an Export to PDF and a close button which is copy-paste for each report).
EDIT
Should have checked before, this happens regardless of whether or not the query driving the report is empty (ie the filter is never applied to some reports, regardless of blankness)
Not sure if the code will help, but:
Private Sub btnOpenSummary_Click()
If IsNull(Me.cboSummary) Then
MsgBox "Please select a building for the SUMMARY report."
Exit Sub
End If
strCrit = "Building = '" & Me.cboSummary & "'"
MsgBox strCrit
survArray = getSurveyArray()
For Each Survey In survArray
DoCmd.OpenReport Survey, acViewReport, , strCrit, acWindowNormal
Next Survey
DoCmd.OpenReport "Total Summary", acViewReport, , , , Me.cboSummary
End Sub
My fault.. there was code which had for some reason been linefed all the way down the code page and out of view. There was an On Open which played with the Control Source. It ended up being useless (as the control source only needed to be set once, not every time) but it was disabling the filter for some reason. Anybody else who may have this problem:
Make sure the control source is not being altered in your VBA for the report.
Thanks to those that helped, sorry my information was wrong.
My OnOpen code:
Private Sub Form_Open(Cancel as Integer)
Me.RecordSource = Me.Name
End Sub
It takes the name of the report, which corresponds to a name of a query, and puts it as the recordsource. (I have about 40 reports done this way, so it's dependent on names to make it fast to duplicate for different items).
Removing this made it work perfectly using Access 2010. Not sure if this was a problem specific to my setup or what, but, this change directly fixed it.
This is not a direct solution, but rather a workaround which should almost certainly work. Instead of applying filtering to the report, dynamically change the report data source by passing the where clause as a parameter.
To open the report use:
DoCmd.OpenReport rptName, acViewReport, , , acWindowNormal, whereClause
Note that the whereClause string is being passed as the OpenArgs parameter.
Then in the report VB:
Private Sub Report_Open(Cancel As Integer)
On Error GoTo ReportOpenError
If Not(IsNull(Me.OpenArgs)) Then
Me.RecordSource = Replace(Me.RecordSource,";"," WHERE " & Me.OpenArgs & ";")
End If
Exit Sub
ReportOpenError:
MsgBox "Unable to open the specified report"
Cancel = 1
End Sub
This solution assumes the report RecordSource is defined as a semicolon terminated SQL query (not a query name) and the record source does not already contain any WHERE, GROUP BY, etc., clauses. In those cases, it may be easier to redefine the query from scratch.
This problem may also occur when a report is copied and re-purposed. I had a report that was working fine then made a copy of it and was no longer able to filter it.
Perhaps there is a glitch in Access that causes it to ignore the filter when the report is copied under certain circumstances.
Solution: Instead of copying and re-naming the report, try creating a new report, linking the data source, and copying the fields back into place. If you are dealing with a new report, try re-creating it.
I've been stuck with this issue for quite a while so help will really be appreciated...
I have a report based on a query that shows machine utilization for a list of machines. Before loading, the report prompts dialog boxes to enter the parameters for the query: sDate and eDate (start & end dates respectively).
Each record in that report has a command button that opens another report that specifies machine errors for the single machine selected.
The new report is based on a query that receives the parameters sDate, eDate and mCode (dates and machine code).
I'm trying to send the parameters from the first report to the second one so that when I open the new report, I won't have to enter anything.
I've tried different methods to send the paramaters by editing the command button's click event in vba.
startdate enddate, machinecode are invisible textbox fields in the first report with the values I need.
DoCmd.OpenReport "rpt_MachineBreaks", acViewReport, , "sDate=" & Me.startdate &
" AND eDate=" & Me.enddate & " AND mCode=" & Me.machinecode
But the dialog boxes prompt me to enter sDate, eDate and mCode
Another approach was to modify the query parameters for the new report using QueryDefs
Dim db As Database
Dim qry As QueryDef
Set db = CurrentDb
Set qry = db.QueryDefs("qry_singleMachineBreaks")
qry.Parameters("sDate") = Me.startdate
qry.Parameters("eDate") = Me.enddate
qry.Parameters("mCode") = Me.machinecode
DoCmd.OpenReport "rpt_singleMachineBreaks", acViewReport
Again, the dialog boxes prompt to enter sDate, eDate and mCode...
Help Please!!!
A common solution in Access for sharing parameters is to use a form. This allows you control of user input and can even be hidden, once the correct parameters are obtained. It is not difficult to refer to a form in a report or query:
SELECT Field1, Field2, ADate
FROM ATable
WHERE ADate = Forms!AForm!txtDate
You could remove the parameters from your underlying queries, and have your criteria defined in the Where clause of the DoCmd.OpenReport.
Another solutions is to use the OpenArgs param of DoCmd.OpenReport, and retrive that value in the OnOpen event of the report, using Me.OpenArgs.