Solved

Loading Forms

Posted on 2000-02-27
4
161 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

776 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