How to make a total column that updates automatically in Access. - vba

In access I am trying to get my total value to be equal to the sum of the all of the different values where the order number is the same.
My current code is as follows;
= SUM( Forms![OrderLine]![Total] ) Where( Forms![OrderLine]![OrderNo] = [OrderNo] )
This however gives me an error. How else would this be done?
Thanks

It sounds like you have an Order form and an OrderLine subform. Add a Form header/footer to your OrderLine form and put a text box in the footer. Put a SUM function in the Control Source of the footer text box to generate the aggregated value you need from the order line item rows.
On your main form, set the Control Source of the text box you want to display the order total equal to the name of the invisible text box in the subform. The control source on the main form text box will resemble SubformControlName.Form.AggregateTextBoxName.
You may need to play with the visibility settings of the header/footer, text box, form view mode, etc. to get the presentation you want, but this works even with the subform in datasheet view. The key is to get a text box somewhere on the subform to calculate the value you need on the parent form and then reference it inside the subform control from the parent form.

Related

My query, getting value from a textbox inside a subform, shows "small box"

The overall scenario: Actually trying to implement an order entry
system. A button on the main form opens 2nd form. In the 2nd form,
there are some combo-boxes and list-boxes from which the user selects
an item. Then pressing a button add that item with append query.
The problem is, as soon as I click anywhere in the 2nd (child) form;
the the id (autonumber) from parent vanishes(becomes null) for new
entry in the 2nd form. (I have lately discovered this vanishing by
using a text box to show current parent-id. Also found a solution,
posting as an answer)
I am trying to get two values from a textbox and a combo box. In the query datasheet view, i checked that, one value is received correctly but the other value it shows a very small box (putting picture below)
)
In this picture, "Expr2" field is showing small box. I used below statement to pull value from subform:
Expr2:[Forms]![customer_f]![products_add_subf].[Form]![customer_id]
And here is the code in sql-view:
INSERT INTO products_t ( product_name, customer_id )
SELECT [Forms]![customer_f]![products_add_subf].[Form]![item_combo] AS Expr1, [Forms]![customer_f]![products_add_subf].[Form]![customer_id] AS Expr2;
What this small box mean and how to avoid it ? How to pull correct value instead of small box ?
The solution will prevent vanishing of parent-id from first form(parent); in the 2nd form (child).
Just disable "Allow Addition" in the 2nd form's property.
Steps:
Open the 2nd form in "design view" . Double Click at the upper left corner of the form to open its property sheet. Goto "data" tab. In the "Allow Addition" select "no".
Now your append query will be able to grab the parent id field. It will not vanish !

What code to use for Access VBA to allow a form to calculate a text response?

I have a generic access database and would like to get the most out of it.
I have created a form (formData_Header) that contains a sub-form (formData_Sub). The sub-form contains text boxes for items to be inventoried, along with data (numbers) from scans that correspond with these items. Each item will have 2 text boxes (count1 and count2) The header of the form contains text boxes that will be applied to all boxes in the sub-form, such as the values from instrument backgrounds. The background will have 2 text boxes (bkg1 and bkg2).
If I haven't lost you yet, I am wanting to take the background values from the header and the count values from the sub-form and calculate a text response in an other text box or Combo Box located in the sub-form.
i.e.: If bkg1 + bkg2 > count1 + count2then the output is "FAIL"
If bkg1 + bkg2 = count1 + count2then the output is "RE-evaluate"
All else output is "OK"
I have tried to do this multiple ways and each time, I get a random error, or the code does nothing.
Create a custom function that is called from the after update event on each control.
It needs to first check if each control/param has a value in it. If not, return the default "FAIL" value, otherwise use the calcs you show above.
If you put the function in the main form code, use this format to put the result into a subform control:
Me.formData_Sub.Controls("txtResult") = strCalculatedResult
If your code is on the subform, write the result into the control with the usual:
Me.ControlName = strCalculatedResult

Control Source of a text boxes set to DCount function - Refresh issue

I'm setting the control source of text fields to return the value of a function (multiple fields with different filtering conditions). The form has a combo box with a list of years: when the user selects a specific year, the on change event triggers a refresh of all the fields.
My problem is the fields don't show any values unless after combo box's On Change events. I have to click on the form/fields before the values start showing up.
I tried to do form refresh & field's requery but doesn't work.
The text field's Control Source is set to:
=SummaryReport("Projects","G","1",[Forms]![frmSUMMARY_REPORT]![cmbYEARS])
What I'm trying to do is when the user selects a year from a drop down, the fields values are updated & displayed by the On Change event - currently they seem to be updated but are not showing unless I click on the screen and that's when values start showing up in each field.
The method to update calculated fields is Me.Recalc (or myForm.Recalc):
https://learn.microsoft.com/en-us/office/vba/api/access.form.recalc
Try this instead of .Refresh.
Also I think the better event to use is After Update instead of On Change for a combo box.

Add a field value to text box

I have an Access Table named Count. It has one field named Anz and it has only 1 record. I want to Show this record in a TextBox on a form named overview. So in the design mode of the form inside the TextBox I use the code
[Count]![Anz]
but it Returns me #Name? error when I Switch back to form mode. Where am I going wrong?
You can use in Control Source of your unbound text box =Dlookup("[Anz]","[Count]")
Also you can bound your form to Count table and use for text box control source Anz

MS Access: text box value show #Name?

I have created a form and set the form's RecordSource to a query. Below is my query:
SELECT GeneralT.*, SalaryT.[Status]
FROM GeneralT INNER JOIN SalaryT ON GeneralT.Deposits = SalaryT.Deposits;
In the form I have 4 textboxes. In the first 3 textboxes I show value from GeneralT table and in fourth textbox I show SalaryT.[Status] value. But this fourth value doesn't show in the textbox rather it show #Name?.
If I open the query in datasheet view I can see all the value properly. I don't understand what is wrong here. Please help me to show the result properly in the form.
Displaying #Name? for a field says that field has a control source that does not match with the query linked to the form. Some things to check:
Make sure Status is a selection from one of the pre-existing options in the Control Source drop down combo box. Click on the combo box to make sure.
Double check to make sure it is a "Text Box" and not a custom
control.
Make sure there isn't another text box named Status
Try chaning the control source to just Status instead of SalaryT.[Status]. If the field name does not conflict with the naming of a field in GeneralT, the selected SalaryT.[Status] will actually be displayed named Status.