Click to See Complete Forum and Search --> : new creates 2....
ant
June 17th, 2001, 11:47 AM
I have the following code in a command button:
private Sub Command1_Click()
Dim n as Form1
set n = new Form1
n.show
End sub
But when i click on the button it creates 2 new forms the first time.
how can i solve this?
--Ant
--------------------------------------------------
check out my newest freeware
E-mail me at: cgeorge@thevortex.com
for the address
Cakkie
June 17th, 2001, 12:51 PM
Hmm, I got this problem myself, and it was because somewhere I reffered to the form directly, not to the instance of the form. This makes that another instance of the form was created.
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
ant
June 17th, 2001, 02:45 PM
How am i doing that?
--Ant
--------------------------------------------------
check out my newest freeware
E-mail me at: cgeorge@thevortex.com
for the address
John G Duffy
June 17th, 2001, 02:58 PM
Your sample creates only one additional form on my computer. Running your sample should result in having two forms afterwards, The original and the newly created copy.
John G
Cimperiali
June 18th, 2001, 04:56 AM
Or you have the commandbutton in form1, and so you have two as 1 is the first where command clicked is and second is the new one, or you called click twice
(maybe once with clicking, and a second via code setting command1= true)
try adding a secod form, and modifying your click code this way:
Dim n as Form2
set n = new Form2
n.Show
how many Form2 do you have now?
Of course, using New keyword you will have a new form each time. If you want only one no matter how many time you click on command button, do the following:
Dim n as Form1
set n = Form1
n.Show
You have to provide a way to set n to nothing. Here may be a way to do it:
option Explicit
'General declaration
Dim n as Form2
private Sub Command1_Click()
'only if it is not already set. However, it will not cause an error
'if not tested
If n is nothing then
set n = Form2
End If
n.Show
End Sub
private Sub Form_Unload(Cancel as Integer)
Dim f as Form
for Each f In Forms
If f.Name <> me.Name then
Unload f
set f = nothing
End If
next
If Not n is nothing then
'set the n form_variable to nothing
'to free memory
set n = nothing
End If
End Sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
Cimperiali
June 18th, 2001, 04:59 AM
Sorry. Did not see all answers...
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.