Loop Through All Form Controls And Show Properties
Some applications require that you cycle through forms controls for various reasons, including showing all the values for a particular type of control. An example would be multiple occurrances of textbox controls so you can accumulate the values in all the boxes.
The code below is a general example of how to loop through all forms controls, identify the type of control, and display various properties.
Program Code
Option Compare Database
Option Explicit
Private Sub cmdListFormObjects_Click()
' **************************************************
' Loop Through All Form Controls And Display
' Properties
' **************************************************
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case "Textbox"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "ToggleButton"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "OptionButton"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "Checkbox"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "Combobox"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "Listbox"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
Debug.Print ctrl.Value
Case "CommandButton"
Debug.Print TypeName(ctrl)
Debug.Print ctrl.Name
End Select
Next ctrl
End Sub
Private Sub Form_Open(Cancel As Integer)
With lstMyListBox
.AddItem "Alberta"
.AddItem "Bakersfield"
.AddItem "Chicago"
.AddItem "Detroit"
.AddItem "Eugene"
.AddItem "France"
.AddItem "Georgia"
.AddItem "Hawaii"
.AddItem "Idaho"
End With
With cmbMyComboBox
.AddItem "ComboValue1"
.AddItem "ComboValue2"
.AddItem "ComboValue3"
.AddItem "ComboValue4"
.AddItem "ComboValue5"
.AddItem "ComboValue6"
.AddItem "ComboValue7"
.AddItem "ComboValue8"
End With
End Sub
