By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,870 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,870 IT Pros & Developers. It's quick & easy.

Set focus in Main Form from PopUp Form

P: 21
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:

Expand|Select|Wrap|Line Numbers
  1. Forms!TheFormName!ControlName.SetFocus
However, this does not work. Here is the code on my PopUp form:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Private strFormName As String        'The calling form
  3. Private strControlName As String     'The calling control, focus may be returned here
  4. Private strValue As String           'The actual value
  5. Private strValueType As String       'The type of value, such as ACT or Hb
In the Form Load:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. strFormName = Split(OpenArgs, "~")(0)
  3. strControlName = Split(OpenArgs, "~")(1)
  4. strValue = Split(OpenArgs, "~")(2)
  5. strValueType = Split(OpenArgs, "~")(3)
  6. lblTypeOfValue.Caption = strValueType
  7. lblValue.Caption = strValue
  8. End Sub
If the user clicks the Reject command button:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNo_Click()
  2. Forms(strFormName).Controls!strControlName.SetFocus
  3. DoCmd.Close acForm, Me.Name, acSaveNo
  4. 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.
Aug 18 '12 #1

✓ answered by Stewart Ross

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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNo_Click()
  2.     With Forms(strFormName)
  3.         .SetFocus
  4.         .Controls(strControlName).SetFocus
  5.     End With
  6.     DoCmd.Close acForm, Me.Name, acSaveNo
  7. End Sub
-Stewart

Share this Question
Share on Google+
2 Replies


Expert Mod 2.5K+
P: 2,545
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNo_Click()
  2.     With Forms(strFormName)
  3.         .SetFocus
  4.         .Controls(strControlName).SetFocus
  5.     End With
  6.     DoCmd.Close acForm, Me.Name, acSaveNo
  7. End Sub
-Stewart
Aug 18 '12 #2

P: 21
Stewart,

Perfect, that works fine now. Thanks for your help.
Aug 18 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.