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

JC10001

Registered User.
Local time
Today, 16:47
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?
 
When you open the form, open it as read only.

the command (in VBA) is something like this:

DoCmd.OpenForm "FromName", , , acReadOnly
 
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.
 
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.
 
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.
 
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.
 
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/
 
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:
 
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 ???
 
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

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

Back
Top Bottom