Referencing a mainform from another mainform. RRS feed

  • Question

  • I am trying to reference form A from form B using VBA in order to give form A's control a value.

    So far I've got this:

    Forms!FormName!ControlName.Caption = valueToAssign

    It's not working for me. What could be wrong?

    Thursday, April 6, 2017 7:44 AM

All replies

  • Depends on the error..

    Debug it..

    Dim frm As Access.Form
    Dim ctl As Access.Control
    Set frm = Forms!FormName 'or Set frm = Forms("FormName)
    If Not frm Is Nothing Then
      Set ctl = frm.Form!ControlName
      If Not ctl Is Nothing Then
        ctl.Caption = valueToAssign
        MsgBox "D'oh?"  
      End If
      MsgBox "D'oh!"
    End If
    Set ctl = Nothing
    Set frm = Nothing

    Thursday, April 6, 2017 7:52 AM
  • I notice that you are assigning to the control's Caption property, but you say, "in order to give form A's control a value."  The Caption property really only applies to labels.  If you want to set the control's *value*, assign to its Value property:

        Forms!FormName!ControlName.Value = valueToAssign

    The Value property is the default property of any control that can have a value, so you can also just write this:

        Forms!FormName!ControlName = valueToAssign

    Of course, all this assumes that the form is open, not in Design view, and has a control by that name.

    Dirk Goldgar, MS Access MVP
    Access tips:

    Thursday, April 6, 2017 2:42 PM
  • Is one of the forms a subform of the other? If so, which is the subform?

    Also, are you attempting to assign a Form A control value to a control on Form B?

    Thursday, April 6, 2017 6:26 PM