• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Multiple instance of form ("New" giving error)

Hi,  (ACCESS 2013)

I want to be able to open my access form multiple times.
I saw the code below on the internet.


Private Sub cmdOpenNewCust_Click()
   Set frmX = New Form_Customers
   frmX.setfocus
End Sub

However, the "NEW" on the second line seems to cause problems.
"User-defined Type Not Defined".

Why am I getting this error?
0
Patrick O'Dea
Asked:
Patrick O'Dea
  • 5
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Is 'Customers' the name of the form?

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Just caught it.  Should be:

Private Sub cmdOpenNewCust_Click()

   Dim frmX as Form_Customers

   Set frmX = New Form_Customers

   frmX.setfocus

End Sub
0
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Note that you can also do:

 Dim frmX as Form

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Also, since your just straying into this area, a couple of other comments:

1. When you start using multiple forms, you can't reference them by name anymore.  Something like this:

 Forms("Customers").Caption = "Customer " & Me.txtCustID

 because they all have the same name.   You need to either:

a. keep track of the ordnial in the forms collection
b. keep a collection of form object references yourself.

Also, because this is an instance of a class, you can't switch the instance into design view (which is a bad idea anyway) and all changes you make to an instance are temporary.  They won't be saved when the form closes.

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
oh and one other comment; you test procedure as written will never show a form.

When a new form instance is created, it's visible property is false.  You need to set it true.  More important however is that as soon as the object variable goes out of scope, your instance is destroyed.

The best way to handle that is have a collection at module level that will not go out of scope.

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

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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