Base the value of a control on another control

Microsoft Office Access 2003

  1. Create a macro.
  2. In a blank action row, click SetValue in the action list.
  3. Set the Item argument to the identifier for the control whose value you want to set, and then set the Expression argument to the identifier for the control whose value you want to assign to the first control.

    Assign the value of one control to another

    Callout 1 These arguments assign the value of the City control to the ShipCity control.

  4. Click Save Button image to save the macro.
  5. Run the macro.

Note  Depending on the context, you might need to use qualified identifiers for the controls. For example, instead of [ShipCity], you might need to use the action argument Forms![ShipForm]![ShipCity].

ShowSet the value of a control based on the value of another control by using Visual Basic for Applications

  1. Create a Visual Basic procedure.

    If you want to set the value of control when a particular event occurs, create an event procedure.

    ShowHow?

    You can set an event property for a form, report, or control to [Event Procedure] to run code in response to an event. Microsoft Access creates the event procedure template for you. You can then add the code you want to run in response to the particular event.

    1. Open a form or report in Design view.
    2. Display the property sheet for the form or report, or for a section or control on the form or report.
    3. Click the Event tab.
    4. Click the event property for the event that you want to trigger the procedure. For example, to display the event procedure for the Change event, click the OnChange property.
    5. Click Build Button image next to the property box to display the Choose Builder dialog box.
    6. Double-click Code Builder to display the event procedure Sub and End Sub statements in the form module or report module. These statements define, or declare, the event procedure.

      Microsoft Access automatically declares event procedures for each object and event in a form or report module by using the Private keyword to indicate that the procedure can be accessed only by other procedures in that module.

    7. Add the code to the event procedure that you want to run when the event occurs. For example, to produce a sound through the computer's speaker when data in the CompanyName text box changes, add a Beep statement to the CompanyName_Change event procedure, as follows:
      Private Sub CompanyName_Change()
          Beep
      End Sub
      										

      The event procedure runs each time the Change event occurs for the object.

  2. Add an assignment statement to the procedure by typing the identifier for the control whose value you want to set, an equal sign (=), and the identifier for the control whose value you want to assign to the first control.

    To refer to the value of a control on the current form, type the Me keyword followed by the ! operator and the name of the control. For example, the following statement assigns the value of the City control on the current form to the ShipCity control on the current form:

    Me![ShipCity] = Me![City]
    						

    To refer to the value of a control on a different form, type the control's full identifier. For example, the following identifier refers to the ShipCity control on the ShipForm form:

    Forms![ShipForm]![ShipCity]
    						
  3. Run the procedure.