Using OnClick of a label to set focus to a control

G

Guest

I am using a label as a menu button and need the OnClick event to set the
source object for a subform and then set focus to a certain control in the
subform.

Please see code below (Form1 has a control called TextBox2)

This following code works, but does not set the subform's source object (the
subform is already set to "SubForm1" and control could be anywhere):
With forms!MainForm
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With

This code does not work, because it includes the crucial step of setting the
source object (the subform's source object could already be "Form1"; once the
menu button is clicked, no control seems to have focus until I click down
again somewhere in the form):
With forms!MainForm
.SubForm1.SourceObject = "Form1"
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With

What am I missing here? Thanks
 
E

Eric Schittlipz

Mike said:
I am using a label as a menu button and need the OnClick event to set the
source object for a subform and then set focus to a certain control in the
subform.

Please see code below (Form1 has a control called TextBox2)

This following code works, but does not set the subform's source object
(the
subform is already set to "SubForm1" and control could be anywhere):
With forms!MainForm
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With

This code does not work, because it includes the crucial step of setting
the
source object (the subform's source object could already be "Form1"; once
the
menu button is clicked, no control seems to have focus until I click down
again somewhere in the form):
With forms!MainForm
.SubForm1.SourceObject = "Form1"
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With

What am I missing here? Thanks



....
..SubForm1.SetFocus
..SubForm1.Form.TextBox2.SetFocus
 
G

Guest

I tried this out using a form, a subform control and two subforms. Using 2
labels on the form, and their OnClick events, i used the following two lines
of code.

Me.subForm.SourceObject = "subForm1"
Me.subForm.Controls.Item("ctrl2").SetFocus

and

Me.subForm.SourceObject = "subForm2"
Me.subForm.Controls.Item("ctrl3").SetFocus

I discovered that if you subsequently clicked in the subform OR clicked and
HELD DOWN in the same label as you last clicked, that the cursor then blinked
in the properly chosen subform control. However, you couldn't directly type
into it or see the cursor blink otherwise.

Ok, while this isn't a fix; the two lines of code actually WORK but aren't
the last executed! Something else is then happening.

What it appears to be is a problem with the OnClick event being
handled/passed to the form's detail section's OnClick and then the form's
OnClick events, or some such additional processing after your MouseUp event
happens in your main form, which "moves" the focus back to the form.

Remember, these events fire even if you haven't implemented a handler!

Haven't figured out a solution yet.
 
E

Eric Schittlipz

Mike said:
That didn't work either

You could follow a couple of newsgroup conventions by avoiding top-posting,
sounding as if you realise someone is trying to help you for free and being
slightly more informative than "it didn't work". Perhaps I should leave it
at "it worked for me".

 
M

Marshall Barton

Mike said:
I am using a label as a menu button and need the OnClick event to set the
source object for a subform and then set focus to a certain control in the
subform.

Please see code below (Form1 has a control called TextBox2)

This following code works, but does not set the subform's source object (the
subform is already set to "SubForm1" and control could be anywhere):
With forms!MainForm
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With

This code does not work, because it includes the crucial step of setting the
source object (the subform's source object could already be "Form1"; once the
menu button is clicked, no control seems to have focus until I click down
again somewhere in the form):
With forms!MainForm
.SubForm1.SourceObject = "Form1"
.SubForm1.SetFocus
.SubForm1!TextBox2.SetFocus
End With


Don't know what the issue is, but I'm wondering if there may
be a race condition. It might(?) be that the new source
object is not ready to have the focus set to one of it's
controls.

If that is the issue, you **might** be able to give it time
to sync up by adding one or two DoEvents before setting the
focus to the text box.
 
G

Guest

Thanks for the suggestion. I tried DoEvents without much luck.

I did discover that using a command button works just fine and sets the
focus as expected, but I am still unable to do the same with a label (which I
need to use as the menu button). Any ideas why this would be?

Thanks for everyone's effort to help.
 
M

Marshall Barton

Mike said:
Thanks for the suggestion. I tried DoEvents without much luck.

I did discover that using a command button works just fine and sets the
focus as expected, but I am still unable to do the same with a label (which I
need to use as the menu button). Any ideas why this would be?

Hmmm, the big difference between a label and a button is
that the label can not receive the focus. I wonder if your
code should set the focus to some other control before
setting it to the subform control???
 
G

Guest

That worked! I thought I had tried that before, but what worked was:

Me.CommandButton.SetFocus
Me.SubForm.SourceObject = "Form1"
Me.SubForm.SetFocus
Me.SubForm.Form.TextBox2.SetFocus

Thanks!
 
M

Marshall Barton

Well, I'm glad you got past this issue.

While I suppose it is somewhat interesting, I have no
explanation as to why it needed this fooling around.
Probably something to do with the focus already being in the
subform??
 

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