(Q) How can I cycle through the controls on a form and retrieve their names?
(A) Use the following function as an example. Pass the form's name to it and watch the
debug window!!
Note: Make sure you also copy fIsLoaded function since fEnumControls
uses it internally to make sure that the form to be enumerated is open.
Function fEnumControls(ByVal strfrmToEnum As String)
Dim astrCtlName() As String
Dim i As Integer, intCnt As Integer
Dim frm As Form
If Not fIsLoaded(strfrmToEnum) Then
MsgBox "Form " & strfrmToEnum & " is probably closed!! " & _
vbCrLf & "Please open it & try again.", vbCritical
Exit Function
End If
Set frm = Forms(strfrmToEnum)
intCnt = frm.Count
ReDim astrCtlName(0 To intCnt - 1, 0 To 1)
For i = 0 To intCnt - 1
astrCtlName(i, 0) = frm(i).Name
Select Case frm(i).ControlType
Case acLabel: astrCtlName(i, 1) = "Label"
Case acRectangle: astrCtlName(i, 1) = "Rectangle"
Case acLine: astrCtlName(i, 1) = "Line"
Case acImage: astrCtlName(i, 1) = "Image"
Case acCommandButton: astrCtlName(i, 1) = "Command Button"
Case acOptionButton: astrCtlName(i, 1) = "Option button"
Case acCheckBox: astrCtlName(i, 1) = "Check box"
Case acOptionGroup: astrCtlName(i, 1) = "Option group"
Case acBoundObjectFrame: astrCtlName(i, 1) = "Bound object frame"
Case acTextBox: astrCtlName(i, 1) = "Text Box"
Case acListBox: astrCtlName(i, 1) = "List box"
Case acComboBox: astrCtlName(i, 1) = "Combo box"
Case acSubform: astrCtlName(i, 1) = "SubForm"
Case acObjectFrame: astrCtlName(i, 1) = "Unbound object frame or chart"
Case acPageBreak: astrCtlName(i, 1) = "Page break"
Case acPage: astrCtlName(i, 1) = "Page"
Case acCustomControl: astrCtlName(i, 1) = "ActiveX (custom) control"
Case acToggleButton: astrCtlName(i, 1) = "Toggle Button"
Case acTabCtl: astrCtlName(i, 1) = "Tab Control"
End Select
Next i
Debug.Print "Control Name", "Control Type"
Debug.Print "------------", "------------"
For i = 0 To intCnt - 1
Debug.Print astrCtlName(i, 0), astrCtlName(i, 1)
Next i
Erase astrCtlName
End Function
|