multiple instances of the same form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Graham R Seach said:
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

Better change "FormX" to "frmX" where it appears in that code.
 
thanks for all your help. its working now : )

Graham R Seach said:
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
 
Oops. Thanks Dirk.

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