Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / April 2005

Tip: Looking for answers? Try searching our database.

multiple instances of the same form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Connie - 06 Apr 2005 03:21 GMT
I want to be able to open another instance of a form by pressing a button so
that i can have multiple/different records of the same form. im using this
code (on the onclick event of the button):
-------------------------------------------------
Private Sub OpenAnotherWindow_Click()
Set frmX = New Form_SwAvailabilityMainForm
  frmX.SetFocus
End Sub
--------------------------------------
I copied this from another database i had and this code worked (obviously i
changed the names). The problem is it that everytime i press the button to
open another form it flickers (like a popup thats being blocked) where it
opens for a milisecond and then it disappears.Any thoughts?

Thanks in advance
Graham R Seach - 06 Apr 2005 14:09 GMT
Connie,

It's all a matter of scope!

I'll bet you don't have Option Explicit enabled. From any code module,
select Tools --> Options. Then select the Editor tab, and tick the Require
Variable Declaration checkbox. In ALL your code modules, including forms and
reports, make sure the following appears at the top:
   Option Compare Database
   Option Explicit

Just for fun, select Compile from the Debug menu. Fix the error that's
reported, then click Compile again. Keep doing this, fixing each problem,
until no more problems are reported.

Now to your real problem. The issue is caused by the fact that frmX only
exists as long as the procedure in which it was declared, is running. As
soon as that procedure ends, the variable goes out of scope, and the
reference it holds to the form, is destroyed. When you destroy an object
reference, the object ceases to exist - thus your form disappears.

To get around this problem, declare the variable at module-level, like so:
   Option Compare Database
   Option Explicit
   'Declare the object variable
   Private FormX As Form_SwAvailabilityMainForm

You still have to set the reference in your procedure:
   Private Sub OpenAnotherWindow_Click()
       'Instantiate the object
       Set frmX = New Form_SwAvailabilityMainForm
       frmX.Visible = True 'Make it visible
       frmX.SetFocus 'If you must
   End Sub

Then when your procedure ends, the object variable remains alive. Just make
sure to kill the reference when you close the form.
   Private Sub Form_Close()
       Set FormX = Nothing 'Destroy the object reference
   End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

>I want to be able to open another instance of a form by pressing a button
>so
[quoted text clipped - 13 lines]
>
> Thanks in advance
Dirk Goldgar - 06 Apr 2005 15:00 GMT
> Connie,
>
[quoted text clipped - 37 lines]
>         Set FormX = Nothing 'Destroy the object reference
>     End Sub

Better change "FormX" to "frmX" where it appears in that code.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Graham R Seach - 08 Apr 2005 00:30 GMT
Oops. Thanks Dirk.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

>> Connie,
>>
[quoted text clipped - 39 lines]
>
> Better change "FormX" to "frmX" where it appears in that code.
Connie - 06 Apr 2005 19:55 GMT
thanks for all your help. its working now : )

> Connie,
>
[quoted text clipped - 60 lines]
> >
> > Thanks in advance
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.