Is there a way to disable all controls on a form w/out specifying them by name? (1 Viewer)

JC10001

Registered User.
Local time
Today, 09:56
Joined
Sep 24, 2003
Messages
48
I have a form with many, many controls and subforms on it that need to be disabled if users of a certain type enter the form. Basically, of the 30+ controls and subforms these users should only have access to 3-4 of them.

Now I could go though each control and disable it like this:
Control1.Enabled = False
Control2.Enabled = False
....
....
....

But that gets old and it looks sloppy IMO.

What I'd like to do is have a for loop that will disable all of the controls and then after the for loop I can re-enable the handfull of controls that I need.

Anyone know the code to do this?
 

ForcedToUseIt

Registered User.
Local time
Today, 09:56
Joined
Jul 25, 2004
Messages
34
When you open the form, open it as read only.

the command (in VBA) is something like this:

DoCmd.OpenForm "FromName", , , acReadOnly
 

JC10001

Registered User.
Local time
Today, 09:56
Joined
Sep 24, 2003
Messages
48
I appreciate your suggestion but I need to do it the way I described. There's actually more to disabling the form than just doing it for users of a certain type but I was trying to simplify the problem when I posted the question. Long story short, I need a for loop of some kind to place in the Form_Open event of the form in question. Thanks for trying to help though.
 

Keith P

Registered User.
Local time
Today, 09:56
Joined
Oct 11, 2000
Messages
122
dim ctl as control

for each ctl in me.controls
ctl.enabled = false
next

If you only want certain controls then using a tag on control may prove useful to you.
 

JC10001

Registered User.
Local time
Today, 09:56
Joined
Sep 24, 2003
Messages
48
Keith,

Thats actually the first thing I tried but it generates a run time error # 438 "Object doesn't support this property on method." because of the Ctl.Enabled = False line of code.
 

Keith P

Registered User.
Local time
Today, 09:56
Joined
Oct 11, 2000
Messages
122
The error is probably because your control is a label control. Place On Error Resume Next
before your other code. If you need error handling then test for the specific err and then resume next.
 

ghudson

Registered User.
Local time
Today, 05:56
Joined
Jun 8, 2002
Messages
6,195
JC10001 said:
Worked like a charm. You're the man. Thanks.
Your welcome. I finally had a chance to test it and it does the job!

You can thank Candice Tripp for the code! I just pointed you in the right direction. Her web page has a lot of great samples for those interested. http://www.candace-tripp.com/
 

ctieppo

programmer on the side
Local time
Today, 05:56
Joined
Jan 3, 2005
Messages
17
Candace Tripp's code works on form but not its subform

I've successfully inserted the code you linked to disable all fields (from Candace Tripp's forum). It works on my main form, but I can't seem to get it working on the subform. I'm having a problem with the name of the subform - access can't find it. I've tried several different names for the subform (the one on the database window, the one on the main form) but none of them work. The name of the subform includes a space, so I also tried to put it in brackets inside the double quotes, but no luck.

Help!
:confused:
 

stapes

New member
Local time
Today, 09:56
Joined
Dec 4, 2006
Messages
6
I've successfully inserted the code you linked to disable all fields (from Candace Tripp's forum). It works on my main form, but I can't seem to get it working on the subform. I'm having a problem with the name of the subform - access can't find it. I've tried several different names for the subform (the one on the database window, the one on the main form) but none of them work. The name of the subform includes a space, so I also tried to put it in brackets inside the double quotes, but no luck.

Help!
:confused:

Well does anyone have an answer i.e. to get it working on a subform ???
 

Shinta

Registered User.
Local time
Today, 03:56
Joined
Jan 11, 2012
Messages
48
Greetings:

I know that this is such an old post; yet, more than trying to "revive it", I just wish to give my approach to this issue stated here. I've faced this same necessity nowadays, and came with this solution:

For Enable/Disable all the Controls in a Form:

Code:
For Each ctl In Forms![MyForm].Controls
On Error Resume Next
ctl.Enabled = True
Next ctl


For Enable/Diable all the Controls in a Subform, you just have to put the explicit path to it; that is:

Code:
For Each ctl In Forms![MyForm].[MySubForm].Controls
On Error Resume Next
ctl.Enabled = True
Next ctl


I'm uploading a basic Access Program that implements this scenario. I hope this will be of any use for anyone else looking for an answer for this matter.

