Disabling all controls on a form when opening it

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Everybody,
I was wondering how can i disable all controls on a form when the form is
opened, so that they are enabled only when the user clicks on the
AddNewRecord command button.
I tried to to use the "For--Each" statement to iterate through all controls
and then set the "enabled" property to "False" but apparantly it doesn't
accept the "enabled" property in the For--Each statement
Any ideas?
 
Omar said:
Hello Everybody,
I was wondering how can i disable all controls on a form when the form is
opened, so that they are enabled only when the user clicks on the
AddNewRecord command button.
I tried to to use the "For--Each" statement to iterate through all controls
and then set the "enabled" property to "False" but apparantly it doesn't
accept the "enabled" property in the For--Each statement
Any ideas?

Your theory is sound. The problem is that not all controls have an enabled
property so when you loop through the collection you hit one that does not and
get an error.

What I do is give all the controls I want to manipulate a common Tag property
entry. Then during your loop you can test the Tag property and only disable the
controls that are appropriate.
 
Without seeing your code and without being told what errors are generated I
am guessing.

When you iterate through all the controls do you ignore labels, lines,
boxes, etc? Not all controls have an enabled property. So if you want to
set enabled to false you either have to check the control's type or ignore
the error that will be generated when you try to set the property on a
label, etc. Also, make sure you have the focus set to an object that you
won't disable.

UNTESTED CODE follows
Public Sub EnableFormControls(frmAny As Form, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)

Dim ctlAny As Control

On Error GoTo ERROR_Handler

frmAny(strControlSkip).SetFocus

For Each ctlAny In frmAny.Controls

If ctlAny.Name <> strControlSkip Then

Select Case ctlAny.ControlType
Case acCheckBox, acComboBox, acCommandButton _
, acListBox, acOptionGroup, acSubform _
, acTextBox, acToggleButton
ctlAny.Enabled = tfEnable

End Select

End If
Next ctlAny

Exit Sub

ERROR_Handler:
If Err.Number = 2164 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description, , _
"Error in EnableFormControls"
End If

End Sub


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Thanks a lot Rick
very nice trick :)

Rick Brandt said:
Your theory is sound. The problem is that not all controls have an enabled
property so when you loop through the collection you hit one that does not and
get an error.

What I do is give all the controls I want to manipulate a common Tag property
entry. Then during your loop you can test the Tag property and only disable the
controls that are appropriate.
 
Thanks John,
This was enriching

John Spencer said:
Without seeing your code and without being told what errors are generated I
am guessing.

When you iterate through all the controls do you ignore labels, lines,
boxes, etc? Not all controls have an enabled property. So if you want to
set enabled to false you either have to check the control's type or ignore
the error that will be generated when you try to set the property on a
label, etc. Also, make sure you have the focus set to an object that you
won't disable.

UNTESTED CODE follows
Public Sub EnableFormControls(frmAny As Form, _
strControlSkip As String, _
Optional tfEnable As Boolean = True)

Dim ctlAny As Control

On Error GoTo ERROR_Handler

frmAny(strControlSkip).SetFocus

For Each ctlAny In frmAny.Controls

If ctlAny.Name <> strControlSkip Then

Select Case ctlAny.ControlType
Case acCheckBox, acComboBox, acCommandButton _
, acListBox, acOptionGroup, acSubform _
, acTextBox, acToggleButton
ctlAny.Enabled = tfEnable

End Select

End If
Next ctlAny

Exit Sub

ERROR_Handler:
If Err.Number = 2164 Then
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description, , _
"Error in EnableFormControls"
End If

End Sub


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Back
Top