MS Access Subreport Grouping and Sorting Issues - ms-access

been pulling my hair out with this one. Searched everywhere and can't get anythign to work. Here's my scenario:
I have a report with several subreports from linked tables. I would like to group the subreports to sort on a given field based on the value of a variable called SORT_BY, and force a page break for each value., i.e. Group a subreport on ItemDescription or ItemLocation. The SORT_BY variable is set from a users prefs dialog. (ya, I'm using it like a constant but the user can change it)
Now, I want the user to be able to preview the report before they print. I have the report being launched in ReportView mode. This allows me to place a "Print" button and a "Close" button in the header of the report (which do not print). I have it set this way because the user is locked out of the normal "ribbon" and other controls so I have to provide buttons. The buttons do not show up in PrintPreview mode.
Obviously, I need to accomplish this via VBA. I can't seem to get GroupLevel settings to work. I've tried a CreateGroupLevel function to no avail. Tried with existing groups and with no existing groups. I've tried abandoning the group idea and simply tried to Sort the subreports using the OrderBy property, and nothing works. I'm going crazy.
Does anyone have any suggestions... other than giving up? :-)
UPDATE:
I've been trying the CreateGrouplevel function before opening the main report. i.e. open each subreport in design view, add the group, close the subform, then open the main report. However, when I try to implement the CreateGroupLevel function, I get the error:
Run-time error '2154':
You can't call this function when the Group, Sort, and Total Pane is open.
I don't understand this. Here is my code:
DoCmd.OpenReport strReport, acViewDesign
intGroupLevel = CreateGroupLevel(strReport, "FluorescentDescription", True, False)
DoCmd.Close acReport, strReport
Thoughts?
OOOPS!
Ok, so I figured out te '2154' error. Silly really, if you have EVER turned on the Group, Sort, Total Pane while editing a report, it will automatically be turned on every time you enter Design View on that report until you physically turn it off. Strange. Still chipping away at this.

I sure someone will correct me if I am wrong, however I don't think putting the buttons on the report will work. Why not build your own custom toolbar to display that contains Print and Close

Related

Property/ function to find empty subreports and hide them in Access report

How can I tell if a subreport is empty? Is there a property or function I can use to check whether there is any data in the subreport? I want to write a subroutine that says
If IsEmpty(subreport) Then
subreport.Visible = False
Else
subreport.Visible = True
End If
That, combined with making the subreport small and allowing CanGrow would help me display only the values that do exist without taking up space on my report if the subreport is empty. Ideally, I'd also be able to hide the label of the subreport if it's empty.
If the VBA code is going to be on the main report then you would use,
Me.subreport.Report.RecordsetClone.RecordCount
to get the number of records. If the code is in the sub-report then you only have to write,
Me.RecordsetClone.RecordCount
If you have any issues with this or need an explanation leave a comment and I'll get back to you.

Filtering in subreport

I have encountered issues with using "filtering menus" in report during development of database aplication for my employer.
For my filtering options I created buttons with OnClick Event:
DoCmd.GoToControl "MyReportField"
DoCmd.RunCommand acCmdFilterMenu
These buttons are embedded in subreport. Initial thought was to create buttons, similar to the one in ribbon, with filtering options (in final stages of development I am planning to disable/hide Access ribbon for users). I created 5 buttons, 3 for fields with data type Text, 1 with data type Date and 1 with Boolean data type.
To quote a classic, the buttons "are misbehaving." When I test their function in form in form view, the "BooleanFilterBtn" on click already doesn't display Yes/No options (or something similar), but displays 2 numeric values (like -24441 or -29696). And filter is not working whatsoever.
Another problem arises when I try to have active more than one filter at a time. Date field filter and one of the Text field filters are working when combined with another filter option. But after using one of the two remaining Text field filters, when I click any other of the filtering buttons, the FilterMenu becomes blank.
Worth noting is that right-clicking on given field in report works without issues, but not the way I want.
Given these rather odd behaviours I think I am missing something. Is there a way how to make FilterMenus work the way as familiar from the ribbon? If not, is there any other option with similar design, options that I could try out?
Additional info:
Subreport is connected with form through "MyIDField." Both subreport and form have query data source, already saved.
All field names and data types are matching.
EDIT: Also I should have written that the FilterBtns doesn't work if I open the subreport separately, they work only when I open form (with subreport in it), eventhough the FilterBtns are using only controls from the report.
EDIT2: Code behind filter button filtering Boolean data type (boolean data are in Report in form of Yes/No check-fields, as mentioned in June7 comment bellow):
Private Sub btn_FiltrGarant_Click()
DoCmd.GoToControl "Garant"
DoCmd.RunCommand acCmdFilterMenu
End Sub
When I change this code to:
Private Sub btn_FiltrGarant_Click()
Me.Garant.SetFocus
DoCmd.RunCommand acCmdFilterMenu
End Sub
Brings no effect, only that the loading time of actual FilterMenu takes a bit longer.
Example for Text data type field:
Private Sub btn_FiltrRzh_Click()
DoCmd.GoToControl "ZkracenyNazev"
DoCmd.RunCommand acCmdFilterMenu
End Sub
Changing code in the same manner:
Private Sub btn_FiltrRzh_Click()
Me.ZkracenyNazev.SetFocus
DoCmd.RunCommand acCmdFilterMenu
End Sub
Brings no effect too.
May it be that the FilterMenu is not meant to/available to be used in reports? Or may it be disabled in default settings of Access?
Because also the situation, that if I open the Report separately (not in form as a subreport, but just as report by itself in report view), using any of the FilterBtns gives me RunTime error 2046, that the action GoToControl is not available. Changing to SetFocus eliminates the error, but the button doesn't bring up any FilterMenu at all. But these issues are valid only if I open report separately (which is not my interest), but might be helpfull as information.
Right now, I don't know where the problem could be. Anything I could think of was data type/naming mismatch, but that's not the case.
FINAL EDIT: After almost a month (there were holydays) of trying and searching for a way how to make this setup work I decided to switch from subreport to subform, in which I will try to obtain the same functionality.
Maybe just reports are not supposed to be handled the way I wanted to.
Just out of courtesy I am putting June7 answer as verifiedfor the time spent on helping me.
Thank you.
Don't know why your yes/no field displays 2 numeric values like -24441 or -29696 in the filter menu. Regardless, not able to get the filter menu to act on a yes/no field at all. However, get around that with an expression in report RecordSource query that changes the Yes/No field from Boolean to text values and use the calculated field in report design, like:
SELECT *, IIf([fieldname], "Yes", "No") AS IsSomethingTrue FROM tablename;
Use Me.controlname.SetFocus instead of GoToControl so the report will open independently or as a sub object.
You have to be careful how you refer to a Report object that is within a subForm container. They do not behave the same as subforms in many ways.
Even a simple refresh is actually difficult to achieve, as reports are not meant to be dynamic like a form.
To refer to it in VBA you have to use the full form syntax;
[Forms]![MainFormName].[Form]![subReportHolderName].Requery
Using the normal Me.blah.blah reference will not work with a form embedded report. Note that you refer to the container - NOT the report object.

Unfreezing columns in a subform in Access

I have a form with a subform that displays records, and the user is able to rearrange that subform and save their preferences. This means they can change the width of the columns, the order, hide columns, etc. and save their changes at any time, and those changes are applied when the form next loads. This is done by saving the preferences in a table and then pulling those preferences from the table in the subform's Form_Load event. This all works fine, but I'm trying to add the ability to save frozen columns as well. I'm able to track how many columns are frozen, and I'm also able to restore the order of the columns, so it should be a simple matter to unfreeze all of the columns and then refreeze the first X columns.
I know that I can use 'RunCommand acCmdUnfreezeAllColumns' to unfreeze all of the columns. I also know that I need the focus to be on the form, so I tried to do myForm.setFocus (where myForm is a reference to the subform) before unfreezing the columns. This gives the error "There is an invalid method in an expression". I thought maybe this was because you can't set focus to a form with enabled controls, so I tried myForm.Controls(0).SetFocus instead. This doesn't give an error, and it seems to set the focus to the first cell in the subform based on the value of Screen.ActiveControl (and the fact that the cell has the focus after the form loads). However, I then get the error "The command or action 'UnfreezeAllColumns' isn't available now." on the line RunCommand acCmdUnfreezeAllColumns. I also thought maybe I was having problems because I was trying to focus in a load event, so I tried calling the repaint method first, but that didn't make any difference. I thought about unfreezing all of the columns in the design view so that I wouldn't need to unfreeze them at runtime, but the user may not have any preferences saved and I want some columns frozen by default.
I'm running out of ideas on this. What exactly needs to happen for the command to unfreeze all columns to work? Or is there an easier way to do this that I'm missing? The solution needs to be versatile enough to work on any subform with any field names. Note that I do have a list of the field names that were present when the user saved their preferences, but I cannot guarantee that they will still all exist when their preferences are restored.
This normally worked for me:
myForm.SetFocus
RunCommand acCmdUnfreezeAllColumns
However, sometimes it failed, so I tried to repeat it:
On Error Resume Next
myForm.SetFocus
RunCommand acCmdUnfreezeAllColumns
If Err.Number <> 0 Then
myForm.SetFocus
RunCommand acCmdUnfreezeAllColumns
End If
On Error GoTo 0
That nearly always worked, but the menu commands are not 100% reliable.

VBA Access Requerying a Query that is outside of the current Form Object

