Refer to Parent form

G

Guest

Hi all,

I have a Form with a combo box that I access by pressing F6 with the
following function:

Function AutoKeysF6()
On Error GoTo AutoKeysF6_Err

' Find PO#
DoCmd.GoToControl "FindPO"

AutoKeysF6_Exit:
Exit Function

AutoKeysF6_Err:
MsgBox Error$
Resume AutoKeysF6_Exit

End Function

The problem is that when the focus is in any of the subforms it doesn't work.
I use this same combo box in more than one form and want to find a way to
refer to the parent form that somehow will let me use the function in a
general way.

I am still a newbie and can't figure it out.

Any help would be greatly appreciated.

Emilio
 
S

SusanV

Hi Wind,

To reference a control on the current form:
Me.ControlName

To reference a control on the Main form from a subform:
Forms!MainFormName!ControlName

To reference a subform control from the Main form:
Forms!MainFormName!SubFormName.Controls!ControlName
 
D

Dirk Goldgar

SusanV said:
To reference a control on the Main form from a subform:
Forms!MainFormName!ControlName

If you know the code is running on a subform, you can take a shortcut by
referring to the subform's Parent property:

Me.Parent!ControlName
 
S

SusanV

Less typing - and more portable - thanks Dirk!

Dirk Goldgar said:
If you know the code is running on a subform, you can take a shortcut by
referring to the subform's Parent property:

Me.Parent!ControlName

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

(please reply to the newsgroup)
 
G

Guest

Thanks for your help,

I tried the following but doesn't work:

DoCmd.GoToControl Me.Parent!FindPO

thanks again,

Emilio
 
D

Dirk Goldgar

Wind54Surfer said:
Thanks for your help,

I tried the following but doesn't work:

DoCmd.GoToControl Me.Parent!FindPO

No, that wouldn't work, because DoCmd.GoToControl requires the *name* of
the control (a string value), whereas that is passing a reference to the
control object itself. A better way to do it would be this:

Me.Parent!FindPO.SetFocus
 
G

Guest

I tried that and I get:
Compile Error:
Invalid use of Me keyword

In help explains it can only be used in Class Modules, I have no idea how to
do that.
Can you please help me.

Thanks again,
Emilio
 
D

Dirk Goldgar

Wind54Surfer said:
I tried that and I get:
Compile Error:
Invalid use of Me keyword

In help explains it can only be used in Class Modules, I have no idea
how to do that.
Can you please help me.

I'm sorry, I didn't realize your code wasn't running in the form's
module. Form and report modules are class modules, while standard
modules aren't. Looking back over the older messages in this thread, I
understand better, but I need to know more about what exactly you want
to do and what error you were getting in the first place. As far as I
can tell, that original code, once invoked, should work if there is a
control on the active form named "FindPO".

Are you calling the function from your AutoKeys macro? If so, did you
consider just using the AutoKeys macro itself to execute the GoToControl
action?
 
G

Guest

I want to be able to press F6 and go to the combo box "FindPO" in the form,
when the form first open no problem but if the focus is in any control in any
of the subforms it just doesn't do anything.
I converted the original macro to a function and would like to keep it like
that if possible.
I believe that in order to work I have to specify the name of the form but
because I use the function in several forms I want to make it more general.
That is what I want to do.

I hope that explains better.
Thanks again,
Emilio
 
G

Guest

I forgot to explain why I don't want to use the macro Autokeys.

Everytime I press F6 (or any other Autokey) in a form that does not apply I
get errors.

Any help really appreciated,
Emilio
 
D

Dirk Goldgar

Wind54Surfer said:
I forgot to explain why I don't want to use the macro Autokeys.

Everytime I press F6 (or any other Autokey) in a form that does not
apply I get errors.

Any help really appreciated,
Emilio

So how are you calling the function? Is it from code in a the form's
KeyDown event (with the KeyPreview property set to Yes)? Or some other
way? If you can give me the complete picture -- everything you have
done and all the code that is related to this feature you're trying to
implement -- I think I can help you. But so far I only have pieces of
it, and I'm not sure what is currently in effect and what is outdated.
 
G

Guest

This is how:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyF6 Then
AutoKeysF6
End If

End Sub

Thanks again,
Emilio
 
D

Dirk Goldgar

Wind54Surfer said:
This is how:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyF6 Then
AutoKeysF6
End If

End Sub

I think the problem is that the main form's KeyDown event doesn't fire
when the subform has the focus -- only the subform's KeyDown event fires
(provided that the subform's KeyPreview property has been set to Yes).
So to make this work, you have to have the same KeyDown event procedure
defined in the subform, and have the subform's KeyPreview property set
to Yes, too.
 

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