Make text box visible only when check box is True

G

Guest

I have created a list of milestones made up of 12 text boxes. I only want
each text boxe to be invisible on the form when I tick a check box. I will
placing 12 check boxes next to each text box. One tick box for each text box.

What code do I use so that when I check the tick box it will automatically
make the corresponding text box visable and then make the field required.

Any help appreciated
 
D

Douglas J. Steele

In the AfterUpdate event of the check box, put:

Private Sub MyCheckbox_AfterUpdate()

Me.MyTextbox.Visible = Me.MyCheckbox

End Sub

(replace MyTextbox and MyCheckbox with the appropriate names)

You'll also want that code in the form's Current event, to ensure that the
textboxes aren't visible in the first place. If you're not initializing the
check boxes to False, use

Me.MyTextbox.Visible = Nz(Me.MyCheckbox, False)
 
G

Guest

Thanks for the quick response Douglas. Worked a Treat.

The text field that appears is specified as a Date. Is there a way I can
now make the date field automaticaly insert the current date.

and

If I uncheck the checkbox, can it delete the date that I inserted.
 
D

Douglas J. Steele

Private Sub MyCheckbox_AfterUpdate()

If Me.MyCheckbox = True Then
Me.MyTextbox.Visible = True
Me.MyTextbox = Date()
Else
Me.MyTextbox.Visible = False
Me.MyTextbox = vbNullString
End If

End Sub
 
G

Guest

Thank you so much. Worked perfectly

Douglas J. Steele said:
Private Sub MyCheckbox_AfterUpdate()

If Me.MyCheckbox = True Then
Me.MyTextbox.Visible = True
Me.MyTextbox = Date()
Else
Me.MyTextbox.Visible = False
Me.MyTextbox = vbNullString
End If

End Sub
 
G

Guest

I have come accross an issue with this method. If I check the boxes and
enter a date, everything works fine. However, if I then go into form design
mode and then out again. The text boxes that I entered the date are no
longer visible even though the check boxes are still ticked.

Any ideas why this would happen?
 
D

Douglas J. Steele

You shouldn't be switching into design mode while you're running the
application, so it shouldn't be a problem, should it?

If for some reason you do have a legitimate need to switch back and forth,
you could try putting login in the form's Current event:

Private Sub MyCheckbox_AfterUpdate()

Me.MyTextbox.Visible = Me.MyCheckbox

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top