> ## 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 Hide Controls in Microsoft Access
- URL: https://nolongerset.com/setctlvis/
- Published: 2021-07-11T02:44:27.000Z
- Updated: 2026-05-08T13:03:42.000Z
- Description: I *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.
- Author: Mike Wolfe
- Tags: Code Library, #FormFunctions, #Import 2026-05-20 02:59

You cannot hide an active control.

If you try to hide the active control, you will get run-time error 2165, "You can't hide a control that has the focus."

![Run-time error 2165 message box, "You can't hide a control that has the focus."](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/07/image-10.png)

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 .Visible property.

Rather than this...

```vba
Me.tbMyTextbox.Visible = IsVisible
```

...use this...

```vba
SetCtlVis Me.tbMyTextbox, IsVisible
```

## The Code: SetCtlVis

The *SetCtlVis* routine wraps the *DefocusIfActive* procedure so that we avoid run-time error 2165, "You can't hide a control that has the focus."

```vba
'----------------------------------------------------------------------------
' Procedure : SetCtlVis
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/setctlvis/
' Purpose   : Sets the visibility of a form control.
' Params    - Ctl: The control whose visibility is being set.
'           - Visibility: A boolean to indicate whether the control 
'                         should be visible or not.
'           - FallbackCtl: The control to receive focus if the original 
'                          Ctl has focus and is being hidden.
' 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 SetCtlVis(Ctl As Control, Visibility As Boolean, _
              Optional FallbackCtl As Control)
    If Visibility Then
        'It's always safe to turn visibility on
        Ctl.Visible = True
    Else
        'https://nolongerset.com/defocusifactive/
        DefocusIfActive Ctl, FallbackCtl
        Ctl.Visible = False
    End If
End Sub
```

---

### Referenced articles

[Fix for the error: “You can’t hide a control that has the focus” in Microsoft AccessThe DefocusIfActive() procedure shifts the focus away from a control so that it may be hidden or disabled.![](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/DefocusIfActive-1-.jpg)](https://nolongerset.com/defocusifactive/)

[Convenience Functions (My Secret Weapon to Create Self-Commenting Code)How much can you really gain by replacing one line of code with a different, functionally equivalent line of code? Quite a bit, as it turns out.![](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/bullets-2166491_1920.jpg)](https://nolongerset.com/convenience-functions/)

*Image by [David Mark](https://pixabay.com/users/12019-12019/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1905290) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1905290)*