Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load form from a string variable

Posted on 1999-08-03
12
Medium Priority
?
164 Views
Last Modified: 2012-05-04
sForm = "frmTest"

How do you load frmTest using sForm?
0
Comment
Question by:northeyk
[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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 1529209
Don't know if this is what you are lookin' for but this code loads up a form as "variable" sForm..

Dim sForm as frmTest
sForm = New frmTest
sForm.Visible = True

0
 
LVL 2

Expert Comment

by:taplin
ID: 1529210
If you are working in VB6, you can use:

   CallByName FormName, "Show", VbMethod


0
 

Author Comment

by:northeyk
ID: 1529211
I have a table containing a field called "FormName".  The user selects the form from a grid and the form should load based on the contents of the field.  Currently, I'm doing this:

Select Case rsForms!FormName
   Case "frmTaxEffects"
      frmTaxEffects.Show
   Case "frmCMT"
      frmCMT.Show
   Case "frmT2S1"
      frmT2S1.Show
   ...
   ...
End Select

Seems like a lot of code for a simple routine.  There are dozens of forms.

0
Technology Partners: 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!

 
LVL 15

Expert Comment

by:ameba
ID: 1529212
' requires VB6/SP3
' add Form2 to your project
' Form1 code
Option Explicit

Private Sub Form_Click()
    Dim x As Form
    Set x = Forms.Add("Form2")
    x.Show
End Sub
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 1529213
And if you don't have VB6 you can create a array containing the form names and reference to them. Something like this..

'Place the type structure in the declarations
  Private Type FormArray
    Name As String
    Reference As Object
  End Type
  Private FormArray() As FormArray

'Creating references to forms
  ReDim FormArray(2)
  FormArray(1).Name = "Form2"
  FormArray(2).Name = "Form3"
  Set FormArray(1).Reference = Form2
  Set FormArray(2).Reference = Form3

'Using the array
  Sub ShowForm(Name) As String
    For a = 1 to Ubound(FormArray)
      If (FormArray(a).Name = Name) Then
        FormArray(a).Reference.Show
        Exit For
      End If
    Next
  End Sub


This way you will have to create references to all the forms in the Load procedure or maybe the very first time the end user wants to show a form for faster startup of your app. The ShowForm procedure will of course have to be optimized with binary search but that's so easy even a child can do it. ;)


0
 

Author Comment

by:northeyk
ID: 1529214
Won't work.

The first parameter passed is the object (form).  You can't pass the object name as a string.

This works:
CallByName frmTaxEffects, "Show", VbMethod

This doesn't:
CallByName "frmIncomeTax", "Show", VbMethod

I don't want to have any of the form names in the code.  They are chosen from a table (see my previous comment).

0
 

Author Comment

by:northeyk
ID: 1529215
Thanks, ameba.  Works great.


0
 
LVL 15

Expert Comment

by:ameba
ID: 1529216
>Works great
You do have SP3 for VB6, do you? If you don't, that won't work when compiled.
0
 

Author Comment

by:northeyk
ID: 1529217
Yes I do.  I compiled it and it works.  Cut out a honkin Select Case.  Thanks again.
0
 
LVL 15

Accepted Solution

by:
ameba earned 400 total points
ID: 1529218
Private Sub Form_Click()
    Forms.Add("Form2").Show
End Sub

or

Private Sub Form_Click()
    Dim x As Form
    Set x = CallByName(Forms, "Add", VbMethod, "Form2")
    x.Show
End Sub
0
 

Author Comment

by:northeyk
ID: 1529219
Adjusted points to 100
0
 
LVL 15

Expert Comment

by:ameba
ID: 1529220
Wow! Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

670 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