By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,154 Members | 2,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,154 IT Pros & Developers. It's quick & easy.

collections/form/controls

P: n/a
As I look through the drop-down list that appears after I type the
word 'as' in a dim statement, I see options such as, collection,
control, controls, form. That makes me think the functionality I am
seeking is available...my problem is I don't know how to put the
pieces together to make it happen.

My questions are:
1) does Access 97 provide the functionality such that one can obtain a
collection (or some other iterable list) of all the controls that
exist on the current form...and then provide a mechanism for iterating
through that collection/list?
2) can you provide a link to a site that has code snippets that
demonstrate:
- creating collection of all controls on a current form
- iterating through the collection
?

Thank you.
Dec 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mi************@yahoo.com wrote:
As I look through the drop-down list that appears after I type the
word 'as' in a dim statement, I see options such as, collection,
control, controls, form. That makes me think the functionality I am
seeking is available...my problem is I don't know how to put the
pieces together to make it happen.

My questions are:
1) does Access 97 provide the functionality such that one can obtain a
collection (or some other iterable list) of all the controls that
exist on the current form...and then provide a mechanism for iterating
through that collection/list?
2) can you provide a link to a site that has code snippets that
demonstrate:
- creating collection of all controls on a current form
- iterating through the collection
?

Thank you.
On a form, create a command button. In the OnClick event enter
ListFormControls Me

You can put this in a code module to see how it works. This will
enumerate the controls on the form. It will display the control name
and the control type (there are more types than in my case statement).
I used a Type as a user-defined data type instead of a collection so I
could assign the values to variable names that make some sense.

Private Type FormControlObjects
strControlName As String
strControlType As String
End Type
Sub ListFormControls(frm As Form)

Static strControls() As FormControlObjects
Dim intControls As Integer
Dim ctl As Control
Dim strC As String
Dim strT As String
Dim intFor As Integer

ReDim Preserve strControls(intControls)
strControls(intControls).strControlName = "Control Name"
strControls(intControls).strControlType = "Control Type"

For Each ctl In frm.Controls
intControls = intControls + 1
strC = strC & ctl.name & " "
Select Case ctl.ControlType
Case acLabel
strT = "Label"
Case acComboBox
strT = "Combo"
Case acListBox
strT = "ListBox"
Case acCommandButton
strT = "Command"
Case acSubform
strT = "Subform"
Case acTextBox
strT = "Textbox"
Case acCheckBox
strT = "Checkbox"
Case acToggleButton
strT = "Toggle"
Case Else
strT = "Unknown"
End Select
strC = strC & strT & vbNewLine

ReDim Preserve strControls(intControls)
strControls(intControls).strControlName = ctl.name
strControls(intControls).strControlType = strT

Next

'display controls and control types
MsgBox strC

'call routine and print them to debug window.
DebugPrintList strControls()

MsgBox "Done. Check out the debug window"
End Sub

Sub DebugPrintList(strControls() As FormControlObjects)
'this will print the control names to the debug window
Dim intFor As Integer
For intFor = LBound(strControls) To UBound(strControls)
Debug.Print strControls(intFor).strControlName; Tab; _
strControls(intFor).strControlType
Next
End Sub

John's girl
http://www.youtube.com/watch?v=TgvKrzh93ug
Dec 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.