I have a subform sfrmMySubForm including an unbound field called CountRows in the footer, which is set to =Count(*). The subform is displayed in table view. I dynamically change the filtering of sfrmMySubForm by VBA Code. This works fine.
After the filter has been updated, I like to change the height of the subform according to the number of rows shown now. I wrote a function to do that, using Me!sfrmMySubForm!CountRows to get the information. This works fine, if I trigger the function by button, but if I call it after the filter-update code, Me!sfrmMySubForm!CountRows returns 0 - apparently the function has been called, before the subform calculated the new value of CountRows (took me some hours to find out).
My Question:
How can I tell my function to wait until the subform finished it's calculations? Or is there another way to get the number of rows displayed in a subform after a filter update - another way, that doesn't lead into the same problem?
I already thought of just looping around until Me!sfrmMySubForm!CountRows > 0 but this won't work if there is really no data to be displayed...
Related
I have a subform (continous mode) in my main form which is used to add some articles with description, amount and price. In the footer of this sumform is a textfield which sums up all the entries of the subform by using =sum([amount] * [price]). That works so far.
Each time when I add a new entry in the subform, this textfield gets calculated. Now I also want to have this sum in a textfield of my main form (which actually also is not a problem). In "AfterUpdate"-event of my combofield in the continuous form I write the value of the sum-textfield to my textfield in the mainform. This initially also worked perfectly.
But now (I have no clue what changed), my textfield in the mainform doesn't get the new value, because the vba code runs faster than the calculated textfield in my subform-footer calculates its new value. It always writes the old value to the mainform. I discovered the problem using brakepoints in my code. So the old value gets written to the mainform before access is finished calculating the sum of all subform-entries.
Here's the code which I use to write the value from my subform textfield to my mainform textfield:
Private Sub txt_Article_AfterUpdate()
Me.Refresh
Me.Parent!txt_Amount = Int(Me.txt_SumOfAmountsInContinuousForm * 20 + 0.5) / 20
End Sub
I hope the description of my problem is somewhat understandable. It's a bit difficult to describe it :)
Is there a way to wait with my vba code until the sum-field has its new value calculated?
Thanks
I am working in Access 2007 with a subform that shows the Mass of a determined set of records linked to a master form.
The form is displayed in spreadsheet view and has two columns [Mass] and [Include]. Include is a True/False field to select whether the record is to be included in the final calculation.
In the footer of the subform, I want to add the average of the masses of the records where [Include] = True.
So far I have created a new field [MassIF] = IIf([Include]=True,[Mass],Null), this works correctly in the spreadsheet.
However, the =Avg([MassIF]) control in the footer gives an error, any idea why that would be and how to get around the issue?
Also, =Avg([Mass]) works fine until I add to the footer the =Avg([MassIF]) control, then I get #ERROR in both fields, which looks really odd to me.
Try calculation in textbox in footer:
=Avg(IIf([Include], [Mass], Null))
I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).
It's been awhile since i've worked in VBA. I have a bound form. It has two drop down lists. One list is bound the other not (the first ddl is a list of values. The second gets refreshed when the first one changes, using the value of the first to create a query for the second. That value is used as a fk in the table the form is bound too).
Anyway, when the form is first run and uses the default value for ddl 1, if the second combobox is empty, and I try to get the value, it's null, which is what you would expect. But, I have code that runs when ddl1's value changes, to requery ddl2. When it requeries, if the list is empty, and I do combobox1.value, instead of being null, the value is 1. This is confusing, because, since the list is empty, I would think it should be null. What's going on here? Here is what I have:
Combo1 is bound to a table
Combo 2 uses this query:
SELECT tbl_office.id, tbl_office.office_name
FROM tbl_office
WHERE (((tbl_office.otherTable_id)=[Forms]![dlg_addDivision].[Combo1]));
On Combo1 afterUpdate event:
me.Combo2.requery
So, after combo1 afterUpdate, the above sql gets called. If this produces an empty dataset, and I try to get the value of combo2, even though the list is empty, the value says it's 1
thanks
The requery requeries the List - not the value. The value stays whatever it is/was before. Its not a bug - its a feature ;-)
If you page through datasets that are already filled you wouldn't want them to be changed without user interaction. The List is just a helper for dataEntry (and validation - if you check "only listitems allowed") - but it has nothing to do with your dataset.
By setting the value to your first Entry like you proposed: Me.Combo2.Value = Me.Combo2.ItemData(0) you change the dataset intentionally. And that is how it is supposed to happen. Not via changing the list.
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