Wrong form has focus - how to set on the proper form? (1 Viewer)

Russ05

New member
Local time
Tomorrow, 08:44
Joined
Jan 19, 2022
Messages
7
Hi everyone, I have a simple database which is storing annual leave applications and their approval status. I have a Employee form (frmEmployeeDetails) showing employee details. I have a button on that form which opens another form and allows the user to enter the last working day (date), return to work day (date) from calendar. A function then calculates annual leave days and public holidays based on those dates. A recordset is created for each week of leave. I have a button on the form which creates the recordset(s) to load into a subform (frmALSched) on the form (Form1). This is where the problem occurs.
Using DAO recordset to create a new record and enter each record into the subform, which was working great, until now. For some reason the form on which the subform exists is not the active form even after I set it as such. When the code reaches the following lines

With Forms!Form1!frmALSched.Form
DoCmd.GoToRecord , , acNewRec


The frmEmployeeDetails form actually goes to a new Record. Obviously, it appears to have the focus.

So I tried the folllowing lines of code:

Forms!Form1.SetFocus 'Sets focus on the form containing the subform
Me!frmALSched.SetFocus 'Sets focus on the subform control

Set frmCurrentForm = Screen.ActiveForm

Debug.Print "Current form is " & frmCurrentForm.Name

I receive this "Current form is frmEmployeeDetails" in the immediate window.

I cannot understand or workout why the Form1 isn't the form in focus as I believe I have set focus on Form1.ALSched.

Any advice will be greatly appreciated. I have come to the breaking point stage after trying to crack this one for the past few days.
 

Minty

AWF VIP
Local time
Today, 23:44
Joined
Jul 26, 2013
Messages
9,238
Try setting the focus to a control on the subform.
The subform control is exactly that a control on the main form, so at that point the current form is the main form.
A subform FORM can't really be the activeform as it's a control on the main form.

I hope that helps?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:44
Joined
Feb 19, 2002
Messages
35,884
The best practice when you want to have two forms visible at once is to have the first form open the second as model. That stops the code in the calling form and keeps the focus in the popup form until it is closed.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:44
Joined
May 7, 2009
Messages
16,092
Forms!Form1.SetFocus
Forms!Form1!frmALSched.Form.SetFocus
Forms!Form1!frmALSched.Form!fieldInTheSubForm.SetFocus
 

Russ05

New member
Local time
Tomorrow, 08:44
Joined
Jan 19, 2022
Messages
7
Many thanks to all respondents, very much appreciated. Despite having thought I had tried everything to and constructed my code, I have learned a valuable lesson. I hadn't opened the "Form1" as modal. Once done it worked great. Can't thank you enough, now I can move onto my next challenge.
Hope I can repay in some way to others in the future.
 

Users who are viewing this thread

Top Bottom