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