In Access 2003 Report, how to hide some detail rows? - ms-access

I'm working up an MS Access report. This report is grouped and has a details section. But some of these details rows don't need to be shown. How do I hide the details sections if, for example, "[fieldx] = 'foo'"?
(In Crystal Reports, there's a place where you can write custom criteria to suppress these kinds of things. I can't find an equivalent functionality here in Access).

As David suggested, you could pass WHERE condition to the report:
Function OpenReport(filterValue As String)
DoCmd.Close acReport, "ReportName"
DoCmd.OpenReport "ReportName", acViewReport, "", "[SomeFieldName]<>'" & filterValue & "'", acNormal
End Function

Related

Access Do.Cmd OpenReport is only printing?

Access 2010 - OpenReport in vba is only printing.
I have a simple modal form where the user selects a date range and the report opens. If the query results are 0, a message pops up saying there are no values, else the report opens and modal form closes. However, every time I run this it will not open in the report view it will only print. I can change it to design and print preview and those all work just not view.
I have been trying to figure this out with no avail and its driving me nuts. What am I missing?
Private Sub Command5_Click()
If DCount("*", "qryalltime_filtered") = 0 Then
MsgBox "No records to display based on the date parameter provided"
Else: DoCmd.OpenReport "rptAllTime", acViewReport
DoCmd.Close acForm, "frmAdmin-Employee"
End If
End Sub
If you wish to preview:
DoCmd.OpenReport "rptAllTime", acViewPreview
I just want to see it in the report view and not preview. I wrote the line again (like the 5th time today after restarting my machine) and I got this to work:
DoCmd.OpenReport "RptAllTime", acViewReport
I have no idea why it worked after trying so many times. Ugh, Microsoft....
I suggest to use this command :
DoCmd.OpenReport "rptAllTime", acViewReport, , , acWindowNormal

Access msgbox Yes No to Choose Report - Opening Wrong Report?

I set up the following simple code, to choose if the report should show "excluded" groups or not.
Private Sub cmdRptWorktypesByGroups_Click()
Dim rptExclYN As String
rptExclYN = MsgBox("Would you like this report with excluded groups (Yes)" & vbclrf & " or without excluded practice groups (No)?", vbYesNo, "Choose Report With or Without excluded groups")
If rptExclYN = Yes Then
DoCmd.OpenReport "rptWorkTypebyGroups", acViewReport
Else
DoCmd.OpenReport "rptWorkTypebyGroupsNoExcl", acViewReport
End If
End Sub
That is my code. It should be totally straight forward. Problem is, that when the user clicks Yes, it opens the rptWorkTypebyGroupsNoExcl report. When I click No, it still opens that same report. Can anyone tell what I might be doing wrong?
I originally had the rptWorkTypebyGroups report, with a query record source. I copied that report, renamed the copy as rptWorkTypebyGroupsNoExcl, and saved it's Record Source query as it's own name. I can't tell why the message box isn't opening the right report. Can anyone please help?
Thank!
The MsgBox() function returns a VbMsgBoxResult value (an Integer), not a string.
Since you apparently don't have Option Explicit on, Yes is created as new empty Variant, so your If condition will always be false, no matter what you selected.
Put Option Explicit at the top of each module.
It enforces variable declaration and reports undeclared or misspelled variables/constants at compile time.
To have this automatically in new modules, set the Require Variable Declaration option in the VBA Editor.
Correct code:
Private Sub cmdRptWorktypesByGroups_Click()
Dim rptExclYN As VbMsgBoxResult
rptExclYN = MsgBox("Would you like this report with excluded groups (Yes)" & vbCrLf & " or without excluded practice groups (No)?", vbYesNo, "Choose Report With or Without excluded groups")
If rptExclYN = vbYes Then
DoCmd.OpenReport "rptWorkTypebyGroups", acViewReport
Else
DoCmd.OpenReport "rptWorkTypebyGroupsNoExcl", acViewReport
End If
End Sub
Edit: It's vbCrLf, not vbclrf.
Another one that Option Explicit would have caught.
I'm a moron. I was having it say if rptExclYN = Yes and not rptExclYN = vbYes. My bad. Here's the simple code that works:
Dim rptExclYN As Variant
rptExclYN = MsgBox("Would you like this report with excluded groups (Yes)" & vbCrLf & _
" or without excluded groups (No)?", vbYesNo, "Choose Report With or Without Excluded Groups")
If rptExclYN = vbYes Then
DoCmd.OpenReport "rptWorkTypebyGroups", acViewReport
ElseIf rptExclYN = vbNo Then
DoCmd.OpenReport "rptWorkTypebyGroupsNoExcl", acViewReport
End If
Hope this helps someone else, because then it would mean I'm not the only one missing simple things.

pass parameters to record source with query Access2007

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]

How to get the ID of the row a command button is in (in MS Access Detail Section)

I have an MS Access 2003 form with a datasheet detail. I want to add "Print" button to each row which will open a detail report for that row.
Inside the Click handler, how do I access the ID of the row the button was in so I can pass it to the report?
Private Sub btnPrint_Click()
DoCmd.OpenReport "rptPrintDetails", acViewPreview, "", "Id=" & '<==== what goes here
End Sub
Use the Value property of the ID control on the form . Or, since the Value property is the default property, you can just use the name of the control.
For example, if you have a textbox control named "txtID", your code would say
DoCmd.OpenReport "rptPrintDetails", acViewPreview, "", "Id=" & Me.txtID

Access Report not Filtering when Others Are

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.