Avatar of Fordraiders
Fordraiders
Flag 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
Microsoft AccessVBA

Avatar of undefined
Last Comment
Fordraiders

8/22/2022 - Mon
Anders Ebro (Microsoft MVP)

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
Daniel Pineault

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
John Tsioumpris

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

Scott McDaniel (EE MVE )

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.

Your help has saved me hundreds of hours of internet surfing.
fblack61
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
Dale Fye

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

John Tsioumpris

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Scott McDaniel (EE MVE )

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 Tsioumpris

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


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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Dale Fye

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.

Arana (G.P.)

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 Fye

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Fordraiders

ASKER
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