Loading Forms

I am trying to load a form in VB using its name as a string. Is this possible?

I have a function that returns a string which is the name of a form ie "mdiRegistration" but I need to know how to load the form using this string.

Something similar to Controls("Command1").Caption but with loading a form.

Thanks.
greenoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
The reason the Controls("Command1") thingie works is because all controls are loaded into the Controls array when the form is loaded. The same system is used in the Forms array, you can only access forms that are loaded into the Forms array (when you use the Load statement or similar). The only way to load forms by it's name is, because of this, using a SelectCase..EndSelect block like...

  Select Case formnametoload
  Case "frmMain"
    Load frmMain
  Case "frmAbout"
    Load frmAbout
  .
  more case statements here
  .
  End Select
0
MaquiaveloCommented:
 No way! If you take a look at the Load function you'll see that I expects an Object as parameter.

  Try sth like this:

dim f as object

set f=createoject("mdiRegistration")
f.show

  I'm not sure if ít'd work... sorry but my help doesn't want to work today.
0
Éric MoreauSenior .Net ConsultantCommented:
Look at the CallByName statement in the VB help file.
0
wsh2Commented:
VbMaster certainly has it right.. testing the string variable and then branching to the appropriate load/show statement.. it is simple to do, very straightforward.. and can be all contained within one procedure.. <smile>.

The Callbyname technique previously suggested, requires a hard coded  ObjectName as its first parameter.. as this the form name.. there really is not much difference between it and a Load form statement. The freedom of variable string usage in the Callbyname function, is reserved to specifying Property and Method names.

The Createobject methodology is used when loading external objects.. ones that are in another COM component other than your program. Although.. to be honest.. I have never tried to late bind my own program.. even if it is possible.. you would then have to contend with the additional object linking overhead.. and really.. who needs it?

One other technique you could use to SHOW a form by the contents of a variable, is to use the Forms collection. Mind you.. for this to work ALL OF THE FORMS HAVE GOT TO BE ALREADY LOADED. In this technique, you iterate through the VB forms collection testing for something UNIQUE.. such as a particular value of a tag or caption. For example, lets say you have a form entitled with MY FORM in its caption.. you would then code the following..

Dim BooFind as Boolean
Dim frmFind as Form
Dim strFind as String
strFind = "MY FORM"

For Each frmFind in Forms
   If Ucase(strFind) _
   = Ucase(frmWork.Caption) _
   Then
      booFind = True
      Exit For
   End If
Next frmFind

If BooFind = True
   frmFind.Show
Else
   ' Error processing
End If

Once again, this code is predicated upon ALL FORMS ARE ALREADY LOADED.

All in all, my recommendation to you is to go with VbMasters solution.. the test and select method.  As the old saying goes.. K.I.S.S... <smile>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.