Solved

Loading A Form

Posted on 2000-04-05
11
207 Views
Last Modified: 2010-05-02
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
Comment
Question by:TGB
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 2686898
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2686945
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2687151
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
 
LVL 4

Expert Comment

by:PBuck
ID: 2687258
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
 

Expert Comment

by:viveksarin
ID: 2687625
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Expert Comment

by:Argonaut
ID: 2687677
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
 

Author Comment

by:TGB
ID: 2689781
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2689837
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
 

Author Comment

by:TGB
ID: 2690278
Cheers for the help. ;)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2690502
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2690518
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

21 Experts available now in Live!

Get 1:1 Help Now