Using a tick box to diplay/hide text boxes in a form

bofonomo

Board Regular
Joined
Feb 4, 2010
Messages
114
I have a working data entry system, and I am wanting to add another feature which is the option of entering octave band data (i.e 11 entries) or third octave band data (i.e. 33 entries).

I could simply have the boxes visable at all times, however I think it will look better if extra text boxes don't take up space on the form when it isn't necessary.

How do I make it so that text boxes appear/dissappear if a tick box it ticked or unticked?

Also, I would like the tick box to be linked to a field, either yes/no or true/false, so that when the record is viewed the same thing happens.

Any pointer would be great.

Many thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You probably wouldn't need the checkbox bound, all you need is some code in the On Current property of the form to check to see if data exists in any of the fields, and if it does, display that set.

A simple loop could work, depending on what the names of your fields are. If they are named like field1, field2...where there is an ordered set of numbers at the end, a loop will work. If not, you will probably have to list each control individually. But I digress...
Code:
Dim showMe as Boolean

If Not IsNull(Me.txtField1.Value) Then
    showMe = True
Else
    showMe = False
End If
and
Code:
Me.txtField1.Visible = True

The first one checks to see if there is a value in the field, the second snippet sets the visible property. These only reflect ONE field - they would have to be repeated for all the fields that you wish to hide/show.

I will try to post the loop code in a bit once I play around with it to make sure it works. It would make the coding much simpler than writing an individual line for all 11 and 33 fields.
 
Upvote 0
Ok, here is the code for the loop. You would need to adapt it as you see fit. For the purposes of this example, all of the textboxes were named txt1, txt2, txt3, and txt4.
Code:
Private Sub cmd1_Click()
Dim varControl As String
Dim a

For a = 1 To 4
    varControl = "txt" & a
    Me.Controls(varControl).Visible = True
Next a
End Sub

To test if there is a value would probably look like:
Code:
Private Sub cmd1_Click()
Dim varControl As String
Dim a

showMe = False
For a = 1 To 4
    varControl = "txt" & a
    If Not IsNull(Me.Controls(varControl).Value) Then
        showMe = True
    End If
Next a

If showMe = True Then
    For a = 1 To 4
        varControl = "txt" & a
        Me.Controls(varControl).Visible = True
    Next a
Else
    For a = 1 To 4
        varControl = "txt" & a
        Me.Controls(varControl).Visible = False
    Next a
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top