Determining the Type of a Control
'Declare a control object variable
Dim ctl As Control
'Loop through the Controls collection using the For..Each Construct
ctlAny.Enabled = True
ctlAny.SetFocus
For Each ctl In frmAny.Controls
'Evaluate the type of the control
If ctl.ControlType = acCommandButton Then
'Make sure that we don't try to disable the command button _
that invoked this routine
If ctl.Name <> ctlAny.Name Then
ctl.Enabled = Not ctl.Enabled
End If
End If
Next ctl
End Sub
The FlipEnabled procedure is called from the form frmTypeOf. Each command button on the form (Add, Edit, Delete, and so on) sends the form and the name of a control to the FlipEnabled routine. The control that it sends is the one that you want to receive the focus after the routine executes. In the example that follows, the code sends the cmdSave command button to the FlipEnabled routine. The FlipEnabled routine sets focus to the Save button:Private Sub cmdAdd_Click()
'Call the FlipEnabled routine, passing references to the current form,
'and to the cmdSave command button on the current form
Call FlipEnabled(Me, Me.cmdSave)
End Sub
The FlipEnabled routine receives the form and control as parameters. It begins by enabling the command button that was passed to it and setting focus to it. The FlipEnabled routine then uses the VBA construct For...Each to loop through all the controls on a form. The For...Each construct repeats a group of statements for each object in an array or collectionin this case, the Controls collection. The code evaluates each control on the form to determine whether it's a command button. If it is, and it isn't the command button that was passed to the routine, the routine flips the control's Enabled property. The following VBA intrinsic controls are used when evaluating the ControlType property of a control: