Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare variable holding a UserForm ?

Status
Not open for further replies.

HelgeLarsen

Programmer
Mar 18, 2004
16
DK
I would like to declare a variable representing a userform. If I simply declare it as OBJECT, things function.
But if I declare it as USERFORM, I get an error when showing the userform.
Run-time error '438': "Object doesn't support this property or method".
Do I really have to declare it as a general OBJECT ?

I have enclosed some VBA to show my problem.

Kindly,
Helge
______________________________

Sub Test0()
Load UserForm1
UserForm1.Show ' No variable : OK
End Sub

Sub Test1()
Dim myUF As Object
Set myUF = UserForm1
Load myUF
myUF.Show ' Object : OK
End Sub

Sub Test2()
Dim myUF As UserForm
Set myUF = UserForm1
Load myUF
myUF.Show ' UserForm : Error 438
End Sub

Sub Test3()
Dim myUF As MSForms.UserForm
Set myUF = UserForm1
Load myUF
myUF.Show ' MSForms.UserForm : Error 438
End Sub
 
how about

Dim myUF As New UserForm1

you dont specifically delare it as a type, but i presume the boiler plate does it for you in the background?

perhaps you can retrun the Type afterwards to check what happened...
 
or more specifcally

Dim mUF As UserForm1

bit weird, i would have thought UserForm would have worked, hoo hum
 
When you create a UserForm, the new object added is, say, UserForm1. In reality, a new class named UserForm1 was added to VBA project. What we can see in VBE is visual class designer, showing how the object created basing on the class (UserForm1) will look like in run-time. When the name of form is changed, the name of class behind is also changed.
While displaying the form, VBA does a lot behind the scene, creates new variable named UserForm1, allows responding to controls' events (like WithEvents declarations) etc.

As an explanation to the above:
It is possible to display two userforms with different names:
Dim frm1 As UserForm1, frm2 As UserForm1
Set frm1 = New UserForm1
Set frm2 = New UserForm1
frm1.Show vbModeless
frm2.Show vbModeless

A Userform is a window, that represent only loaded forms.
Msgbox UserForms.Count ' will return 2 after the above
This is a way to get run-time design mode (via hidden property), this doer not change the original form:
Dim uf As UserForm
Dim frm1 As UserForm1
Set frm1 = New UserForm1
frm1.Show vbModeless
Set uf = UserForms(0) ' collection starts counting from 0
uf.DesignMode = fmModeOn

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top