show from by using object variable

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

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

TheAmigoCommented:
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
TheAmigoCommented:
Ok, I pasted in the wrong window... ignore that.
0
VBtomCommented:
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
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
abc.Show
 


in this case your method will give me error.

please advice. thanks
   
End Sub
0
VBtomCommented:
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
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 ?

0
DabasCommented:
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

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
CleanupPingCommented:
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
DabasCommented:
Mod:
I think my answer solves Panda's question as clarified in his last post


Dabas
0
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
Programming

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.