Allforms collection (1 Viewer)

whenthegeeseinvade

Registered User.
Local time
Today, 17:46
Joined
Nov 12, 2008
Messages
39
Hi all

I'm working on a listbox to list all open forms so that I can use it as a quick shortcut to access any open form.

I'm trying to use the AllForms collection and it is working to a point, but I can only access the form Name rather than it's Caption as this is not a property of the AllForms collection.

I want to present my forms as a list of their captions rather than;

frmProjects
frmDashboard
etc.

This is what I currently have;

Private Sub cmdRefreshOpenFormsList_Click()
Dim i As Integer
Me.lstOpenForms.RowSource = ""
Me.lstOpenForms.Requery
For i = 0 To CurrentProject.AllForms.Count - 1
If CurrentProject.AllForms(i).IsLoaded = True Then
Me.lstOpenForms.AddItem CurrentProject.AllForms(i).Name
Else
End If
Next i
End Sub

Ideally, I would like to use a 2 column listbox and write the form name to column 1 and the Caption to column 2. I could then set column 1 to zero width and when I click on the form's caption in column 2, it can set the focus to this form.

I'm rather stuck with how to do this and so any help would be gratefully received. There's a mince pie in it for anyone who can help!

Thank you very much,

Larry
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:46
Joined
Jan 20, 2009
Messages
12,852
For some reason some properties are not available in the dotted format and need to be reference by name from the Properties collection.

Try this (untested):
CurrentProject.AllForms(i).Properties("Caption")
 

whenthegeeseinvade

Registered User.
Local time
Today, 17:46
Joined
Nov 12, 2008
Messages
39
Thanks Galaxiom

I just tried it with a;

Debug.Print CurrentProject.AllForms(i).Properties("Caption")

but I'm getting the message;

Rum-time error '2455':
You entered an expression that has an invalid reference to the property 'Caption'.

Does this mean that the property is unavailable to this collection or just that I've typed something incorrectly?

Thank you.
 

evanscamman

Registered User.
Local time
Today, 09:46
Joined
Feb 25, 2007
Messages
274
AllForms is a collection that contains all forms - loaded or unloaded. Caption is only available when a form is loaded.

Try the Forms collection instead - it contains only loaded forms, and the Caption property is available to it. An added benefit is that you no longer need to test your forms to see if they are loaded.

? Forms.Count
? Forms(0).Caption
 

stopher

AWF VIP
Local time
Today, 17:46
Joined
Feb 1, 2006
Messages
2,395
I might be wrong but Allforms is just a collection of references to forms (.names). It is not a collection of forms. So .caption will never be available.

As evanscamman said, try the Forms object.


Code:
dim frmMyForm as Form

For Each frmMyForm in Forms
   debug.print frmMyForm.Name
Next frmMyForm

hth
Chris
 

vbaInet

AWF VIP
Local time
Today, 17:46
Joined
Jan 22, 2010
Messages
26,374
Just fyi:

* The caption property is available at design time and run time via the Forms collection. All form properties are available at design time via the Forms collection.
* IsLoaded is not testing whether the form is open or running. It tests whether the form is in design view mode or open, meaning it will return True if the form was open in design view. But if you don't mind the differences then IsLoaded will do just fine.
 

Users who are viewing this thread

Top Bottom