A common requirement in many applications is the ability to disable all
the controls on a form while some process takes place, such as a saving
routine, before restoring all the controls to their original state. On the
surface this seems a simple case of setting all the control's enabled
property to false and setting them to true afterwards. However, a few
spanner's waiting to fall in the works exist...
For a start, not all the controls may initially be enabled, plus some
controls do not have an enabled property at all, then other controls may
need to be enabled such as an Exit command button to allow a user to abort
the form if necessary.
Therefore, the initial state of all the controls needs to be stored,
disabled and subsequently reset to their initial state.
A convenient way to achieve this is to step through the forms object
collection and store the control type and enabled state in a user defined
type array. This subroutine has the ability to disable and enable controls
on a form:
'declare user defined variable type for form object data
Private Type udvFormControl
' Control variable to hold form control
objFrmCtrl As Control
' Boolean variable to hold control's enabled status
blnEnabled As Boolean
End Type
'declare modular level array as User Defined Variable
Private arrFrmCtrl() As udvFormControl
Private Sub EnableControls(blnStatus As Boolean)
Dim intCollectionCount As Integer
Dim objControl As Control
Dim strName As String
If Not blnStatus Then
'store initial settings & disable form controls
For Each objControl In Me
'avoid controls that don't have an enabled property or
'the EXIT facility
If TypeOf objControl Is Line Or _
TypeOf objControl Is Label Or _
TypeOf objControl Is Frame Or _
objControl.Name = "cmdExit" Then
'ignore
Else
intCollectionCount = intCollectionCount + 1
End If
Next objControl
ReDim arrFrmCtrl(intCollectionCount) As udvFormControl
intCollectionCount = 0
For Each objControl In Me
'avoid controls that don't have an enabled property or
'the EXIT facility
If TypeOf objControl Is Line Or _
TypeOf objControl Is Label Or _
TypeOf objControl Is Frame Or _
objControl.Name = "cmdExit" Then
'ignore
Else
strName = objControl.Name
Set arrFrmCtrl(intCollectionCount).objFrmCtrl = objControl
If objControl.Enabled Then
arrFrmCtrl(intCollectionCount).blnEnabled = True
Else
arrFrmCtrl(intCollectionCount).blnEnabled = False
End If
intCollectionCount = intCollectionCount + 1
objControl.Enabled = False
End If
Next objControl
Else
'enable form controls by reinstating initial settings
For intCollectionCount = 0 To UBound(arrFrmCtrl) - 1
Set objControl = arrFrmCtrl(intCollectionCount).objFrmCtrl
objControl.Enabled = arrFrmCtrl(intCollectionCount).blnEnabled
Set objControl = Nothing
Next intCollectionCount
End If
End Sub
The sub works as follows.... pass the blnStatus argument as True to
disable all the required controls. First a user defined datatype is
declared to hold the form controls identity and enabled status. Then all
the controls on the form are counted excluding controls without enabled
properties and the Exit control. This done, an array is dimensioned to the
number of applicable controls and is populated by iterating through the forms
control collection. At the same time all these controls are set to enabled
= False.
To reverse the process, pass the blnStatus argument as False to reset all
the controls to their original state.
A couple of points to consider, define and declare the user defined
datatype in the form or modular declarations and secondly code the calling
routine to disable the controls first before re-enabling them, this will
not cause an error, just serves little point! The only other point to be
aware of is that other controls may exist that do not have an enable
status other than the ones listed above, which can be identified by
stepping through the subroutine in the event of an undefined object
property error, which simply means the control hasn't an enabled property.
|