access 2010 expression builder - ms-access

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

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

Using a calculated field value from a form text box in a query

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.

Make default value on textbox triggered when other field change

So I have some textbox on my form,and on the last textbox, I turn off the enable so instead of asking the user for the input, I want to make the value on the textbox calculate automatically when the user input on another textbox, so I set the default value like this :
=IIf(IsNull([jmlh]),0,[jmlh]*[harga]*IIf([diskon]>0,[diskon],1))
But the problem is the expression just work when the form loaded, it's still not when value in "[jmlh]" change. Is there any method or something to make it possible?
Don't put your expression into Default Value, but into Control Source property.
Then it will always update itself when one of the used fields changes.
If it is a bound field (but why would you want a calculated field stored in the table?), you need to put that formula into a VBA function
Private Function UpdateCalcField()
Me.calcField = IIf(IsNull([jmlh]),0,[jmlh]*[harga]*IIf([diskon]>0,[diskon],1))
End Function
and call that function from the AfterUpdate events of all involved fields.

set Access database table field with form check box

I am trying to set the value of a number field on a table using the value of a bound checkbox on a form. The value needs to be 40 if checked and 0 if unchecked. I'm new to this and have no idea where to start.
No, the checkbox was added to a form and is bound to a number field in the table. The data is imported from an antiquated system that uses codes that most users would not understand, so if it says 40 in the table I just want the form to show checked, all other values should show unchecked, and I want the user checking/unchecking the checkbox to update the table accordingly.
The checkbox is to indicate whether or not a customer has provided direct debit information. 40 would mean they have, anything other than 40 would mean they haven't.
One way do this is using VBA in Access. The way you access VBA console in Access, is when in your form's design view, right-click the checkbox box control and choose "Build Event" from the menu. Make sure your control's Triple State is set to "Yes" in the property for the checkbox in the form's design view. Also, be certain that the checkbox's Control Source is pointed to the field in the table where you're putting the data.
Please note that prior to doing this, it is helpful to prefix your control's name... something like chkDebitInfo... "chk" stands for check box. You can change the name in the property sheet for the control, by clicking on the Other tab, and simply change the default name, which might say something like Check625. Also, you want to set the table field's data type to an "Integer" so it holds the value.
Once you're in the VBA console, after having chosen Build Event... you're presented with the control's sub procedure. I've done a sample for you:
Private Sub chkDebitInfo_Click()
If chkDebitInfo.Value = True Then
chkDebitInfo.Value = 40 // Sets the checkbox to 40 if checked
ElseIf chkDebitInfo.Value = False Then
chkDebitInfo.Value = 0 // Sets the checkbox to 0 if un-checked
End If
End Sub
I've commented the statements that set the checkbox values if checked or un-checked, so you don't have to add those. Just substitute the control name for your's, or use the default name if you haven't changed it, and you're good. Then when the check box is checked or not checked, you'll see the values populated in the database table.
Alternatively, you can set the checkbox to default value for when the form loads, but you haven't stated it, so I won't go into it. Hope this helps. Let me know if I need to expound further.

MS Access Form Insertion Hidden value

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.