We help IT Professionals succeed at work.

make all objects on a form disabled on a form except the one I'm clicking on

Fordraiders
Fordraiders asked
on
make all objects on a form disabled on a form except the object/checkbox  I'm clicking on..

I have checkboxes/combo/textboxes  buttons  on a form.,

FormMain

When i click on a checkbox  "chkbox1"

I need all other objects on the form disabled or something except for        "chkbox1"  ?

Thanks
fordtraiders
Comment
Watch Question

Anders Ebro (Microsoft MVP)Microsoft Developer
CERTIFIED EXPERT

Commented:
Dim ctrl as control
For each ctrl in me.Controls
  if ctrl.name<>"chhbox1" then
     ctrl.Enabled=chkBox1.value
  End if
Next

Open in new window


Depending on what you are doing you might want to use
ctrl.Enabled=Not chkBox1.value

Open in new window

President / Owner CARDA Consultants Inc.
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:

Perhaps something like


Private Sub YourControlName_Click()
Dim ctl As Access.Control

For Each ctl In Me.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCheckBox 'You may wish to add others: acCommandButton, acOptionGroup, acSubform, acTabCtl, ...
        If ctl.Name <> " YourControlName " Then
            ctl.Enabled = Not (Me.YourControlName = True)
        End If
    End Select
Next ctl
End Sub


And you'd have to do the same in the form's current event.

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
 Private Function disableAllbut() 
Dim ctl as control 
 For each ctrl in me.Controls 
   if ctl.ControlType=acTextbox or ctl.ControlType=accheckbox Then 
   If ctl.Name <> " chkbox1" Then 
      ctl.Enabled = Nz(Me.chkBox1.Value,False) 
   End If 
End If 
Next 
End Function
 
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Set all of your controls to Disabled (or whatever you want), then use the GotFocus event of each control to enable it:


Sub <YourControl>_GotFocus()

  Me.Controls("YourControl").Enabled = True

End Sub


Then use hte LostFocus event to disable it:


Sub <YourControl>_LostFocus()

  Me.Controls("YourControl").Enabled = False

End Sub


You'll have to do this on EVERY control, however, which can be a real PITA.

Author

Commented:
ok, when i click on the chkbox  i need all the controls to be disabled except for Chkbox1

When i uncheck  Chkbox1   then i need them enabled.

Sorry


fordraiders
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:

If they are all disabled but chkbox1, then what do you expect to accomplish on that form?

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:

Sidenote...all the above codes (except mine and Scott's )will fail due to the fact that checkBoxes come with a default value of Null...

Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Sidenote...all the above codes (except mine( will fail due to the fact that checkBoxes come with a default value of Null...


Mine won't ...

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:

Mine won't ...  


Sry Sott i missed that you put the value hard coded.... i almost never do that's why i missed it. ...i corrected it in the previous post


CERTIFIED EXPERT

Commented:
@Dale, if he unchecks checkbox1 he needs all controls to reactivate, but since unchecking checkbox1 would stil keep the focus on it, the other wont activate, also you since the others are disabled you cannot check them or change focus to them.
 
@John Daniel doesn't evenuse .value, so I dont know what you mean by "all the others" only one example is using value.
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:

arana,


if he checks in chkBox1, and all other controls are disabled, then what can he possibly do?  All of the other controls are now disabled.

CERTIFIED EXPERT

Commented:
thats why he said when he uncheck it he needs them enabled again.
He will not be able to do anything on the form, but the code can still be running other stuff, timers, events, mouseovers, form events, other forms, mdi forms, I can imagine some situations when one could want that behavior, like limiting controls to another form while that one is checked, example:
open form1:  X
open form2:  0
open form3:  0
open form4:  0
open form5:  0

I have done this myself on forms that need only one button enabled  until user selects a file then all the others get enabled, is not an uncommon practice.
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:

arana, 


Let the OP answer for himself, please.  Generally you might disable some controls, but I cannot think of a single use case where you want to disable every other control on a form if a checkbox is checked.  I could understand locking all of the data controls, but not disabling every control.


Dale

Author

Commented:
This is what I ended up doing....



If Me.chkAdmin = True Then



For Each ctl In Me.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCheckBox, acCommandButton, acSubform, acTabCtl 'You may wish to add others: acCommandButton, acOptionGroup, acSubform, acTabCtl, ...
        If ctl.Name <> "chkAdmin" Then
            ctl.Enabled = Not (Me.chkAdmin = True)
        End If
    End Select
Next ctl
Else

For Each ctl In Me.Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acCheckBox, acCommandButton, acSubform, acTabCtl 'You may wish to add others: acCommandButton, acOptionGroup, acSubform, acTabCtl, ...
        If ctl.Name <> "chkAdmin" Then
            ctl.Enabled = True  'Not (Me.chkAdmin = False)
        End If
    End Select
Next ctl

End If