Solved

Loading A Form

Posted on 2000-04-05
11
209 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 63
VBA error replacing data 6 39
How does CurrentUser work? 10 36
IF ELSE Statement in Excel Macro VBA 16 62
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 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

9 Experts available now in Live!

Get 1:1 Help Now