Solved

Loading Forms

Posted on 2000-02-27
4
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

696 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