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
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 […]
Error 91: Object Variable or With Block Variable Not Set — Causes and Solutions
If you work with VBA (Visual Basic for Applications) in Excel, Access, Word, or other Microsoft Office programs, you’ve probably come across the dreaded Error 91 at least once: Run-time error ‘91’: Object variable or With block variable not set This error can be frustrating, especially if your code seems perfectly fine at first glance. […]
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 […]
Support these sponsors: