Solved

Is a form open or not

Posted on 2001-07-27
16
199 Views
Last Modified: 2011-08-08
How do you check to see if a form is open?  Without setting boolean flags.

like :
frm.show
boolFlagfrm = true
.
.
.
.
if(boolFlagfrm= true)then
    unload frm
end if

0
Comment
Question by:DandL
  • 3
  • 3
  • 2
  • +7
16 Comments
 
LVL 6

Accepted Solution

by:
JonFish85 earned 25 total points
ID: 6327110
if frm.visible = true then
'form is visible
end if
0
 
LVL 18

Expert Comment

by:deighton
ID: 6327124
Public Function IsFormLoaded(sForm As String) As Boolean


Dim frmX As Form
Dim sUForm As String

sUForm = UCase(sForm)

For Each frmX In Forms
    If UCase(frmX.Name) = sUForm Then IsFormLoaded = True
Next


End Function

e.g.

isformloaded("frmMain")
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6327140
Deighton, that will only determine if the form is loaded, not if it is also SHOWN.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6327152

Private Sub Command1_Click()
  Dim i As Integer
  Dim frmX As Form
  For i = 0 To Forms.Count - 1
    Set frmX = Forms.Item(i)
    If frmX.Name = "Form2" Then
       If frmX.Visible then
         MsgBox "Form2 Shown"
       End if
    End If
  Next
End Sub
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6327153
As an assist, here is modified version of deighton's routine which is optimized a little:

Public Function IsFormLoaded(sForm As String) As Boolean
   Dim frmX As Form

   For Each frmX In Forms
      If StrComp(frmX.Name,sForm,vbTextCompare) = 0 Then
         IsFormLoaded = True
         Exit For
      End If
      Set frmX = Nothing
   Next
End Function

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6327160
Dandl,
   Why are you concerned if the form is Showing?

You can easily Unload ALL Loaded forms:

dim frm as Form
   
for each frm in Forms
    frm.Unload
Next

That will cleanly Unload ALL Loaded forms.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6327175
See below function

' See if a Form is Loaded
' You can use the Forms collection to see if a form with a certain name is
' loaded. Loop through the forms comparing their names to the target name.

' Return a form by name if it is loaded.
Private Function FindForm(ByVal form_name As String) As Boolean
     Dim i As Integer

        ' Assume we will not find it.
        FindForm = False

        ' Search the loaded forms.
        For i = 0 To Forms.Count - 1
            If Forms(i).Name = form_name Then
                ' We found it. Return this form.
                FindForm = True
                Exit For
            End If
        Next i
End Function


hongjun
0
 
LVL 1

Author Comment

by:DandL
ID: 6327196
okay that is a lot of info....give me some time to wade through it all.  I think y'all have brought up an issue or two that I had not thought about. Like if the form is load or if it is visable.

Arthur_Wood you made a good point about
>Why are you concerned if the form is Showing?
I think that might be the real issue....I can just always unload it.
Humm..
0
 
LVL 18

Expert Comment

by:deighton
ID: 6327207
In my app I hide form A then show form B, but sometimes I have only shown form B - so I need to know if Form A is loaded.

DennisBorg,

I agree the 'exit for' is a good idea, but I get shot if I use it her :->
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6327471
>I agree the 'exit for' is a good idea, but I get shot if
>I use it her :->

Who would shoot you, and why? :-)


-Dennis Borg
0
 

Expert Comment

by:viklele
ID: 6329230
I was also wondering why use of "Exit For" is disallowed ... I can under barring using of Goto execpt for error handling.

0
 
LVL 17
ID: 6329407
I think that a For loop is meant to complete it's iterations. If you say For x = 1 To 10 then it is expected to loop 10 times. To exit before this number would be wrong.
I also feel that Exit Do should be wrong unless the loop condition has been met.
However you need to get out of the thing sometimes unless your code design is perfect and I have not found this 'perfect' answer yet. :7)
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6332690
Hi ,

Can we try:

If myForm is Nothing then
 ..
End if

?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6333785
RobinD
   The use of Exit For is to allow a For...Next loop to termnate at the appropriate point  ->  if you are looping through a collection, in order to find a certain entry , once you have found it, searching further is a waste of CPU cycles, so the Exit For allows you to get on with the processing, rather than spining the extra cycles:

   For each frm in Forms
      if frm.Name = "MyForm"
         frm.Hide
         Exit FOr
      End If
   Next frm

will look for the Form named "MyForm", Hide it, and then exit the loop---> if you had 10 forms, and MyForm happened to be #2, then you find it, exit the loop, and don't waste the time or cycles looking at the remaing 8, which ARE NOT named "MyForm".


It is NOT ALWAYS the case that a For...Next loop is intended to carry out ALL of the indicated iterations.

0
 
LVL 17
ID: 6333852
I fully appreciate that Arthur (I use it myself.) I just wondered if this was why there was a threat of being shot.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6336963
RobinD - my superior does not like exit for, he thinks it is a 'goto' statement - I have been conditioned not to use it, but it does increase program efficiency - and leaves you with a record of the final value of c.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now