Using a calculated field value from a form text box in a query - ms-access

I have a calculated text box on a form. How can I use that value in a query? The value is based on many other calculations done on the form as well.
I was hoping I wouldn't have to re-do all the calculations in the query.

Providing the form remains open when the query is evaluated, you can reference the value of any control on the form using the following syntax in your query:
Forms![Your Form Name]![Your Control Name]
In the case of referencing the value held by a control on a subform, consider that the subform is just another control on the parent form, and so the chain of references becomes:
Forms![Your Form Name]![SubForm Name].Form![SubForm Control Name]
You can test the value obtained by simply creating a new query in Access with the SQL code:
select Forms![Your Form Name]![Your Control Name] as FormValue
When run, this will yield a single record displaying whichever value was held by the control Your Control Name on the open form Your Form Name at the time of execution.

Related

Setting properties for combobox in the form.open event Access 2019

I have the following piece of code located in the open event of a form.
If GetUserName = "Bob" Or GetUserName = "Ned" Then
Me.cbo_Position2.LimitToList = False
Me.cbo_Position2.AllowValueListEdits = True
Me.cbo_Position2.ListItemsEditForm = frm_MasterDropDown_lookup
End If
I'm using this code to set who can edit a combo box list. The combo box is bound to a field on the form and the combobox info is in a separate lookup table. What I want to know is whether or not these properties can be set as the form opens.
When I run this code, it executes with no errors but the properties do not change. I also tried doing 'Me.Refresh' at the end but that did not help. My assumption at this time is that they can't be "set on the fly" so to speak. If not, I was wondering if there is something I'm missing to get this to work.
TIA,
Tim
From Access help:
1.If you set the combo box's BoundColumn property to any column other than the first visible column (or if you set BoundColumn to 0), the LimitToList property is automatically set to Yes.
2.When the LimitToList property of a bound (bound control: A control used on a form, report, or data access page to display or modify data from a table, query, or SQL statement. The control's ControlSource property stores the field name to which the control is bound.) combo box is set to No, you can enter a value in the combo box that isn't included in the list. Microsoft Access stores the new value in the form's underlying table (table: A database object that stores data in records (rows) and fields (columns). The data is usually about a particular category of things, such as employees or orders.) or query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form or report.) (in the field specified in the combo box's ControlSource property), not the table or query set for the combo box by the RowSource property. To have newly entered values appear in the combo box, you must add the new value to the table or query set in the RowSource property by using a macro or Visual Basic event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) that runs when the NotInList event occurs.
Example for setting property:
Forms("Order Entry").Controls("States").LimitToList = False

MS Access using ApplyFilter from Macro Builder with combobox shows input parameter box

This time I'm trying to work on an MS Access application. I have a split form populated using a SQL query. Now I want to filter this form using a combobox which is located in the header of the form. This CB is also populated with a SQL query:
SELECT DISTINCT [ConsultQ].[ClientName] FROM ConsultQ;
I have added an embedded query to this Combobox which should filter the form. The values shown in the Combobox are correct. But when I select a value from the box a popup show which asks me for input.
The ApplyFilter action is set to:
So, apparently, the ApplyFilter action cannot retrieve the selected value of the Combobox. What am I doing wrong here?
When I enter a name in the input box, the filter is applied correctly. So the filter works, but I cannot set the filter using the selected combobox value.
It must be something simple, but I cannot find it.
I'm using MS Access Office 365 version.
Remove the [Text] property. You want [Value] and [Value] is the default so doesn't have to be explicitly referenced.
Also need full path reference to combobox.
Forms!yourformName!cboClient
However, really should use ClientID to filter records. If the combobox has ClientID as first column and first column is set as the BoundColumn, then combobox value is ClientID, not ClientName.

How to enable filtering on a field defined by DLOOKUP()?

I have a form TForm based on table T, set up as a datasheet. My goal is to add a filterable column to the datasheet where the column's value is calculated from a query using another column's value.
I tried to do this by adding a text box currentBox to T. The control source for currentBox is:
=DLookUp("name","currentStatus","itemID=" & [ID])
where [ID] is a field in T and currentStatus is an aggregate query on a table that T is related to.
I can filter on all the fields in TForm that are in T. But I can't filter on currentBox, even though it also appears as a column in the form; clicking on the column header doesn't do anything.
I'm guessing the problem is that currentBox is not bound to a field in T; is there a way to work around this?
Here's a VBA solution:
Add a combo box (aka drop-down) object to your form header. This drop-down's source will be an independent query that displays all the values your Dlookup() currently pulls (names?) and stores the itemID. Let's call it ObjPickName in this example.
Add an AfterUpdate event to ObjPickName that will filter your form for you (your form will still be based on T). The code will be something like:
Private Sub Combo_ObjPickName_AfterUpdate()
Me.Form.Filter="[itemID]='" & Me.Combo_ObjPickName.Value & "'"
Me.Form.Filteron=True
End Sub
The way that I ended up solving this was to add a field to T, and have that field updated during the AfterUpdate() event with the value from the DLookup() call. Because the field is now no longer query-based, it can be used to filter the form.

Bind Textbox To a Field

I have a form in MS access. I have created a few textboxs in that form. I have input a function in the control source property of a particular textbox(TOTAL). This basically sums up numbers entered by the user and displays it in the textbox.
The problem is the textbox is unbound. I want to bind the textbox to a field in the table using vba code. I could have just bind the textbox by the putting the table field name in the control source property but it is occupied with the SUM() function.
I not sure how to proceed here. I have tried searching up on the web for several days but failed to gather a reasonale solution.
Please help
You need to remove the sum function and put that in a suitable event for the form or event for another control and then bind the textbox.
As a general rule, you should not be storing calculated values, it is against the rules for normalization.

access 2010 expression builder

I want a text box to contain data which is a calculation based on 2 other control field values - only if it's value is null (ie the current value of the column in the database is null).
So I entered =([control1]*[Control2])/1000 in the expression builder for the default value property - however the result always shows the textbox to be empty (even tho control2 and control2 contain values).
How can I achieve this? Can such an operation only be done in code-behind ie VB??
thanks,
KS
I think you're talking about a control bound to a field in the form's record source. And when the underlying field is Null, you want the control loaded with your calculated value.
If that interpretation is correct, you can do it from the form's On Current event.
If IsNull(Me.txtYourTextBox) Then
Me.txtYourTextBox = (Nz(Me.control1) * Nz(Me.Control2)) / 1000
End If
That will load the computed value into the text box, allow the user to change its value if desired, and store the value to the bound field when the record is saved.
If the bound field is not Null, its value will be displayed in the text box without alteration by the On Current code.
Is that what you want?
To accomplish this using VBA, add a Form_Load Event. (Open the form in Design View and in Form properties click the Event tab and choose Event Procedure for "On Load" and click ...)
This example uses [TextField] to refer to the table data.
Private Sub Form_Load()
TextControl.SetFocus
If IsNull([TextField]) Then
TextControl.Text = ([Control1] * [Control2]) / 1000
End If
End Sub