Solved

Loading Forms

Posted on 2000-02-27
4
163 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:greeno
4 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2563165
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
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2563185
 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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2563352
Look at the CallByName statement in the VB help file.
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2563889
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question