Link to home
Start Free TrialLog in
Avatar of Cesar Aracena
Cesar AracenaFlag 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!
Avatar of mbizup
mbizup
Flag of Kazakhstan image

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

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

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
Avatar of 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!
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.
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?
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
Avatar of mbizup
mbizup
Flag of Kazakhstan image

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
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?
Excellent! Thanks a lot. For Attachments is... acAttachment :P

Savd me lots and lots of lines in VB. TY!!!
Glad to help out :)