Avatar of Cesar Aracena
Cesar Aracena
Flag for Argentina asked on

How can I enable/disable multiple fields with one click in Access 2010?

Hi guys,

I have a form that displays lots of data. Those fields are disabled by default but I want the user to be able to edit them by pressing a button. Right now I'm putting a line for each field in the "Click" event of the button (i.e. Me.FirstName.Enabled = True) but it's driving me insane.

Isn't there a fastest way to tell Access to turn all those disabled fields to enabled at once? I've searched for a method like "DisabledFields" but there's none of that. I've also tried grouping but groups don't have names so... nothing.

Thanks in advance!
Microsoft AccessSQL

Avatar of undefined
Last Comment
mbizup

8/22/2022 - Mon
mbizup

If you label your fields numerically (you really should only do this if they are closely related), like txtName01, txtName02, txtName03, etc:


For I = 1 to 10
      Me.Controls("txtName" & Format(I,"00")).enabled = false
End if

Open in new window

mbizup

Or if  you want to enable/disable ALL textboxes at once:


Dim ctl as Control
For Each ctl in Me.Controls

If ctl.ControlType = acTextbox
    ctl.Enabled = True
End if

Open in new window


The following modification will toggle controls between enabled and disabled:

   
ctl.Enabled = Not ctl.Enabled

Open in new window

IrogSinta

One other way (quite similar to grouping) is to put a Tab control on your form and put the textboxes in a tab page.  
Then set the tab control BackStyle to Transparent and the Style to None so that you don't see the tab control.
The you could just use either the name of the Tab control or the name of the page to enable/disable your fields.

Me.NameOfTabControl.Enabled = False
or
Me.NameOfPage.Enabled = False
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
Cesar Aracena

ASKER
Thanks guys.

I'm using (second) mbizup's approach as I need to see the disabled fields but I have a problem. Maybe you can see whats wrong?
Private Sub btnAllowEdit_Click()

    'Actions
    Dim ctl As Control
    
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            ctl.Enabled = True
        End If
    Next ctl
    
    'Buttons
    Me.btnStopEdit.Visible = True
    Me.EmployeeNumber.SetFocus
    Me.btnAllowEdit.Visible = False
    
End Sub

Private Sub btnStopEdit_Click()

    'Actions
    Dim ctl As Control
    
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then
            ctl.Enabled = Not ctl.Enabled
        End If
    Next ctl

    'Buttons
    Me.btnStopEdit.Visible = False
    Me.EmployeeNumber.SetFocus
    Me.btnAllowEdit.Visible = True
    
End Sub

Open in new window

When I hit the btnAllowEdit button, every textbox becomes enabled and the buttons switch (the SetFocus part is becouse focused buttons can't be hidden) but when I hit the btnStopEdit button... nothing happens. The buttons don't even change... why?

Thanks again!
mbizup

In design view, click the "..." next to on click in btnStopEdit's proeperty sheet. This will ensure that the code is "connected" to its event.  Occasionally (especially if you have copy/pasted code), the events are not connected to their code.

I'm suggesting this because it doesn't sound like your code is running at all.
IrogSinta

I'm using (second) mbizup's approach as I need to see the disabled fields
I'm not following what you mean here. All the solutions posted will not hide the fields but just dIsable them. Can you explain a bit more?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Cesar Aracena

ASKER
Thanks mbizup... I was copy/pasting. It was recorded as macro rofl.

IrogSinta, I read wrong sorry. I thought you said "hide" but still, I have many elements that should be still enabled even after disabling the textboxes. I'm using tabs btw.

mbizup, what if I also want to disable something else besides a textbox? Say an attachment box. What would I need to include in the query? Not very familiar with VB I'm affraid.
ASKER CERTIFIED SOLUTION
mbizup

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mbizup

Nevermind...  Do you mean the paperclip on the ribbon?  I think it is "acAttachment":


    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox  OR ctl.ControlType = acAttachment  Then
            ctl.Enabled = Not ctl.Enabled
        End If
    Next ctl

Open in new window



Are you disabling ALL of the controls on your form, or just certain types of controls?
Cesar Aracena

ASKER
Excellent! Thanks a lot. For Attachments is... acAttachment :P

Savd me lots and lots of lines in VB. TY!!!
Your help has saved me hundreds of hours of internet surfing.
fblack61
mbizup

Glad to help out :)