MS Access Hiding Text Boxes Based On User Selection VBA

I received an email from a guy to the effect of wanting to
hide some textboxes based on a value entered into a single
textbox.

In my answer, I came up with a way to do this, and
decided to share it with you…

This example will show you how to hide
the textboxes on your form based on the user’s selection.

This helps limit the user’s focus to just the fields necessary
for input.

Tweak the code around, and fit it into your situation.

(You can get the database below)

[sourcecode language=”vb”]

‘========================================================
‘DATABASE DESIGNED & CODED BY LOEBLCOM SERVICES 2014
‘ERIK LOEBL(713)409-7041
‘EMAIL: erik@loeblcomservices.com
‘WEB: http://loeblcomservices.com
‘https://vbahowto.com
‘========================================================

Option Compare Database

Private Sub btnHide_Click()
Dim strHide As String

Select Case Me.cboHide
Case 1
strHide = "2,3,4,5,6,7,8,9,10"
Case 2
strHide = "3,4,5,6,7,8,9,10"
Case 3
strHide = "4,5,6,7,8,9,10"
Case 4
strHide = "5,6,7,8,9,10"
Case 5
strHide = "6,7,8,9,10"
Case 6
strHide = "7,8,9,10"
Case 7
strHide = "8,9,10"
Case 8
strHide = "9,10"
Case 9
strHide = "10"
End Select

hide (strHide)

End Sub

Sub hide(these)
Dim varThese As Variant
Dim intCount As Integer
Dim strControlName As String

varThese = Split(these, ",")

For intCount = 0 To UBound(varThese)
If Len(varThese(intCount)) = 1 Then
strControlName = "txt0" & varThese(intCount)
Else
strControlName = "txt" & varThese(intCount)
End If
Forms("frmMain").Controls(strControlName).Visible = False
Next

End Sub

Private Sub btnShow_Click()
Dim intCount As Integer

For intCount = 0 To Me.Controls.Count – 1
If Me.Controls(intCount).Visible = False Then
Me.Controls(intCount).Visible = True
End If
Next

End Sub

[/sourcecode]

Click here for the file:
ms-access-hiding-text-boxes-based-on-user-selection-vba.mdb




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide)

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide) When you work with large datasets in Excel, it’s common to end up with thousands of blank rows scattered throughout your worksheet. Maybe your data came from a mainframe export, a CSV download, or a legacy system that doesn’t format properly. Deleting all those […]

How to Copy Data from One Worksheet to Another Using VBA in Excel

Learn how to copy data between worksheets in Excel using VBA step-by-step! This quick tutorial shows how to automate data movement, save time, and boost productivity. Watch: How to Copy Data from One Worksheet to Another Using VBA Why Automate Copying Data Between Worksheets? Copying and pasting data manually can take up valuable time and […]

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How to Fix Run Time Error 1004 in Excel

If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]


Support these sponsors:
Previous Post

Access Pagination

Next Post

How To Write A Recordset To File With VBA