I am working on a complicated project in MS Access 2007.
I am having some difficulty finding the correct method/Syntax for having a query outside of the open form be requeried. I am still fairly new at the VBA so forgive me if I make mistake or I am incorrect.
I have created a query which uses the value of a particular Combo Box in my form as part of its WHERE criteria. I have tested that and it works just fine. Now I am working on an "After Update" event for the Combo Box in question so that When I change the value of that Combo Box in question it will automatically tell my query to rerun itself with the new value in the WHERE Clause.
I was originally thinking of using the following command in VBA
DoCmd.Requery [Queries]![TestQuery]
But I am unclear on if I can use the DoCmd.Requery since the query is outside of the open form and not imbedded into it.
I am looking for options on how best to accomplish this effect, Not Strictly VBA Only. So if a Macro would work better please give me an example to work from
UPDATE
Just to make things a little clearer Here is the actual SQL Code for the Select Query that I want to requery through this after Update event.
SELECT ForcastTrans.Location, ForcastTrans.Client, ForcastTrans.Department, ForcastTrans.Account, ForcastTrans.Currency, ForcastTrans.Month1, ForcastTrans.Month2, ForcastTrans.Month3, ForcastTrans.Month4, ForcastTrans.Month5, ForcastTrans.Month6, ForcastTrans.Month7, ForcastTrans.Month8, ForcastTrans.Month9, ForcastTrans.Month10, ForcastTrans.Month11, ForcastTrans.Month12
FROM ForcastTrans
WHERE (((ForcastTrans.EntityID)=[Forms]![ReportSelect]![BusinessUnit]));
As I said before this Query works just fine by itself I just need to be able to issue an after update event which will tell this query to Rerun based on the updated WHERE criteria.
Danke.
It still matters how you're building the report. I would assume that this query is the record source for the report and that the report is only generated when you request it from this very form you're updating. In which case, the query should automatically take the updated value when you load the report; If you're looking to generate the report after you close the form, then the query won't work once the combobox is destroyed. I'm still speculating on what exactly you want to do here, but suffice it to say, I don't recommend having a stored query that depends on an object in a form.
A cleaner way of doing this is to use a WhereCondition in your OpenReport call:
(inside a button click on ReportSelect)
DoCmd.OpenReport "YourReportName", acViewPreview,,"EntityID=" & Me.BusinessUnit
This opens your report filtered by the form that opens it, but still allows the report to open showing all of the data when the form is closed.
Kevin

Calculated textbox control not showing value in Access 2007, but shows in 2003

I have an Access form with a textbox that is uses as an expression as it's controlsource to calculate a value to display. While the below expression works fine in Access 2003, it doesn't display in Access 2007 automatically. Only when I click on the field, does the value display. What is does is it checks the database for a value based on other fields on the form. If its NULL, the field displays a "N/A". If it's not NULL, it displays the value found in the database.
Does anyone know what I might be doing wrong?
=IIf
(
IsNull( DLookUp("SomeField","SomeTable","SomeField = Forms!frm_FormName!cboSomeCombobox")),
"N/A",
LookUp("SomeField","SomeTable","SomeField = Forms!frm_FormName!cboSomeCombobox")
)
Issues that are fixed in Access 2007 by the 2007 Microsoft Office suites Service Pack 1
On a Windows Vista-based computer, fields that are bound to time-intensive
expressions are blank in Access 2007
Fields that are bound to time-intensive expressions are blank in Access 2007.
These fields are empty until you click the text box or until you click anything
outside of Access 2007 if the following conditions are true:
The Control Source property of a text box uses the DLookup() function.
The query that is called by the DLookup() function references a control on
a form.
http://support.microsoft.com/kb/942378
I've run into this before. Previously I found mentions of it on a couple of different Access VBA forums after searching with Google, but now I cannot find any of these hits.
I've been fiddling with this for hours today. I tried a variety of code bits in various events, but as any of us know from running into this problem, the Report View has vastly fewer event triggers than does Print Preview. However, I did find a workaround and a couple of important discoveries regarding this irritating bug:
The TextBox need not receive focus; simply accessing the .Value property is sufficient to give it the kick in the pants that it needs to properly display its contents. As such, the .SetFocus method isn't necessary.
The Section_Paint event is sufficient for this purpose.
The following bit of code worked for me. Of course, change the section and control names to match those on your own report.
Fair warning: This causes screen flicker on my system when the report is scrolled. I did attempt to disable screen updates with Application.Echo, but this only made the flicker worse. It seemed like each and every line of code added to the event, no matter what it was, made the flicker worse; it seems to be a timing issue:
Private Sub GrpGlacctFooter_Paint()
Dim DummyVar As Variant
'DummyVar can be reused for each control. We just need somewhere (anywhere) to
'stuff each .Value. Simply accessing .Value in any way is enough to trigger the
'TextBoxes to display their contents.
DummyVar = txtAcctSubtotalMessage.Value
DummyVar = txtAcctTotalSpent.Value
DummyVar = txtAcctRemainder.Value
End Sub