Set focus in Main Form from PopUp Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigukfan
    New Member
    • Mar 2010
    • 21

    Set focus in Main Form from PopUp Form

    I have an application that is used to collect clinical data from hospital patients involved in research. Various forms are used to collect specific data, often blood results. When the users enter this data, they may make a mistake and enter a value well outside the normal range. The normal range differs depending on the blood test.

    If the value entered is outside the expected range, a PopUp form is opened, and values are passed using OpenArgs. These include the name of the main form, the name of the control, the actual value entered and a 'plain English' name of the type of blood test. The popup form displays the value and plain English name, and asks the user if they are sure they meant that specific value. The user has two command buttons, Accept which simply closes the PopUp, or Reject which returns the focus to the control on the main form.

    I am using Access 2007.

    The problem is I can't seem to return the focus to the main form. The conventional way to achieve this is:

    Code:
    Forms!TheFormName!ControlName.SetFocus
    However, this does not work. Here is the code on my PopUp form:

    Code:
    Option Compare Database
    Private strFormName As String        'The calling form
    Private strControlName As String     'The calling control, focus may be returned here
    Private strValue As String           'The actual value
    Private strValueType As String       'The type of value, such as ACT or Hb
    In the Form Load:

    Code:
    Private Sub Form_Load()
    strFormName = Split(OpenArgs, "~")(0)
    strControlName = Split(OpenArgs, "~")(1)
    strValue = Split(OpenArgs, "~")(2)
    strValueType = Split(OpenArgs, "~")(3)
    lblTypeOfValue.Caption = strValueType
    lblValue.Caption = strValue
    End Sub
    If the user clicks the Reject command button:

    Code:
    Private Sub cmdNo_Click()
    Forms(strFormName).Controls!strControlName.SetFocus
    DoCmd.Close acForm, Me.Name, acSaveNo
    End Sub
    The error message is Can't find the field strControlName referred to in your expression.

    I have tried declaring fldMyField as a Field in stead of strControlName, but that did not work.

    Any ideas where I am going wrong? Thanks for your help.
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    You have made a small error with the use of the bang separator in line 2 of the last code segment above. The bang version can't be used to refer indirectly to the value of a variable.

    To use the variable you need to refer instead to the controls collection of the specified form as a whole:

    Code:
    Forms(strFormName).Controls(strControlName).SetFocus
    However, to make sure that the correct form has the focus when you close the popup you will need to set the focus to the form itself first. The complete sub from your last code segment is then:

    Code:
    Private Sub cmdNo_Click()
        With Forms(strFormName)
            .SetFocus
            .Controls(strControlName).SetFocus
        End With
        DoCmd.Close acForm, Me.Name, acSaveNo
    End Sub
    -Stewart

    Comment

    • bigukfan
      New Member
      • Mar 2010
      • 21

      #3
      Stewart,

      Perfect, that works fine now. Thanks for your help.

      Comment

      Working...