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 give me a tip, thanks!


These posts may help answer your question too...

How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]

How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How to pick a file to load In VBA

Picking a file to load in your Microsoft App is a very important skill to know. In this blog post you will see how to do it. First you need to set a reference to the MS office object library From VBE editor –> select Tools > MS office object library (click check mark) Sub […]

Previous Post

Access Pagination

Next Post

How To Write A Recordset To File With VBA