Reference Previous control on subform from a Main form control

T

tuesamlarry

When user's focus is a textbox control on a subform control, and then Clicks
a Close button on the Main form, would like a messagebox to pop up depending
on which control on the subform had the focus. Some controls will be okay if
user decides to CLOSE, but not others. How do I reference the previous
control?
Main Form is frmClient
Subform is sfrmServiceProvided
Controls on subform I don't want to allow Closing Main form are srvcVoid,
cmdOpenNotes.
I believe my sticking point is that the Screen.PreviousControl is probably
the subform control, and not the textbox control on the subform.
My difficulty is that all my volunteers are seniors, and they are absolutely
wedded to the mouse which they click willy nilly all over the place. Would
appreciate suggestions, including other approaches.
Using Access 2007 on Win XP Pro.
 
K

Ken Snell [MVP]

You could use the Exit event of the subform control to store the name of the
control on the subform that has the focus. You could store this in a
private, global variable in the form's module. Then read that control name
from where you store it in the Click event of the command button.
 
D

Dirk Goldgar

tuesamlarry said:
When user's focus is a textbox control on a subform control, and then
Clicks
a Close button on the Main form, would like a messagebox to pop up
depending
on which control on the subform had the focus. Some controls will be okay
if
user decides to CLOSE, but not others. How do I reference the previous
control?
Main Form is frmClient
Subform is sfrmServiceProvided
Controls on subform I don't want to allow Closing Main form are srvcVoid,
cmdOpenNotes.
I believe my sticking point is that the Screen.PreviousControl is probably
the subform control, and not the textbox control on the subform.
My difficulty is that all my volunteers are seniors, and they are
absolutely
wedded to the mouse which they click willy nilly all over the place. Would
appreciate suggestions, including other approaches.
Using Access 2007 on Win XP Pro.


This may not be as hard as you think. Aside from Screen.ActiveControl and
Screen.PreviousControl, each form has its own ActiveControl property. The
control that was active on the subform remains its ActiveControl even after
the focus is transferred to the main form. So you can check for whether one
of those controls was the previous control like this:

Dim blnBadPrevious As Boolean

On Error Resume Next ' in case no previous control
If Screen.PreviousControl.Name = "sfrmServiceProvided" Then
Select Case Me!sfrmServiceProvided.Form.ActiveControl.Name
Case "srvcVoid", "cmdOpenNotes"
blnBadPrevious = True
End Select
End If

If blnBadPrevious Then
' ... do something ...
Else
' ... do something else ...
End If

That said, there are lots of ways to close a form. If your users are able
to close the form without clicking your close button, you may have to put
this code in the form's Unload event (which can be canceled), and I don't
know for sure whether it will work there.

I usually find it a losing game to try to control where the users move the
focus. Instead, I try to either control the validity of data in the form's
BeforeUpdate event, or control the flow by having controls be disabled until
it is appropriate for the user to work with them, or both.
 
T

tuesamlarry

Thank you. The coding and syntax is what I needed. Yes I have a myriad of
ways I employ to keep the forms humming correctly considering the creative
ways my volunteers, without effort, can thwart them. I do have code, gleaned
from these forums which limit users to using only my CLOSE command button to
close forms and my own EXIT button to exit the database. These work
perfectly. I also of course have required fields which are found out during
Before_Update if they are blank. My Main forms also open with all controls
disabled until they enter a client ID number. These forums have taught me a
lot! Just starting to get the hang of the Boolean place holders, and what a
treat they are.
Thanks again.
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top