Solved

Loading Forms

Posted on 2000-02-27
4
159 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
Comment Utility
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
Comment Utility
 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 69

Expert Comment

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

Accepted Solution

by:
wsh2 earned 50 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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 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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now