Link to home
Start Free TrialLog in
Avatar of TGB
TGB

asked on

Loading A Form

I'm trying to write an application that will load a form, based on what a user may type into a text box.
So if the value of the text box was "frmAccounts", that form would be loaded.
The trouble I'm having seems to be that VB wont allow the command text1.show vbmodal, because text1 is a variable.
frmAccounts.show vbmodal works fine however.
Can anyone help me please?
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe it would be better to use a Combobox or Listbox to display forms which are available to load.  If the user types in a non-existent form, then you have to error trap.
Avatar of Guy Hengel [angelIII / a3]
TimCottee: to use the forms collection, you need the forms all to be loaded already, even if not visible (hint)
Erick37: you should ALWAYS use Error Trapping, loading a form may raise an error...

TGB:
You might try this

DIM frm as Form
set frm = CreateObject ( "YourprojectName." & text1 )
frm.Show

Avatar of PBuck
PBuck

Another suggestion is to use the Case Statement.

Select Case Text1
    Case "frmSTART"
        frmSTART.Show vbmodal
    Case "frmACCOUNTS"
        frmACCOUNTS.Show vbmodal
    Case "frmDEBITS"
        frmDEBITS.Show vbmodal
    Case Else
        MsgBox "Form not found."
End Select

*Don't forget to use the Set frmACCOUNTS = Nothing 'to free all resources on that form in the Unload.

Hope this Helps some!  Good Luck
best way to build a application is to hide internal details from the operators and in that try to use combobox or listbox. dont use any other mean of making projects. mind you you have text box as object in VB not variable. so textbox has properties like visible. which could be set to true or false.
AngelIII: I've have been fighting a similar problem and thought CreatObject would work ... but it doesn't, and I can't explain why.  I can get it to work with Classes in a registered DLL but not a form.  Have you gotten this to work?

TimCottee: This will work but can be a real problem if you have lots of objects to loop through.  Forms shouldn't be a problem.

TGB:  I would vote for TimCottee's solution for your case.  In my case I'm looping through over 1,000 objects on a designer and trying to reference them with a string name.  I have not found a solution yet.  I can do this on a form without a problem like this:

Me("StringNameOfControl").property

I also did some testing using the CallByName function.  This is great if the property or method you want is a string but you still need a reference to the object which is the original problem.
Avatar of TGB

ASKER

Cheers for the advice, but for Erick37  I have a question: I'm trying to add all the forms in the forms collection to a list box, but it's only picking up the form name from which the code is being run. how do I get it to pick up the name of other forms that are available within the project?
(Sounds daft, but I have limited VB knowledge..... that's what you get when you're trying to build a Clent Server app' and trying to learn a new language from a book)
Sorry for the CreateObject example, as I forgot that i changed things in my projects which made that it worked.
I found that it is not possible do use CreateObject with forms, because forms are not typical VB Classes, but window objects provided by the OS (Win9x, Win2K, WinNT)...

In fact i have collection of objects (of different class types, but implementing a given interface), they all have their proper form reference, and they all know how to load and show and initialize the form.
This solved several problems:
* I could handle properties without really loading forms
* I could handle events better, even with multiple occurences of the form
* I could put the available forms into my collection, in order to access them by name (similar to your situation)
* I could create "forms" on demand.

This solution however is basically the same as the one with combobox and select case construct, but the

Avatar of TGB

ASKER

Cheers for the help. ;)
Here is a little mod to Tim's code.  This will catch invalid forms and prevent loading multiple instances of the same form:

Private Sub Command1_Click()
    Dim frm As Form
    For Each frm In Forms
        'first test if it is already loaded
        If frm.Name = Text1.Text Then
            frm.Show
            'Exit once we have a match
            Exit Sub
        End If
    Next
    'If not listed, then add it
    On Error GoTo NOFORMERR
    Forms.Add Text1.Text
    For Each frm In Forms
        'first test if it is already loaded
        If frm.Name = Text1.Text Then
            frm.Show
            Exit For
        End If
    Next
    Exit Sub
NOFORMERR:
    If Err.Number = 424 Then
    MsgBox "There is no " & Chr(34) & Text1.Text & Chr(34), vbInformation
    Else
    MsgBox Err.Description
    End If
End Sub
This is a little better than using the second For loop:

....
'If not listed, then add it
On Error GoTo NOFORMERR
Forms.Add Text1.Text
Forms(Forms.Count - 1).Show
Exit Sub
....