UserForm Variable

  • Thread starter Ronald R. Dodge, Jr.
  • Start date
R

Ronald R. Dodge, Jr.

Excel 2002, SP3

Based on the research I have done thus far, I don't like what I see as it
breaks away from good programming practices and it also relies on late
binding rather than early binding.

When declaring userform variables, it must be assigned to the "Object" type
to be able to use a few different things like the Show method. While using
"Object" is slightly better, it's still almost as bad as using "Variant"
cause it doesn't specify the "TYPE" of object.

Why does the VBA UserForm Object not match up to the UserForm that's created
within Excel VBA. If it's cause the actual UserForm is only for Excel, then
why does the Excel codes not have a UserForm object that can be used
programmatically?

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
 
P

Peter T

When declaring userform variables, it must be assigned to the "Object"

If you want to assign some unknown userform to an Object variable, then yes,
you will need to declare as variant or as object. Alternatively, if you know
in advance which userform you want to assign then do
Dim uf1 as Userform1

You'd need to do exactly the same if you want to assign an ordinary class
module to a variable. As Object or As Class1 but not As Class (a userform is
a class module, albeit a custom one provided by the MSForms library)

You could do
dim uf As Userform
set uf = [new] Userform1

but be careful as 'uf' will not be assigned to the representation of the
form you expect, it will have virtually none of the methods that only become
available once the form has become a member of your project, or rather it
may have apparently similar methods but to different aspects of the object.
IOW, there's a difference between the form as a member of MSForms (uf) and
as a member of your project (uf1).

Regards,
Peter T
 
R

Ronald R. Dodge, Jr.

I guess I will just have to live with using this Object bit as I'm not about
to DE-modulate my code just to satisfy the specific declaration aspect of
good programming practices. Not only that, but at least the Object bit is
still allowed in the .NET environment, should MS decide to move VBA that
way, where as Variant isn't allowed in the .NET environment.

As for the Userform being a class module, I thought about that just like the
Form object within Access is also a class module.

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
Peter T said:
When declaring userform variables, it must be assigned to the "Object"
type

If you want to assign some unknown userform to an Object variable, then
yes, you will need to declare as variant or as object. Alternatively, if
you know in advance which userform you want to assign then do
Dim uf1 as Userform1

You'd need to do exactly the same if you want to assign an ordinary class
module to a variable. As Object or As Class1 but not As Class (a userform
is a class module, albeit a custom one provided by the MSForms library)

You could do
dim uf As Userform
set uf = [new] Userform1

but be careful as 'uf' will not be assigned to the representation of the
form you expect, it will have virtually none of the methods that only
become available once the form has become a member of your project, or
rather it may have apparently similar methods but to different aspects of
the object. IOW, there's a difference between the form as a member of
MSForms (uf) and as a member of your project (uf1).

Regards,
Peter T


Ronald R. Dodge said:
Excel 2002, SP3

Based on the research I have done thus far, I don't like what I see as it
breaks away from good programming practices and it also relies on late
binding rather than early binding.

When declaring userform variables, it must be assigned to the "Object"
type to be able to use a few different things like the Show method.
While using "Object" is slightly better, it's still almost as bad as
using "Variant" cause it doesn't specify the "TYPE" of object.

Why does the VBA UserForm Object not match up to the UserForm that's
created within Excel VBA. If it's cause the actual UserForm is only for
Excel, then why does the Excel codes not have a UserForm object that can
be used programmatically?

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
 

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