show from by using object variable


   In conventional, I use

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
Ok, I pasted in the wrong window... ignore that.
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

(if for some reason you need the name of the form frm is set to, use frm.Name)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PandaYongAuthor Commented:
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

in this case your method will give me error.

please advice. thanks
End Sub
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)

PandaYongAuthor Commented:
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 ?

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
    Set frm = Form2
    Set frm = Form3
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 
Post your closing recommendations!  No comment means you don't care.
I think my answer solves Panda's question as clarified in his last post

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.