daoRS.FindFirst not setting the located field with focus when Form is a Subform

mdlueck

Sr. Application Developer
Local time
, 21:33
Joined
Jun 23, 2011
Messages
2,633
I think that the subform context is blocking setting the actual record / field with focus.

A call to daoRS.FindFirst does succeed properly in the subform context. However the button on the main form is left with focus and not the correct located field of the subform.

The locate record code works as desired when NOT involved in a subform context. So I think the fact that the button is on the main form and the multiple records form is within a subform control is some how messing up which control has focus.

If I double click the record of the subform to bring up the edit record dialog, then the correct subform control will retain focus when the edit form closes.

If I push a button on the main form which puts the selected record of the subform into edit mode, then closing the edit form, the button retains focus, not the selected record in the subform control.

How can I hard code shifting focus back to the subform \ correct record of the record list \ ID column of that record... same as if the record had been double clicked?
 
I don't understand Michael. Isn't it the button that you push to fire off the code?
 
Isn't it the button that you push to fire off the code?

Correct. The Edit Record button is what I pushed to open the Edit Record form.

The Edit Record form then contacts back the record list form to have it daoRS.FindFirst the record which was being edited. (Or added in the case of the corresponding Add Record form.)

In the case where no Subform controls are involved:
1) Push the Edit Record button
2) Edit Form opens
3) Click button on the Edit Form to close it
4) That makes a call back to the previous form to locate the record being edited (or the newly added record) Specifically it is locating based on the record ID column so the ID column gets focus
5) Edit Record form closes

Same scenario only with a subform, the ID field of the multiple records list does not have focus, the button does.

The only difference I can find is the need to use Subform controls.
 
Same scenario only with a subform, the ID field of the multiple records list does not have focus, the button does.

And thus I am thinking to in the code which is relocating the correct record, also shift focus to the record grid control.
 
I attempted to shift focus to the correct control in the area of the code which locates the correct record, as follows:
Code:
  'Locate the record if we received a valid ID
   If lRecordID > 0 Then
     Set daoRS = Me.RecordsetClone
     daoRS.FindFirst ("[id] = " & lRecordID)
     'If we could find the newly added record, jump to it
     If Not daoRS.NoMatch Then
       Me.Bookmark = daoRS.Bookmark
       Me.ActiveControl.SetFocus
       Debug.Print "Me.ActiveControl.SetFocus on: " & Me.ActiveControl.Name
     End If
   End If
The Immediate window gets the output I expected:
Code:
Me.ActiveControl.SetFocus on: fldid
However the button still has focus, not the ID field of the record grid. It works just fine outside of a subform context.
 
Last edited:
You said the textbox control doesn't have the focus so why do you think the ActiveControl is the textbox? ;)

Set focus to the subform control
Set focus to the textbox

in that order.
 
Set focus to the subform control
Set focus to the textbox

in that order.
Worked like a charm!!!

Code:
    If Not daoRS.NoMatch Then
      Me.Bookmark = daoRS.Bookmark
      Me.Parent.subform_quality.SetFocus
      Me.ActiveControl.SetFocus
    End If
Thank you, vbaInet!
 
You're welcome Michael!

And I won't use the ActiveControl in that context if I were you.
 
And I won't use the ActiveControl in that context if I were you.

Then what would you suggest? The immediate window output certainly looks like the control I want to have focus is the ActiveControl at the time.
 
Code:
    If Not daoRS.NoMatch Then
      Me.Bookmark = daoRS.Bookmark
      Me.Parent.subform_quality.SetFocus
      Me.fldid.SetFocus
    End If
Sending the column control the SetFocus works and the correct record of the record list remains selected. I was unsure if I specified a control name that is repeated multiple times via Access itself if the correct control would receive the SetFocus or not. So, is this a better idea, vbaInet?
 
It can be flakey, you just can't be guaranteed that's the control with the focus. Refer to it directly.
 

Users who are viewing this thread

Back
Top Bottom