Re: Check if a form is loaded

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Bent Lund" <bstlu@xxxxxxxxx> wrote in message
news:%23IKVhaHuFHA.1244@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> yes but is there a Forms collection in VB6?
>
> Do I need to add a library of some sort?
>
> Bent

Yes... you can type Forms into a code window, select it and hit F1 to see
help on the "Form Object, Forms Collection"

Here's 3 ways to check for loaded forms. There are many more. It all depends
on what you want to do with the result.
'============
Option Explicit

Private Sub Form_Load()
'Project has forms named "Form1" and "Form2"
MsgBox IsFormLoaded("Form2") 'False
Load Form2
MsgBox IsFormLoaded("Form2") 'True
Unload Form2
MsgBox IsFormLoaded("Form2") 'False

Dim f As Form
Set f = LoadedForm("Form2")
If f Is Nothing Then
Debug.Print "Form2's not loaded"
Else
f.Show
End If
Load Form2
Set f = LoadedForm("Form2")
If f Is Nothing Then
Debug.Print "Form2's not loaded"
Else
f.Show
End If
End Sub

Private Function IsFormLoaded(FormName As String) As Boolean
'Returns True if form name is found
Dim f As Form
For Each f In Forms
If f.Name = FormName Then
IsFormLoaded = True
Exit For
End If
Next f
End Function

Private Function FormsCount(FormName As String) As Long
'Returns the number of forms named FormName - good for MDI Child forms,
etc
Dim f As Form
For Each f In Forms
If f.Name = Name Then
FormsCount = FormsCount + 1
End If
Next
End Function

Private Function LoadedForm(FormName As String) As Form
'Returns reference to form if form name is found
Dim f As Form
For Each f In Forms
If f.Name = FormName Then
Set LoadedForm = f
Exit For
End If
Next f
End Function
'============

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..


.



Relevant Pages

  • Re: Re-Linking Tables, based on INI file
    ... Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" As ... lpstrCustomFilter As String ... Dim colDistinctLinkedMDBS As New Collection ... Private Function CanOpenExclusiveAs Boolean ...
    (comp.databases.ms-access)
  • Re: outlook status bar
    ... ByVal lpClassName As String, ByVal nMaxCount As Long) As Long ... Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ... Dim hwnd As Long ... Private Function FindChildWindowText(ByVal lHwnd As Long, ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: mkdir
    ... Private Function VBCreatePath(NewPath As String) As Boolean ... Dim vArr As Variant ... Dim iFor As Integer ...
    (microsoft.public.vb.bugs)
  • Re: mkdir
    ... > Private Function VBCreatePath(NewPath As String) As Boolean ... > Dim vArr As Variant ... > Dim iFor As Integer ...
    (microsoft.public.vb.bugs)
  • Re: current date and time object
    ... > 'Office97 VBE does not recognise the AddressOf operator; ... > Private Function vbaPassAs Long ... > ByVal lpWindowName As String) As Long ... > Dim CurrentTime As String ...
    (microsoft.public.excel)