473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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:

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

2 8102
Stewart Ross
2,545 Expert Mod 2GB
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
Stewart,

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

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

Similar topics

1
by: David B | last post by:
I have an app which handles data relating to cattle. The ear tag number consists of 3 parts. Country code - UK. Farm Code - 107248. and animal number - 600555. At present the complete set has...
3
by: PerryC | last post by:
I have a Main Form, then a subform. There is a date field in the Subform that I want to be able to click on it, and a VBCalendar popup in the main form for me to choose a date (I don't want it on...
0
by: Deano | last post by:
I have a main/subform arrangement which works well. The main form's parent records are about employees. The subform calculates their salary. I have now decided that the user can enter some...
2
by: Reginald Bal | last post by:
Hello, I created a main form with 2 subforms. In an attempt to move to the next record (or new record) on the main form I get the error " Method 'requery' of object '_Subform' failed". The...
9
by: | last post by:
Hi How to get a reference to main form in a Windows Form 2.0 Application? I'm making a a library and I need a reference to the main form of the application that is using that library. TIA
12
by: info | last post by:
Hi All, I am trying to set the hourglass cursor inside a class that has nothing to do with MainForm class and I don't want to pass a reference to MainForm. How can I set the current cursor to...
6
by: CJONES | last post by:
I have a main form and use a combo box to open a pop up form. The combo box lists customers and only one customer requires the fields that are found on the pop up form. I can get the form to pop...
2
by: CJONES | last post by:
I have a main form with a combo box where if a specific customer is selected a popup form opens and additional information required for this customer is entered into the form. Data from the main...
5
by: Thelma Roslyn Lubkin | last post by:
I am still having trouble trying to use a popup form to allow user to set filters for the main form. The main form is based on a single table. The popup contains 5 listboxes, so the user can...
2
by: Miro | last post by:
Without using this: For Each currentForm As Form In My.Application.OpenForms or using something like me.mdi parent how can i reference the main form's form variable. Example: If i open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.