?
Solved

Indirect reference a form name

Posted on 1999-01-27
11
Medium Priority
?
170 Views
Last Modified: 2010-05-03
I want to use a common routine to show and unload forms. The name of the next form to process is stored in a database. I save this name in a string variable. How can I later reference this name to show the form? The unload is easy - I simply walk the Forms collection until I find a matching name. But I can't figure out how to do something like "nextForm.show" (where nextForm contains the name of the form).
0
Comment
Question by:chucka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Expert Comment

by:dabellei
ID: 1470922
Dim nextForm as form
set nextform = nameoftheform
nextForm.show
0
 

Author Comment

by:chucka
ID: 1470923
No, that won't work. I had already tried it - gives a Type Mismatch. I did the following:

dim nextShow as form
set nextShow = rs("show") ' Contains the name of the next form
0
 

Author Comment

by:chucka
ID: 1470924
There must be a collection of Forms in the project (not the loaded forms collection). Perhaps I could walk that list and locate the form that way maybe, if I knew where to find it?? There has to be a way to reference a form through a variable rather than explicitly by it's name in code.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:traygreen
ID: 1470925
The form instances are aded to the collection as they are created.  
I am not aware of anyway you can typecast the string in such a way that your set statement will work.
The only thing I could suggest is this...
Add a type filed to your table (or use your name field)
Then in your nextShow put in a case statement eg...
Select Case rs("show")
  Case "Form1"
    set nextShow = Form1
0
 

Author Comment

by:chucka
ID: 1470926
That would work and there's an easier way to do it, but that's what we are trying to get away from - that's the way it's being done now....... There are about 160 forms in this project! There are several Select Case structures in various modules to deal with all these forms and we want to simplify things.
0
 
LVL 10

Expert Comment

by:brewdog
ID: 1470927
You said the name of the next form in the process was stored in the database? In a table? Could you set up a recordset based on that table and then do one of these each time?

    rs.MoveNext
    strName = rs(0)
    Set nextForm = Forms(strName)
    nextForm.Show

Would that work?

brewdog

0
 

Author Comment

by:chucka
ID: 1470928
Nope - Type Mismatch - I tried something like that earlier. First, the form is not loaded so not in the Forms collection. Second, I think you ned to use an index, not a name to access the collection.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 1470929
I'm not at a machine where I can test this but how about trying:

dim nextShow as form
set nextShow = CreateObject(rs("show"))
nextShow.Show

MD
0
 

Author Comment

by:chucka
ID: 1470930
Got "ActiveX component can't create object". I implemented your suggestion as :

    Dim nextSh As Form
    Set nextSh = CreateObject(nextShow)
    nextSh.Show

Note: Nextshow was set previously to the name of the form.

Thanks for the help. I think this may not be possible (although it should). I'm out of town until Saturday from now. I'll check back on any other suggestions then. Thanks to all.
0
 
LVL 2

Accepted Solution

by:
VBDesigns earned 400 total points
ID: 1470931
Thanks to Mike Dixon from Microsoft for this response:

You should be able to use Forms.Add, but it is broke. The bug has been isolated and fixed. The fix will not be available for several months, as it requires a service pack release for updating MSVBVM60.DLL. Please note that there is always a possibility that the bug could become "unfixed" or testing could reveal a need to change it back. (In this case, I highly doubt that will happen based on the reason behind the bug.) I have tested it against an internal build and it does work in the exe now.

In the mean time, the workaround mentioned in Q194912 will have to be used
if you need to ship the application or to run it as an exe. Below is another
code sample that may work better for you. Since each form is known by the
programmer when they add it to the project, all they need to do is add
another Case Statement to the LoadFormByString function for the new form:

Private Sub Command1_Click()
    Dim frm As Form
    Set frm = LoadFormByString("Form2")
    if frm is nothing then
        'Form was not loaded
    else
        frm.Show
    End If
End Sub

Public Function LoadFormByString(sFormName As String) As Form
    'Each time a new form is added to the project, add the code here:
    Select Case sFormName
        Case "Form2": Set LoadFormByString = New Form2
        Case "Form3": Set LoadFormByString = New Form3
        Case "Form4": Set LoadFormByString = New Form4
    End Select
End Function

0
 
LVL 3

Expert Comment

by:covington
ID: 1470932
Use this:

(I'm commenting because I believe that VBDesigns' answer is bascially the same as a previuously rejected answer...)

Note that the return value of GetForm must be declared as a variant (or object) if it is a public procedure - VB won't let you return a Form from a public procedure.

Now you can say:

GetForm(rs("show")).Show

You'll get an Err 91 if the forms hasn't been loaded.


Public Function GetForm(FormName as String) As Variant

    Dim frm As Form
    For Each frm In Forms
        If frm.Name = FormName Then
            Set GetForm = frm
            Exit Function
        End If
    Next

End Function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

764 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