> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Safely Disable Controls in Microsoft Access
- URL: https://nolongerset.com/setctlenabled/
- Published: 2023-04-07T00:01:04.000Z
- Updated: 2026-05-08T12:50:39.000Z
- Description: I *never* set the Enabled property of a form control directly. Instead, I use a convenience function so that I don't have to worry about run-time error 2164.
- Author: Mike Wolfe
- Tags: Code Library, #FormFunctions, #Import 2026-05-20 02:59

You cannot disable an active control.\*

If you try to disable the active control, you will get run-time error 2164, "You can't disable a control while it has the focus."

To avoid the error, set the focus to a different control first.

For a general solution to this problem, you can use my [*DefocusIfActive* function](https://nolongerset.com/defocusifactive/).

Better yet, let's call a [convenience function](https://nolongerset.com/convenience-functions/) instead of setting the `.Enabled` property.

Rather than this...

```vba
Me.tbMyTextbox.Enabled = IsEnabled
```

...use this...

```vba
SetCtlEnabled Me.tbMyTextbox, IsEnabled
```

## The Code: SetCtlEnabled

The *SetCtlEnabled* routine wraps the *DefocusIfActive* procedure so that we avoid run-time error 2164, "You can't disable a control while it has the focus."

```vba
'----------------------------------------------------------------------------
' Procedure : SetCtlEnabled
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/setctlenabled/
' Purpose   : Safely sets the Enabled property of a form control.
' Params    - Ctl: The control whose Enabled property is being set.
'           - Enabled: A boolean to indicate whether the control 
'                      should be enabled or not.
'           - FallbackCtl: The control to receive focus if the original 
'                          Ctl has focus and is being disabled.
' Notes     : If FallbackCtl is not provided, the function will cycle through
'               the form's controls collection looking for a suitable
'               recipient of the focus.
'----------------------------------------------------------------------------
Sub SetCtlEnabled(Ctl As Control, Enabled As Boolean, _
                  Optional FallbackCtl As Control)
    If Enabled Then
        'It's always safe to enable a control
        Ctl.Enabled = True
    Else
        'https://nolongerset.com/defocusifactive/
        DefocusIfActive Ctl, FallbackCtl
        Ctl.Enabled = False
    End If
End Sub
```

---

## \*UPDATE: Behavior Change

According to [this article](https://www.isladogs.co.uk/hide-control-with-focus/index.html) from Access MVP Colin Riddington, Microsoft quietly changed this behavior (emphasis mine):

> For as long as I can remember, I’ve always moved the focus to another control in order to disable the first control. This had always seemed completely logical to me.  
>  
> However a recent comment by pdanes in this thread at Access World Forums : [Disable ALL controls on form ](https://www.access-programmers.co.uk/forums/threads/disable-all-controls-on-form.326783/) led me to re-check this behaviour.  
>  
> I can confirm that although that had to be done up to Access 2007, from Access 2010 onwards, **it is no longer necessary to move the focus before disabling a control**.

That said, I don't think I'll be changing my ways any time soon. It's not clear to me where exactly the focus goes under the new behavior, so I'll stick with the code I know.

### Additional reading

[How to Safely Hide Controls in Microsoft AccessI \*never\* set the Visible property of a form control directly. Instead, I use a convenience function so that I don’t have to worry about run-time error 2165.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/07/lion-1905290_1920.jpg)](https://nolongerset.com/setctlvis/)

*Cover image created with [Microsoft Designer](https://designer.microsoft.com/)*