Showing a form!!!

OK - I have the name of a form in a string variable (tmpForm = "frmForm" for example).  How do I show it?  I cannot loop through the Forms collection as this is a collection of loaded forms - the form I want to display may not be loaded yet.
DaveWaltonAsked:
Who is Participating?
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.

mcriderCommented:
You can use a case statement to load the form... For example:


Select case FormStringName
    Case "Form1": Form1.show
    Case "Form2": Form2.show
    case "Form3": Form3.show
End Select


Cheers!
0
ventondCommented:
Unfortunately VB does not have referential execution. The closest you can come is:

Select tmpform
case "frmForm"
   frmForm.Show
....
end select
0
mcriderCommented:
YEP, THATS MY ANSWER!
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mark2150Commented:
Yes, you can specify the form name:

Public FormName As Form
....
Set FormName = OtherForm
FormName.Show

You can use this as a variable return:

Set FormName = Me
CommonForm.Show

Then in CommonForm's exit routine code:

Me.Hide
FormName.Show

And the "parent" form will reappear.

M
0
amebaCommented:
' requires VB6/SP3
' add Form2 to your project
' Form1 code
Option Explicit

Private Sub Form_Click()
    Dim x As Form
    Set x = Forms.Add("Form2")
    x.Show
End Sub
'or
Private Sub Form_Click()
    Forms.Add("Form2").Show
End Sub
'or
Private Sub Form_Click()
    Dim x As Form
    Set x = CallByName(Forms, "Add", VbMethod, "Form2")
    x.Show
End Sub
0
DaveWaltonAuthor Commented:
MCRIDER, I was aware of that but I do not want any actual reference to the form because at the time I compile my project the form may or may not be included.  The information relating to available forms is stored externally to my application and therefore I do not want any reference to it other than via a string variable.

MARK2150, I'm not sure that your solution is what I'm looking for.  I simply want to display a form (any form) knowing only its name as a string variable.

AMEBA, Unfortunately I am using VB5 SP3 at the moment with no immediate plans to upgrade.

0
caraf_gCommented:
I'm afraid mcrider's suggestion is the best that can be done in VB.

Apart from that, I would recommend not using direct reference to the name of the form:

FormX.SomeControl

Instead, use a slightly more "object oriented" approach:

Dim objFormX As FormX

'When an instance of the form is needed
'Instantiate the form
Set objFormX = New FormX
'Load the window
Load objFormX
'Do stuff to the controls on the form
objFormX.SomeControl.DoSomethingToTheControl

'And finally, when you don't need the form anymore
Unload objFormX
Set objFormX = Nothing

If you show the form modally you might be able to get away with declaring the object variable inside a procedure, otherwise you'll have to declare it at module level

Not only is this easier on your memory resources, but because of its structured approach it encourages better programming practices. For example, with little dialog forms this technique ensures you'll never show a dialog with redundant data from the previous session still in it. If dialogs are to be shown in different "modes", handling these will be easier, etcetera.
0
caraf_gCommented:
You can even have the main form handle events from a child form if you declare the object variable at module level and use WithEvents. But that goes beyond the scope of this question. If you have time it's worth looking into, though!
0
vettrangerCommented:
If you're using VB6, don't let ameba's solution get lost in the middle of all these comments. The CallByName function is designed for just what you want to do. I'd actually tried to do it a different way (using callbyname with the form name as the first parameter, and telling it to use the Show method. That didn't work, so I was very interested to see the spin ameba put on that process.
0
VbmasterCommented:
You mentioned you don't know if the form is included into your project or not, you could use #statements to exclude some stuff depending on if you have the form included or not.. ehh something like this..

Select case FormStringName
#If UseForm1 Then
   Case "Form1": Form1.show
#End If
#If UseForm2 Then
   Case "Form2": Form2.show
#End if
End Select

etc adding these #if..#endif statements for the form that is or is not included to your project

Then you go to the menu item Projects -> Properties.. -> tab "Make" -> set "Conditional Compilation Arguments" to..

UseForm1 = 1: UseForm2 = 1

to tell the compiler to include all statements inside the #If UseForm1..#EndIf and #If UseForm2..#EndIf statements. Or

UseForm1 = 1

to only include statements inside the #If UseForm1..#EndIf.


Argh.. too much info for me to read again and correct. Hope you understand at least something of it. ;)
0
VbmasterCommented:
If you want to use CallByName and don't use VB6, tell me and I can put up some files on my homepage, that gives you access to CallByName in VB5. ;)

0
caraf_gCommented:
ameba, does that work outside the IDE now?
0
caraf_gCommented:
Dave,

Why not put those forms that may or may not be included into DLLs and use a class wrapper to interact with the main .exe?
0
amebaCommented:
>ameba, does that work outside the IDE now?
Yes, with VB6 SP3
0
amebaCommented:
Vbmaster, CallByName is not useful here - Forms collection in VB5 does not support "Add" method.
0
DaveWaltonAuthor Commented:
Vbmaster, Yes - I'm interested in using CallByName in VB5, maybe not for this problem but would be good to know. Let me know where your homepage is - or you can email the files to me:

davew@freedomcomputers.co.uk
0
DaveWaltonAuthor Commented:
Vbmaster, I do understand your conditional compilation arguments solution and that looks like it will do the job for me.  Still would like to see your CallByName solution though.  I'll grade your comment as an acceptable answer if you can see your way clear to letting me have the files.
0
VbmasterCommented:
Una zip sent to ya email.
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
DaveWaltonAuthor Commented:
Thanks very much.
0
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.