passing UserForm to a Sub

E

Enda

Hi,
Could someone please offer some advice on the following? I am trying
to keep my UserForm code separate from the UserForm. I want to pass a
reference to the UserForm to a Sub in another generic module for
manipulating UserForms. I have used the following code in a Sub of the
UserForm.

Private Sub UserForm_Activate()
Dim form As UserForm
Set form = Me
Call modForm.Activate(form)
End Sub

After setting form=Me, I can no longer properties such as
form.Caption. How can I achieve a correct reference to a UserForm and
pass this to other modules?

Regards,
Enda Ridge
 
J

Jezebel

What you're doing is fine, except that 'form' is a reserved word, so using
it as a variable name will cause problems. In any case you don't need a
variable here at all:

modForm.Activate Me

is functionally identical, assuming that modForm contains something like

Public Sub Activate (oForm as UserForm)
 
P

Peter Hewett

Hi Jezebel

Something really flakey is going on here (yet another one!). I've been playing with this
code for about 45 minutes now! And still can't get it to work correctly.

When you set the caption of a Form using a reference of anything other than Me the Caption
text appears in the actual From area!!! I haven't tested absolutely every property, but
it seems specific to the Caption property.

Try the following code in a Form:

Private Sub UserForm_Click()
Dim frmX As MSForms.UserForm

Set frmX = Me
frmX.Caption = "Oh shite"
End Sub

This occurs for both Word 2k and XP. I haven't done a KB search on this "feature".

Cheers - Peter


What you're doing is fine, except that 'form' is a reserved word, so using
it as a variable name will cause problems. In any case you don't need a
variable here at all:

modForm.Activate Me

is functionally identical, assuming that modForm contains something like

Public Sub Activate (oForm as UserForm)

HTH + Cheers - Peter
 
M

murdoch

Yup, some collections are different to others! Try "Dim
frmX As Object" instead of "Dim frmX As MSForms.UserForm".
You won't get the autocomplete help when entering code but
it does work a bit better!


-----Original Message-----
Hi Jezebel

Something really flakey is going on here (yet another
one!). I've been playing with this
code for about 45 minutes now! And still can't get it to work correctly.

When you set the caption of a Form using a reference of
anything other than Me the Caption
 
J

Jezebel

Flakey indeed. Declaring the variable 'as object' certainly works. As does
declaring the variable 'as UserForm1' (or whatever the form is called) --
although that won't help for passing the object as a parameter, since
presumably the target function has to work on all forms.

But to compound the issue, experimenting with this appears to have disabled
all my function keys. I hate user forms. Yet another good reason for
sticking with plain VB rather than VBA.
 
E

Enda

Thank you all for your help.

I found
Dim form as Object
rather than as UserForm avoids whatever the problem was.

Regards,

Enda
 

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