Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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

0
PandaYong
Asked:
PandaYong
  • 2
  • 2
  • 2
  • +2
1 Solution
 
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
Industry Leaders: 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!

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

Featured Post

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.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now