Focus to SubForm Control

I

iamnu

I want to set the focus on a control in my subform AFTER I have
entered the applicable data on the Main Form, but before moving on to
the the next record in the Main form.

I am using the following syntax:
Forms!TelephoneBook!TelephoneBookSubForm.Form!cboDirectory.SetFocus

where TelephoneBook is the Main Form
TelephoneBookSubForm is the SubForm
cboDirectory is the name of the control I want to have focus in the
SubForm.

1. What's wrong with my syntax?
2. Which event should I be using for this?
 
D

Dirk Goldgar

iamnu said:
I want to set the focus on a control in my subform AFTER I have
entered the applicable data on the Main Form, but before moving on to
the the next record in the Main form.

I am using the following syntax:
Forms!TelephoneBook!TelephoneBookSubForm.Form!cboDirectory.SetFocus

where TelephoneBook is the Main Form
TelephoneBookSubForm is the SubForm
cboDirectory is the name of the control I want to have focus in the
SubForm.

1. What's wrong with my syntax?

Nothing; it's just incomplete. You have to set the focus twice: once to
the subform control on the main form, and once to the control on the
subform:

Forms!TelephoneBook!TelephoneBookSubForm.SetFocus
Forms!TelephoneBook!TelephoneBookSubForm.Form!cboDirectory.SetFocus

That's because the main form and the subform each have their own active
control, and the subform's active control will only have the visible focus
if the subform has the focus.
2. Which event should I be using for this?

How do you know when the "applicable data" has been entered? What is the
applicable data?
 
I

iamnu

Nothing; it's just incomplete.  You have to set the focus twice:  once to
the subform control on the main form, and once to the control on the
subform:

    Forms!TelephoneBook!TelephoneBookSubForm.SetFocus
    Forms!TelephoneBook!TelephoneBookSubForm.Form!cboDirectory.SetFocus

That's because the main form and the subform each have their own active
control, and the subform's active control will only have the visible focus
if the subform has the focus.


How do you know when the "applicable data" has been entered?  What is the
applicable data?

I said I was getting a syntax error, but I should have said a run time
error # 2110..."Microsoft Access can't move the focus to the control
TelephoneBookSubForm".
The code getting the error is...Forms!TelephoneBook!
TelephoneBookSubForm.SetFocus
and I have this code in the BeforeUpdate event of the Main Form

In my case, applicable data can simply be a person's name.
 
D

Dirk Goldgar

iamnu said:
I said I was getting a syntax error, but I should have said a run time
error # 2110..."Microsoft Access can't move the focus to the control
TelephoneBookSubForm". The code getting the error
is...Forms!TelephoneBook!TelephoneBookSubForm.SetFocus and I have this
code in the BeforeUpdate event of the Main Form

In my case, applicable data can simply be a person's name.

Actually, you didn't *say* you were getting any kind of error, so I assumed
you just weren't seeing the focus change, for the reason I described. That
reason still holds true, by the way -- you must both move the focus to the
subform *and* move the focus to the control on the subform. However, now
that you tell me both what error you're getting and what event you're
running the code in, I can see why you're getting the error.

You can't move the focus to the subform in the main form's BeforeUpdate
event. That's because, in order for the focus to go to the subform, the
main form's record must be saved. But in the form's BeforeUpdate event, the
record hasn't been saved yet. Moving the focus to the subform would force
the form to save the record, which would fire the BeforeUpdate event, which
would try to move the focus to the subform, which would fire the
BeforeUpdate event ... I hope you see the problem.

Instead of using the form's BeforeUpdate event, I suggest you use the
AfterUpdate events of the controls that should be completed first. For
example, if there are two controls, FirstName and LastName, then you could
write a function to move the focus to the subform when they are both filled
in, and then call that function from the AfterUpdate event of each of the
two controls. This function would go in the General section of the form's
Module, and might look like this:

'----- start of function code -----
Private Function GoToSubformIfComplete()

If IsNull(Me.LastName) _
Or IsNull(Me.FirstName) _
Then
' Record is incomplete, so do nothing.
Else
' Set the focus to cboDirectory on the subform.
With Me.TelephoneBookSubForm
.SetFocus
.Form!cboDirectory.SetFocus
End With
End If

End Function

'----- end of function code -----

Now, you still have to call this function from the AfterUpdate events of
FirstName and LastName. You can do that easily, without even writing event
procedures, by using a function expression in the AfterUpdate *properties*
of those controls. Just select both controls (in design view) and open
their joint property sheet. On the Event tab of the property sheet, in the
AfterUpdate property line, enter:

=GoToSubformIfComplete()

And that should do it. Note: if you are already using the AfterUpdate event
of one or both of these controls, you won't be able to do it this way,
because you'd be replacing the existing event code (or macro), not just
adding to it. In that case, you'd have to create an actual event procedure
for the control, and have that event procedure both execute whatever was
previously being executed, and also call the function
GoToSubformIfComplete().
 
I

iamnu

Actually, you didn't *say* you were getting any kind of error, so I assumed
you just weren't seeing the focus change, for the reason I described.  That
reason still holds true, by the way -- you must both move the focus to the
subform *and* move the focus to the control on the subform.  However, now
that you tell me both what error you're getting and what event you're
running the code in, I can see why you're getting the error.

You can't move the focus to the subform in the main form's BeforeUpdate
event.  That's because, in order for the focus to go to the subform, the
main form's record must be saved.  But in the form's BeforeUpdate event, the
record hasn't been saved yet.  Moving the focus to the subform would force
the form to save the record, which would fire the BeforeUpdate event, which
would try to move the focus to the subform, which would fire the
BeforeUpdate event ... I hope you see the problem.

