I currently am using a continuous form in MS Access that the users input values into one field. In the form there are three records under one field. I'm not sure if this is possible or even how to do it, but I would like to subtract the value in the first record from the value in the second to calculate the value for the 3rd. Form in question
In the example I provided it would be 20-10 and then have the third record be 10. The field name is Initial_Value. I've been looking online and I haven't seen any information on how to grab those values to do this with VBA. The name of the form is "frm_main_process_values" and is a subform of "frm_main_msmt".
Add column with measurement type (initial, final, change, it can be hidden) and use for Value textbox with calculated control source based on function: =CalcDrop([ResType],[Result]).
And create public function in module with static variables, which remember previous values:
Public Function CalcDrop(strResType As String, lngVal As Long) As Long
Static lngInit As Long
Static lngFinal As Long
Select Case strResType
Case "initial"
lngInit = lngVal
CalcDrop = lngVal
Case "final"
lngFinal = lngVal
CalcDrop = lngVal
Case "change"
CalcDrop = lngInit - lngFinal
End Select
End Function
You will need to make sure that the rows order is the same for each series: initial, final, change. Here are results, column "Value" is calculated:
This is not possible in a continuous form, since the Value textbox has only one ControlSource, it can't be a bound field and a calculated value at the same time.
If it's always three records, and you pre-fill the form with the three records, and they have a defined order with a sorting field, you could have a button that calculates the value and writes it to the third record. But that would be a very odd usage of a continuous form.
Related
I have a textbox in a form that I created.
I want to add an expression in the textbox's Control Source property.
I also want to bind the textbox to a field in the table using Control Source propety of the textbox.
Is there any way I can add a field name and an expression in the control source property of a particular textbox?
The expression basically adds up couple of numbers and displays the output in that textbox. And I want the output to be stored in a field in the table.
You could link the text box to the table control source and then set the value of the text box to the result of the expression in the AfterUpdate property on the two objects the contain the values you want to add.
For example
me.textbox.value= int1 + int2
This value should then be written to the table.
If you have a text box named txtMy_field which is bound to a field named My_field in your form's record source, you could use the form's on current event to load the sum of those other two numbers into txtMy_field.
If you don't want to over-ride existing values stored in My_field ...
If IsNull(Me.txtMy_field) = True Then
Me.txtMy_field = Number1 + Number2
End If
If you do want to over-ride existing values stored in My_field, eliminate the IsNull() condition.
Me.txtMy_field = Number1 + Number2
If Number1 and Number2 are from other controls on your form, you could also use their after update events to update txtMy_field value on demand.
However, I'm unsure how well I understand your question. These are only general suggestions and I hope they point you to something useful. Incidentally, if those two numbers come from two different forms (as in one of your previous questions) and you intend to display and store their sum via a third form, this could be more challenging than it appears from this question.
If you have textboxes with names text1 and text2 and you will save your answer in text3
you should give this code in text3 properties Control Source:
=Val([text1])+Val([text2])
I'm a beginner at MS Access. and I have created a local database to work on;
what I want to do is the following:
If the textBox named "Number" starts with specific digits, the ComboBox value to be automatically determined to x value.
if we can replace the Combobox with just a label that automatically updates the value based on number textbox, It'd be better, aesthetically.
I want this field to be automatically counted from another list that is being input by the user himself. Instead of individually counting each approved case, it'll pull off the approved cases BASED on a specific response code "0".
I tried this solution and it just does NOT work.
=DCount("[Response code]","Information Subform","[Response code] = '0'")
So for example, if the user inputs data as the following, this field should return with "2" automatically instead of the user counting them manually based on Response Code = 0
I want to my list to perform the following function: if selected a specific value, change the context of a textbox. It should act as a timesaver to write a specific dialog or script for the user.
I have a column full of comboboxes. The comboboxes work fine on their own.
But if I right click on the column and apply a filter, e.g. sort alphabetically, the comboboxes still show with the correct entries, but I can no longer select another entry in the box. E.g. the box says "supplier", but I want to select "customer". Clicking on "customer" simply does nothing.
When I apply the filter, apparently the entire formula "refreshes" itself. Therefore I am currently trying to add some code to
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer).
However, being rather new to MS Access, I have no idea whether this is a step in the right direction and if so, what code I should have in Form_ApplyFilter in order to make my comboboxes clickable/updateable again.
The properties of the comboboxes are:
Row source type = Table/Query
Row source = SELECT Tbl_Liability.ID, Tbl_Liability.Liability FROM Tbl_Liability;
Bound column = 1
Enabled = Yes
Locked = No
And the form the comboboxes are in has ALLOW FILTERS set to YES (but no code in Form_ApplyFilter).
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.
I'm not really sure how to define this.
I have a table that has a few fields, let's call them: ID, Name, and Type.
I have a form that allows the user to add new records via Datasheet view. ID and Type is hidden, and only Name appears. I've setup a Filter that shows only records of a specific Type (i.e. Type = 2)
However, if someone enters a new record into the datasheet, the Type field does not get set. Setting a default value on the field won't accomplish what I want because I have a few Forms that are tailored based on Type, therefore, each needs to submit new records to the same table based on that type.
Is there a way to define what value it should set Type to? I guess I could capture the BeforeUpdate event, and set the value that way, and just hide the column. I was wondering if there is a way "proper" technique, though.
In the form's Before Insert event, you can examine the current Filter expression, parse out the filter's Type value, convert it from a string to a number, and finally assign that number to the hidden control which is bound to the Type field.
So, assuming that hidden control is a text box named txtType, and its control source is a long integer field:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.txtType = CLng(Split(Me.Filter, "=")(1))
End Sub
Use the appropriate type conversion function in place of CLng() to match the bound field's data type.
That approach should work if you're setting the Filter like this:
Me.Filter = "[Type] = 2"
Me.FilterOn = True
But, if you're using a different method to do the filtering, please give us details about it.