Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluate if a form is open without triggering its Load event 2

Status
Not open for further replies.

Sashanan

Programmer
Jan 19, 2001
235
NL
At a certain point in my VB6 program, I want to check if another form is currently open (and refresh an ADO control there if it is). However, if it's not open, I want nothing to happen.

I've tried doing this with the .Visible property as follows:

Code:
If Form1.Visible = True then
  Form1.Adodc1.Refresh
End If

It works fine in the sense that the .Refresh method only triggers if the form was visible; however, just checking the condition triggers the form's Load event and that's exactly what I want to keep from happening.

How do I check if the form is currently loaded (or visible, either will work for my situation) without triggering its Load event? Alternatively, if this is not possible, in what event would I put the code that's currently in its Load event if I want it to trigger only when the form is actually shown?


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Use the forms collection.

From MSDN:
===========
A Formscollection is a collection whose elements represent each loaded form in an application. The collection includes the application'sMDI form,MDI child forms, and non-MDI forms. The Forms collection has a single property, Count, that specifies the number of elements in the collection.



I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
try this!

Code:
dim f as form1

set f = new form1

if not f is nothing then
 f.adodc1.Refresh
else
 debug.print "f is nothing"
end if

'some code using form f

set f = nothing 'destroy reference to f

if not f is nothing then
 f.adodc1.Refresh
else
 debug.print "f is nothing"
end if


my guess is that you are creating your form1 in the IDE and refering to it in code as form1. Each and every time you refer to form1 (eg if form1.visible = true) in code, VB will check to see if form1 exists, if it doesn't exist it will be created

creating a reference (dim f as form1) alters this behaviour.

Try this as well, it may offer some more insight...

Code:
dim f as form1
dim anotherf as new form1


f.show
'comment out f.show after observing error 424

set f = new form1



f.caption = "I am a well behaved form"

f.show vbmodal

set f = nothing

f.show
'comment out f.show after observing error 424

[code]

now try replacing the [COLOR=blue]f[/color]s in the code above  with first [COLOR=blue]anotherf[/color] and finally [COLOR=blue]form1[/color] and observe the differences!

There is also a FAQ on the lifetime of the form! Well wortha read!



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 


Even though I agree with mattKnight in using a form variable, you can also use a Public variable (boolean flag) in the form's code declaration section and set it when the from laods/unloads:

Option Explicit
Private m_FormIsLoaded As Boolean

Private Sub Form_Load()
m_FormIsLoaded = True
End Sub


Private Sub Form_Unload(Cancel As Integer)
m_FormIsLoaded = False
End Sub

Property Get FormIsLoaded() As Boolean
FormIsLoaded = m_FormIsLoaded
If Not m_FormIsLoaded Then Set Form3 = Nothing
End Property

Referencing this variable will only Initialize the form, if it isn't already, but will not load it.
 
Sounds like CClint's code is going to be my easy deadline-friendly solution for now, but I'll definitely look into mattKnight's solution as well. Gotta keep learning.

Thanks, and star to both of you.


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 

Thank you.

tb's solution would also work, you only need to compare the Form's name against a literal.

The other two solutions are encapsuled a little better.

Be careful not to declare any form level object variables using the NEW keyword...
 
To be honest the whole concept of 'NEW' declarations is still an enigma to me. It's one of the things I still need to read up on and experiment with. Starting a new application at a different company next week and have ordered what looked like a pretty comprehensive book on VB programming, so it'll be a good opportunity to look into that and other things I've been avoiding so far.

I must say that the more I learn of VB, the more I'm beginning to enjoy serious programming. Never thought I'd get this far when I made my first crude text adventures on the Commodore 64. And still learning every day. :)


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
"NEW" is used to "Initialise" an object. An object sounds very flash, but it's basically just a lump of data and code for working with that data all tied together in a neat little package...

The general procedure is something like

Dim objTemp as CMyClass

'You now have a reference to an object defined by the type
'my class, but the reference doesn't point to anything yet.
'You've essentially said "This variable is going to be of
'this type...", without actually pointing it any anything.

Set objTemp = new CMyClass

'This now actually creates an object (the "New" Bit)
'and then sets the reference to point to it (the "Set" Bit).

Have a scoot through MSDN / Online tutorials for Classes and object orientation...

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top