set focus to field in another form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

i have a main form with a save button. When this is pressed, a subform
appears (the save button's onclick event makes the form visible.) I am
having difficulty in setting the focus to the field on the subform.
What is the code I need? I've tried these 2 lines together, as listed:
Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a control that
has got the focus! Even when I have tried not hiding the save button, the
focus still isn't shifting. I must be doing something wrong... please help.
(just to reiterate, the save button is on the main form.)

Thank you
Rich
 
Rich1234 said:
Hi

i have a main form with a save button. When this is pressed, a
subform appears (the save button's onclick event makes the form
visible.) I am having difficulty in setting the focus to the field
on the subform.
What is the code I need? I've tried these 2 lines together, as
listed: Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a control
that has got the focus! Even when I have tried not hiding the save
button, the focus still isn't shifting. I must be doing something
wrong... please help. (just to reiterate, the save button is on the
main form.)

You have to set the focus both to the subform control (on the main
form), and to the control on the subform itself:

Me!SubformControlName.SetFocus
Me!NameOfControlOnSubform.SetFocus

Explanation: The main form has an "active control", and the form object
displayed in the subform control has its own "active control". You need
to make the main form's active control be the subform, and you also need
to make the subform's active control be the one you want.
 
Rich1234 said:
Hi

i have a main form with a save button. When this is pressed, a subform
appears (the save button's onclick event makes the form visible.) I am
having difficulty in setting the focus to the field on the subform.
What is the code I need? I've tried these 2 lines together, as listed:
Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a control that
has got the focus! Even when I have tried not hiding the save button, the
focus still isn't shifting. I must be doing something wrong... please help.
(just to reiterate, the save button is on the main form.)

Thank you
Rich

You need to set the focus to the subform control first e.g.

Me.subformname.SetFocus
Me.subformname.form![field name].SetFocus
 
Rich,

First set the focus to the subform, then to the control, and then you can
toggle the
Visible property off:

' sets focus to subform control on main form
Me.subform name.SetFocus

' sets focus to desired control on the subform
Me.subform name.Form![field name].SetFocus

Me.Save_Vehicle.Visible = False

Hope that helps.
Sprinks
 
Thanks guys. I'm still not too clear on how this all works (when to use !
instead of .) I need to read up on this.... can you recommend?

Dirk - I'm a bit confused. What is the "subform control name" in your first
line of code? Is that just the name of the subform? (I guess not - I tried
this and it doesn't work. I don't know what this "subform control name" is
in my case.)

Baz- Thanks... I got it working using your suggestion

Dirk Goldgar said:
Rich1234 said:
Hi

i have a main form with a save button. When this is pressed, a
subform appears (the save button's onclick event makes the form
visible.) I am having difficulty in setting the focus to the field
on the subform.
What is the code I need? I've tried these 2 lines together, as
listed: Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a control
that has got the focus! Even when I have tried not hiding the save
button, the focus still isn't shifting. I must be doing something
wrong... please help. (just to reiterate, the save button is on the
main form.)

You have to set the focus both to the subform control (on the main
form), and to the control on the subform itself:

Me!SubformControlName.SetFocus
Me!NameOfControlOnSubform.SetFocus

Explanation: The main form has an "active control", and the form object
displayed in the subform control has its own "active control". You need
to make the main form's active control be the subform, and you also need
to make the subform's active control be the one you want.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Rich,

Although Access permits the dot operator to be used, the bang operator is
intended to be used to separate an object from its collection, as in:

Forms!frmContacts
Forms!frmContacts!txtContactID

frmContacts is in the Forms collection, and txtContactID is part of the
Controls collection of the frmContacts form.

The period is to separate an object from a property or method:

' Sets the Visible property
Forms!frmContacts!txtContactID.Visible = True

' Applies the SetFocus method
Forms!frmContacts!txtContactID.SetFocus

Using them appropriately makes your intention and thinking IMO more clear
both to you and anyone who might need to support your app down the road.

Hope that helps.

Sprinks

Rich1234 said:
Thanks guys. I'm still not too clear on how this all works (when to use !
instead of .) I need to read up on this.... can you recommend?

Dirk - I'm a bit confused. What is the "subform control name" in your first
line of code? Is that just the name of the subform? (I guess not - I tried
this and it doesn't work. I don't know what this "subform control name" is
in my case.)

Baz- Thanks... I got it working using your suggestion

Dirk Goldgar said:
Rich1234 said:
Hi

i have a main form with a save button. When this is pressed, a
subform appears (the save button's onclick event makes the form
visible.) I am having difficulty in setting the focus to the field
on the subform.
What is the code I need? I've tried these 2 lines together, as
listed: Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a control
that has got the focus! Even when I have tried not hiding the save
button, the focus still isn't shifting. I must be doing something
wrong... please help. (just to reiterate, the save button is on the
main form.)

You have to set the focus both to the subform control (on the main
form), and to the control on the subform itself:

Me!SubformControlName.SetFocus
Me!NameOfControlOnSubform.SetFocus

Explanation: The main form has an "active control", and the form object
displayed in the subform control has its own "active control". You need
to make the main form's active control be the subform, and you also need
to make the subform's active control be the one you want.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Rich1234 said:
Thanks guys. I'm still not too clear on how this all works (when to
use ! instead of .) I need to read up on this.... can you recommend?