Instead of using the form's BeforeUpdate event, I suggest you use the
AfterUpdate events of the controls that should be completed first.  For
example, if there are two controls, FirstName and LastName, then you could
write a function to move the focus to the subform when they are both filled
in, and then call that function from the AfterUpdate event of each of the
two controls.  This function would go in the General section of the form's
Module, and might look like this:

'----- start of function code -----
Private Function GoToSubformIfComplete()

    If IsNull(Me.LastName) _
    Or IsNull(Me.FirstName) _
    Then
        ' Record is incomplete, so do nothing.
    Else
        ' Set the focus to cboDirectory on the subform.
        With Me.TelephoneBookSubForm
            .SetFocus
            .Form!cboDirectory.SetFocus
        End With
    End If

End Function

'----- end of function code -----

Now, you still have to call this function from the AfterUpdate events of
FirstName and LastName.  You can do that easily, without even writing event
procedures, by using a function expression in the AfterUpdate *properties*
of those controls.  Just select both controls (in design view) and open
their joint property sheet.  On the Event tab of the property sheet, inthe
AfterUpdate property line, enter:

    =GoToSubformIfComplete()

And that should do it.  Note: if you are already using the AfterUpdate event
of one or both of these controls, you won't be able to do it this way,
because you'd be replacing the existing event code (or macro), not just
adding to it.  In that case, you'd have to create an actual event procedure
for the control, and have that event procedure both execute whatever was
previously being executed, and also call the function
GoToSubformIfComplete().

Excellent! I used your GoToSubformIfComplete() function, and then
called it from the AfterUpdate event for the Main Form. Works great
now.

Thanks so much...
 
I

iamnu

Excellent!  I used your GoToSubformIfComplete() function, and then
called it from the AfterUpdate event for the Main Form.  Works great
now.

Thanks so much...

Whoops! One more problem.
When I use the page down/up keys to move from the Main Form, this code
works fine. But if I use the enter key to leave the Main Form and go
to the SubForm (via the Tab Order), the cursor is not visible. I'm
not sure where I wind up after using the enter key, but the
GoToSubformIfComplete() function does get executed.

Do you have a suggestion?
 
D

Dirk Goldgar

iamnu said:
Whoops! One more problem.
When I use the page down/up keys to move from the Main Form, this code
works fine. But if I use the enter key to leave the Main Form and go to
the SubForm (via the Tab Order), the cursor is not visible. I'm not sure
where I wind up after using the enter key, but the GoToSubformIfComplete()
function does get executed.

Do you have a suggestion?


I'm not sure what's going on. When you do that, and can't tell where the
focus is, press Ctrrl+G to open the Immediate Window, then enter this in
that window:

?Screen.ActiveControl.Name

When you press Enter after typing that in, the name of the control with the
focus should be displayed on the next line. That may be a clue to what's
happening.
 
I

iamnu

I'm not sure what's going on.  When you do that, and can't tell where the
focus is, press Ctrrl+G to open the Immediate Window, then enter this in
that window:

    ?Screen.ActiveControl.Name

When you press Enter after typing that in, the name of the control with the
focus should be displayed on the next line.  That may be a clue to what's
happening.

All these "tricks of the trade"! We learn lots from asking questions
from you experts.

The active control is in fact the cboDirectory as it should be, but
the cursor is just not flashing. However, when I alt+tab back to the
form after viewing the results from the immediate window, the cursor
IS flashing as it does when I use page down. Is there some way to
make the cursor flash when I use the Enter Key?

Thanks again for your expert advice.
 
I

iamnu

All these "tricks of the trade"!  We learn lots from asking questions
from you experts.

The active control is in fact the cboDirectory as it should be, but
the cursor is just not flashing.  However, when I alt+tab back to the
form after viewing the results from the immediate window, the cursor
IS flashing as it does when I use page down.  Is there some way to
make the cursor flash when I use the Enter Key?

Thanks again for your expert advice.

In an effort to be more clear, the cursor is not only "not flashing",
it's not visible at all. Using the enter key to exit the main form
and go to the subform works, but repeated use of the enter key does
not cause the focus to change.

While the immediate window results indicate the focus is on the
cboDirectory, there appears to be no way to prove this without going
to another application (clicking on another application or using alt
+tab, etc) and then returning to access, where I am then able to see
the flashing cursor in the cboDirectory control.
 
D

Dirk Goldgar

iamnu said:
While the immediate window results indicate the focus is on the
cboDirectory, there appears to be no way to prove this without going to
another application (clicking on another application or using alt+tab,
etc) and then returning to access, where I am then able to see the
flashing cursor in the cboDirectory control.

Hmm. I'm able to reproduce the problem, and I think it must be a display
bug of some sort -- maybe for some reason Access doesn't realize that it
needs to repaint the window. At first I thought a simple call to Me.Repaint
would fix it, but no. If you invalidate the form by moving another window
on top of it, then the caret and selection are visible. However, none of
the things I've tried so far in code seems to have any effect whatsoever.

I repeat: hmm.
 
I

iamnu

Hmm.  I'm able to reproduce the problem, and I think it must be a display
bug of some sort -- maybe for some reason Access doesn't realize that it
needs to repaint the window.  At first I thought a simple call to Me.Repaint
would fix it, but no.  If you invalidate the form by moving another window
on top of it, then the caret and selection are visible.  However, none of
the things I've tried so far in code seems to have any effect whatsoever.

I repeat:  hmm.

Okay, at least I'm know I'm not the only one unable to solve this.
I'll check back periodically to see if you have come up with a
solution, and if I find something, I'll let you know also.

Thank your sir! I can't expression my appreciation enough for you
getting me this far.
 

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