?
Solved

show from by using object variable

Posted on 2003-03-19
9
Medium Priority
?
224 Views
Last Modified: 2010-04-17
Greeting,

   In conventional, I use

      formName.show

to show a form. I just want to ask can i declare a variable to store form name can show the form by showing the variable ?

If yes. How ?

Thanks for replying ......

0
Comment
Question by:PandaYong
[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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:TheAmigo
ID: 8172240
If you have a variable (strFormName) containing the name of a form, you can open the form like this:

docmd.openform strFormName

Then hide/show it like this:

Forms(strFormName).visible = true
Forms(strFormName).visible = false

Does that do what you want?
0
 
LVL 6

Expert Comment

by:TheAmigo
ID: 8172242
Ok, I pasted in the wrong window... ignore that.
0
 
LVL 3

Expert Comment

by:VBtom
ID: 8172270
It's much easier to use a object variable with class Form than using as tring variable with the formname:

Declare in a module:
Public frm As Form

Make this object variable point to a certain form, e.g.:
Set frm = Form1

Then you can show a form with
frm.Show

(if for some reason you need the name of the form frm is set to, use frm.Name)
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!

 

Author Comment

by:PandaYong
ID: 8172301
VBTOM, I tried that, your method is working well if u specified your form directly.

what if the form name is within a string variable
something like below,


Private Sub Command1_Click()
    Dim abc As Form
    Dim var1 As String

var1 = "form2"
Set abc = var1
abc.Show
 


in this case your method will give me error.

please advice. thanks
   
End Sub
0
 
LVL 3

Expert Comment

by:VBtom
ID: 8172353
If the form is loaded in memory (it doens't need to be shown), you can access it with its name using the forms-collection.
Forms are loaded in memory (without showing) if you ask or set any property or use any method, or when you explicitly say Load Form1.
When your application is not too heavy, you could consider loading all forms (in Sub Main) when starting your program.
Then this should work.

Private Sub Command1_Click()
   Dim abc As Form
   Dim var1 As String

var1 = "form2"
Set abc = Forms(var1)
abc.Show




0
 

Author Comment

by:PandaYong
ID: 8172381
VBTom ,

  I still got an error saying "type mismatch"
when i am running the code.

unfortunately, my application is a big one. i think your suggestion couldn't be accepted.

is there any other way to do so ?

0
 
LVL 27

Accepted Solution

by:
Dabas earned 1000 total points
ID: 8173087
This snippet might help you out:

Create three forms, Form1, Form2, Form3

-----------------
Option Explicit

Private Sub Form_Load()
Dim frm As Form, i As Integer
Load Form2
Load Form3
    For i = 0 To Forms.Count - 1
        Debug.Print Forms(i).Name
         
    Next
   
    Set frm = Form2
    frm.Show
    Set frm = Form3
    frm.Show
End Sub

As you see, the same variable frm, can load either one of the other forms.

If you want to actually have a variable with the form name, then you will have to loop through the collection thus:

   var1 holds the name of the form

   For i = 0 to Forms.Count - 1
      If Forms(i).Name = var1 then
      Select Case var1
          Case "Form1" : set frm = Form1
          Case "Form2" : set frm = Form2
          Case "Form3" : set frm = Form3
      End Select
   Next

0
 

Expert Comment

by:CleanupPing
ID: 9446967
PandaYong:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9447227
Mod:
I think my answer solves Panda's question as clarified in his last post


Dabas
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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