Dirk - I'm a bit confused. What is the "subform control name" in
your first line of code? Is that just the name of the subform? (I
guess not - I tried this and it doesn't work. I don't know what this
"subform control name" is in my case.)

Baz- Thanks... I got it working using your suggestion

Dirk Goldgar said:
Rich1234 said:
Hi

i have a main form with a save button. When this is pressed, a
subform appears (the save button's onclick event makes the form
visible.) I am having difficulty in setting the focus to the field
on the subform.
What is the code I need? I've tried these 2 lines together, as
listed: Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a
control that has got the focus! Even when I have tried not hiding
the save button, the focus still isn't shifting. I must be doing
something wrong... please help. (just to reiterate, the save button
is on the main form.)

You have to set the focus both to the subform control (on the main
form), and to the control on the subform itself:

Me!SubformControlName.SetFocus
Me!NameOfControlOnSubform.SetFocus

Explanation: The main form has an "active control", and the form
object displayed in the subform control has its own "active
control". You need to make the main form's active control be the
subform, and you also need to make the subform's active control be
the one you want.

I actually made a mistake -- sorry. That second line should have been:

Me!SubformControlName!NameOfControlOnSubform.SetFocus

By "SubformControlName", I mean the name of the control on the main form
that displays the subform in its window. That may or may not be the
same as the name of the form object that is displayed in that window,
depending on how the subform control was created and whether you renamed
it. If you just click the "subform control" tool on the Controls
toolbar and and then click on the form, a new subform control will be
created and named something like "Subform1" (numbers increasing
according to the number of controls already on the form). But if you
drag a form object from the database window and drop it on the form, the
subform control will be created with the same name as that form object.
And if the form wizard creates the subform, it gives it a name based on
what you decide to call it.

By "NameOfControlOnSubform", I mean the name of the control *on the
subform* to which you want to SetFocus.
 
Thanks.. I understand better than before.....
How do I find the "subform control name?" Is this the name in the "name"
row in the properties window when you first click on the topleft corner of
the subform in design view? But I thought this was the name of the subform
itself!


Dirk Goldgar said:
Rich1234 said:
Thanks guys. I'm still not too clear on how this all works (when to
use ! instead of .) I need to read up on this.... can you recommend?

Dirk - I'm a bit confused. What is the "subform control name" in
your first line of code? Is that just the name of the subform? (I
guess not - I tried this and it doesn't work. I don't know what this
"subform control name" is in my case.)

Baz- Thanks... I got it working using your suggestion

Dirk Goldgar said:
Hi

i have a main form with a save button. When this is pressed, a
subform appears (the save button's onclick event makes the form
visible.) I am having difficulty in setting the focus to the field
on the subform.
What is the code I need? I've tried these 2 lines together, as
listed: Me.subform name.Controls![field name].SetFocus
Me.Save_Vehicle.Visible = False

which isn't working... I get an error saying you can't hide a
control that has got the focus! Even when I have tried not hiding
the save button, the focus still isn't shifting. I must be doing
something wrong... please help. (just to reiterate, the save button
is on the main form.)

You have to set the focus both to the subform control (on the main
form), and to the control on the subform itself:

Me!SubformControlName.SetFocus
Me!NameOfControlOnSubform.SetFocus

Explanation: The main form has an "active control", and the form
object displayed in the subform control has its own "active
control". You need to make the main form's active control be the
subform, and you also need to make the subform's active control be
the one you want.

I actually made a mistake -- sorry. That second line should have been:

Me!SubformControlName!NameOfControlOnSubform.SetFocus

By "SubformControlName", I mean the name of the control on the main form
that displays the subform in its window. That may or may not be the
same as the name of the form object that is displayed in that window,
depending on how the subform control was created and whether you renamed
it. If you just click the "subform control" tool on the Controls
toolbar and and then click on the form, a new subform control will be
created and named something like "Subform1" (numbers increasing
according to the number of controls already on the form). But if you
drag a form object from the database window and drop it on the form, the
subform control will be created with the same name as that form object.
And if the form wizard creates the subform, it gives it a name based on
what you decide to call it.

By "NameOfControlOnSubform", I mean the name of the control *on the
subform* to which you want to SetFocus.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Rich1234 said:
Thanks.. I understand better than before.....
How do I find the "subform control name?" Is this the name in the
"name" row in the properties window when you first click on the
topleft corner of the subform in design view? But I thought this was
the name of the subform itself!

It's a bit tricky, because Access shows you the design view of the
subform's *form* when you look at the main form in design view. It's
easy to lose track, when you click around in design view, of whether
it's the subform control you have currently selected, or the form in
that control.

If you open the main form in design view, initially the main form itself
will be the selected object. There will be a square dot in the box at
the upper left corner of the form window, just under the caption bar.
If you bring up the property sheet now, you'll see that its caption says
"Form".

Now click just once anywhere on the subform, as you see it displayed in
design view. That will select the subform control, and you'll see the
caption of the property sheet change to "Subform/Subreport: ", followed
by the name of the subform control. The "object" dropdown box on both
the property sheet and the form design toolbar will both also state the
name of the subform control, and that name will be the first property on
the Other tab of the property sheet.

If you then click again in the subform, you find that it's the various
objects on the subform's Form object that are selected (including the
form itself, if you clicked in the upper-left "form box" to put the
square dot there), and the property sheet will show the properties of
those objects.
 
Thanks for your insight Dirk... I wondered what was selected the first time
a subform is clicked.

rich
 
Back
Top