How to Load Form using Variable for Name

S

= Stuart =

I have a project I'm working on that will have lots and lots (20-30 or
more) UserForms. As such, I don't want to pre-load them. Instead, I
want to load them as needed. To do so, I've created a combo box on
each form that lets the user choose what form to go to. The User
picks one from the list then clicks on "OK" button. I then want to
load that form and show it. To make it one bit harder my forms are
all named "frm" + Form name.

For example the forms are named, frmMyForm, frmYourForm, frmHisForm
and the combo box shows "MyForm", "YourForm", "HisForm"

The code I have so far for the OK click routine is:

Private Sub cmdChoose_Area_Click()
vChosen = Me.cmbChoose_Area.Value
vName = "frm" + vChosen

Call FormGoTo(Me.Name, vName)
End Sub

In FormGoTo I have the receiving parameters (vFormFrom, vFormTo)

No matter what I do I can't Load "vFormTo".

I've tried
FormGoTo (vFormFrom, vFormTo as UserForm)

but then I get an error from the calling procedure.

I've tried:
Load vFormTo
Load UserForms(vFormTo)
etc, but with no luck.

Is there any way to do this?
 
D

Doug Robbins - Word MVP

There may be a better way, but you could create a subroutines to load each
form and call the appropriate subroutine based on the item selected from the
combobox.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jonathan West

= Stuart = said:
I have a project I'm working on that will have lots and lots (20-30 or
more) UserForms. As such, I don't want to pre-load them. Instead, I
want to load them as needed. To do so, I've created a combo box on
each form that lets the user choose what form to go to. The User
picks one from the list then clicks on "OK" button. I then want to
load that form and show it. To make it one bit harder my forms are
all named "frm" + Form name.

For example the forms are named, frmMyForm, frmYourForm, frmHisForm
and the combo box shows "MyForm", "YourForm", "HisForm"

The code I have so far for the OK click routine is:

Private Sub cmdChoose_Area_Click()
vChosen = Me.cmbChoose_Area.Value
vName = "frm" + vChosen

Call FormGoTo(Me.Name, vName)
End Sub

In FormGoTo I have the receiving parameters (vFormFrom, vFormTo)

No matter what I do I can't Load "vFormTo".

I've tried
FormGoTo (vFormFrom, vFormTo as UserForm)

but then I get an error from the calling procedure.

I've tried:
Load vFormTo
Load UserForms(vFormTo)
etc, but with no luck.

Is there any way to do this?

What is the declaration line of routine FormGoTo?
 
S

= Stuart =

What is the declaration line of routine FormGoTo?

It is:
Sub FormGoTo(vFrom, vTo)

I've tried
Sub FormGoTo(vFrom, vTo as UserForm)

but then I got an error from the calling procedure:
Call FormGoTo(Me.Name, vTo)

I tried:
Call FormGoTo(Me.Name, vTo as UserForm)
but that didn't work as vTo is a string

Right now, the kinda brute force workaround I came up with is using a
Select Case in FormGoTo like this:

Select Case (vTo)
Case "MyForm"
frmMyForm.Show
Case "YourForm"
frmYourForm.Show
Case "HisForm"
frmHisForm.Show
End Select

It works, but to say the least is less than elegant and will get
cumbersome as I get more and more forms.
 

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