Assigning Values to Controls and Properties at Run Time

By assigning values to controls and properties on a form or report while your application is running, you can make your forms and reports more responsive to your users’ needs.

See Also   To find out whether you can set a particular property at run time, search the Help index for the name of the property.

For example, after a user selects a customer for an order, you may want to fill in the controls that contain the shipping address with the customer’s address. You do this by assigning values to the shipping controls. Or you may want to disable some controls on the form in response to values the user enters in other controls. You do this by setting the Enabled property of each control to False when the user enters the values.

In a macro, use the SetValue action, as shown in the following illustrations.

If the control to which you assign a value is located on a form or report other than the one that runs the macro, you must enter its full identifier as the Item argument.

If you want to use Visual Basic to assign a value to a control or property, use a simple assignment statement. To refer to a control that is located on the form or report from which you’re running the event procedure, you don’t need to use its full identifier.

ShipCity = City 
Details.Enabled = False

To refer to a control on a different form or report, use its full identifier.

Forms!ShipForm!ShipCity = City

Note   Although property names with more than one word appear in the property sheet with spaces between words, you must concatenate them when you refer to them in Visual Basic. For example, to refer to the RecordSource property, which appears in the property sheet as Record Source, you would use the following line of code:

Forms!Orders.RecordSource = "OrdersQuery"
Example
Disabling a Command Button at Run Time

As described previously in this chapter, the Orders form in the Orders sample application includes a command button named Details that opens the ProductsPopup form. This pop-up form shows details about the current product in the Orders subform. The Details button works fine as long as the ProductID field in the current record of the subform contains a value. But if the user clicks in the new record of the Orders subform or starts a new order, then the wherecondition argument in the button’s DoCmd.OpenForm statement returns no record, and the ProductsPopup form opens with no record in it. To avoid having the ProductsPopup form show no information, you can disable the Details button when there’s no value in the ProductID field for the current record of the subform, and enable it when there is a value in the ProductID field.

For example, the Current event of the Orders subform occurs when the Orders form first opens, every time the focus moves from one record to another on the subform, and every time you requery the subform. You can write an event procedure for the Current event that disables the Details button when there’s no value in the ProductID field for the current record of the subform, and enables it when there is one.

Private Sub Form_Current()
	If IsNull(ProductID) Then
		Me.Parent!Details.Enabled = False
	Else
		Me.Parent!Details.Enabled = True
	End If
End Sub

The statements in this event procedure use the Parent property of the Orders subform to refer to the Orders form. If there’s no value in the ProductID field in the subform, the first statement sets the Enabled property of the Details button on the Orders form to False (the same as No in the property sheet). If there is a value in the field, the second statement sets the Enabled property to True (the same as Yes in the property sheet).

When you use the Parent property to refer to the main form from an event procedure attached to a subform, the code runs only when the main form is open. If you open the subform by itself as a separate form, the code that refers to the main form doesn’t work.

Note   As described previously in this chapter, the Form_Current event procedure for the Orders subform also contains code that updates the record values in the ProductsPopup form to match the current record in the subform.