[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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?
0
TGB
Asked:
TGB
  • 3
  • 2
  • 2
  • +4
1 Solution
 
TimCotteeCommented:
Here is one way of doing it:

    Forms.Add Text1.Text
    For Each Form In Forms
        If Form.Name = Text1.Text Then Form.Show
    Next

This adds the form to the forms collection, finds the form in the collection and displays it.
0
 
Erick37Commented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PBuckCommented:
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
0
 
viveksarinCommented:
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.
0
 
ArgonautCommented:
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.
0
 
TGBAuthor Commented:
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)
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
TGBAuthor Commented:
Cheers for the help. ;)
0
 
Erick37Commented:
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
0
 
Erick37Commented:
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
....
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now