As to your follow-up response:
dameeti said:
If I set the focus upon the field within the subform, does that implicitly set the focus upon the form? Or must I first set the focus on the form and then set the focus upon the field itself?
TheAceMan1 said:
In form view selecting a control sets the focus to that control, but yes!, that form/subform also has the focus.
In VBA No!. Its the two step process (form then control) if that form/subform doesn't already have the focus. Note: this only applies to a form with subform. Single forms of course always have the focus & no problemo.
That explains a few areas that were a bit cloudy before as they apply to VBA usage.
TheAceMan1 said:
Now . . . a little more heads-up on the focus code I gave. Although its executed everytime, the focus method is executed only if required. If you put the focus method in an infinite loop all by itself and watch the screen, you'll see a flicker every now and then.
Okay, I understand your flicker point completely. Let me just clarify my usage. The code in question only executes once when the user changes customers by specifying an item owned by a customer (hence the main form will almost always be updated/repainted as a matter of course). I do a query and the new customer and its related items are the current recordset [—] and then this code attempts to select to the actual item of interest within the sub-table (hence it will also (almost always) trigger a screen repaint).
So the net result: the screen will "flicker" no matter what I do, and it will only happen only when the customer is changed when the combo box is being used by the user to perform a search. So... I don't think any flicker will matter in this case, and the code could be a tad cleaner when reading. But I
do completely agree with you on the technique in general of only performing the code when it's actually needed (especially when the underlying code involves more than a simple logic test).
Hmmm... after ALL of these various back-and-forths,
this appears to be
the crux of the matter:
The subform's routines must always be clear about the field reference, specifically when the routine might be called from outside the form. Int this case, I think the main problem was the usage of the
Me.FieldName. Since the
Me. referred to the current active form and the main form still had that focus, it was trying to find the field within the main form (not the form that contained the field and the called code) [—] which makes total sense when you consider the overall picture of referencing fields.
So by making sure that the desired form always has the required focus, the
Me. short-cut reference will always retain that proper focus. This leads me to believe the final code should look like:
Code:
If Screen.ActiveControl.Parent.Name <> "SubFormName" Then _
Forms!MainFormName!SubFormName.SetFocus
Me.TextFieldName.SetFocus 'Set focus on control
DoCmd.FindRecord DesiredValue
or even
If Screen.ActiveControl.Parent.Name <> "SubFormName" Then _
Me!SubFormName.SetFocus 'Set focus on form
Me.TextFieldName.SetFocus 'Set focus on control
DoCmd.FindRecord DesiredValue
Which leads to one final question:
If I need to reference a field within the main form after the call to that search routine has completed, how can I restore the focus back to the original form? In my case, I'm actually going to want to leave the focus upon the new subform, BUT can I code that search routine to restore the previous focus when done? Something like:
Code:
SavedFocus = Me 'Save pointer to current form?
If Screen.ActiveControl.Parent.Name <> "subFormName" Then _
Me!subFormName.SetFocus
Me.TextFieldName.SetFocus 'Set focus on control
DoCmd.FindRecord DesiredValue
[COLOR=purple]Me = SavedFocus[/color] 'Restore current active form ??
If this would work, how would I define "SavedFocus"?
... or another way:
Code:
Dim PreviousForm as String, NeedForm as Boolean
PreviousForm = Screen.ActiveControl.Parent.Name
ChgForm = PreviousForm <> "subFormName"
If ChgForm Then Me!subFormName.SetFocus
Me.TextFieldName.SetFocus
DoCmd.FindRecord DesiredValue
If ChgForm Then _
[COLOR=purple][b]Hmmm... what goes here to restore the previous form's focus?[/b][/color]
Am I close?
If this would restore the focus so that any follow-up statements within the main routine continue to be directed to the original (in this case, the main) form, then that would make the search routine very form-independent (always a nice attribute). (I'm big on modular, when appropriate.)
If the previously noted solution you provided solves my problem (and I suspect it will), then I believe you (and all of the other posts that help delve into this problem) may have solved my problem [—] and you gave me some insight into the methodology of VBA/Access.
Thanks to all and to all a good night (okay, a good afternoon).
Happy New Year
David (now a much-happier camper)