Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

make all objects on a form disabled on a form except the one I'm clicking 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
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

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

ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

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.

Avatar of Fordraiders

ASKER

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

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

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...

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 ...

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


Avatar of Arana (G.P.)
Arana (G.P.)

@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.

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.

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.

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

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