Regards,
 

Attachments

  • Disable Controls in Forms - SubForms with VBA.zip
    30.5 KB · Views: 154

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:56
Joined
Jul 9, 2003
Messages
16,245
Re:-

Code:
For Each ctl In Forms![MyForm].[MySubForm].Controls

I don't think that's quite right. The problem is the subform isn't actually on the main form, it's held within a Subform/Subreport Control which I refer to as a "subform window". You have to address the subform window, then you tell the code to operate on the "FORM" within the Subform/Subreport Control (subform window) like this:-

For Each ctl In Forms![MyForm].[Subform/Subreport Control].Form.Controls

Replace [Subform/Subreport Control] with the name of the Subform/Subreport Control, which unfortunately access normally names exactly the same name as the form within it which is usually the cause of confusion.

I use a slightly different syntax than you've used...

Code:
Dim Ctrl As Control
    For Each Ctrl In Me.subFrmWinForm2.Form.Controls
        On Error Resume Next
        Ctrl.Enabled = False
    Next Ctrl

You have used "On Error Resume Next" This is done to skip an error caused when you access a control which doesn't have an enabled property. If you try and set the enabled property of a control that doesn't have an enabled property then you will get an error. "On Error Resume Next" skips any errors produced. However, this isn't considered good practice.

It's better to skip a control if it doesn't have the event you are testing. Or even better, only test the collection of controls that you are interested in. For instance you would normally only want to effect combo boxes and text boxes, therefore you can write your code like this:-

Code:
Dim Ctrl As Control

    For Each Ctrl In Me.Controls
        Select Case Ctrl.ControlType
            Case acTextBox, acComboBox ', acLabel, acListBox, acOptionButton, acOptionGroup, acToggleButton, acCheckBox
            Ctrl.Enabled = False
        End Select
    Next Ctrl

The other problem with your approach is, it will disable all the command buttons. For example, if you have a command button to disabled the controls, once you trigger it, it will also disable ALL the command buttons. Even the Command Button to enable the Controls! The above code does not affect Command Buttons, hence you don't suffer from this problem
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 04:56
Joined
Apr 3, 2020
Messages
1,003
here is code to Lock/Unlock controls in the Detail section with a comment you can change (or add) Enable/Disable. Or maybe Locked is good enough. You pass the form reference, so after you do it for the mainform, you can do it for a subform. It looks for something specific in the TAG property so you can pick which controls are affected. Normally, I use ~ around things in the TAG property so I can put more than one thing there. For instance, maybe you'd use "~Data~"

Rich (BB code):
Function LockUnlockControlsTag_Detail( _
      poForm As Form _
      , pBoo As Boolean _
      , psTag As String _
      , Optional psControlNameFocus As String = "" _
      ) As Boolean

 'lock or unlock controls in the detail section
 'statement to set Enabled is commented in case you want to do that also or instead
 'use Tag to mark what to change
 's4p 170619

   On Error GoTo Proc_Err

   LockUnlockControlsTag_Detail = False

   Dim ctl As Control

   'if we are locking controls then move focus if control name was specified
   If pBoo And Len(psControlNameFocus) > 0 Then poForm(psControlNameFocus).SetFocus

   'loop through all controls in the Detail section
   'NOTE: as written, this is limited to controls in the detail section

   For Each ctl In poForm.Detail.Controls
      With ctl
         If InStr(.Tag, psTag) > 0 Then
            If Not .Locked = pBoo Then .Locked = pBoo 'value can still be selected
            'optionally, you may want to set Enabled
           ' If .Enabled = pBoo Then .Enabled = Not pBoo   'not enabled means value cannot be selected
         End If
      End With 'ctl
   Next ctl

   LockUnlockControlsTag_Detail = True

Proc_Exit:

   On Error Resume Next
   Set ctl = Nothing
   Exit Function

Proc_Err:

   MsgBox Err.Description, , _
          "ERROR " & Err.Number _
        & "   LockUnlockControlsTag_Detail"

   Resume Proc_Exit

   'if you want to single-step code to find error, CTRL-Break at MsgBox
   'then right-click on "Resume" and choose --> Set Next Statement
   'press F8 to resume with the line that threw the error so you can inspect it
   Resume

End Function
 

Users who are viewing this thread

Top Bottom