Conditional Button Enabling based on Checkbox

  • Thread starter Thread starter Todd uttenstine
  • Start date Start date
T

Todd uttenstine

I have CheckBox1 and TextBox1 on a UserForm. What is the
code that would enable entry into TextBox1 if CheckBox1
has a check in it?

Thank you

Todd Huttenstine
 
Todd,

Try something like the following in the user form's code module:

Private Sub CheckBox1_Click()
Me.TextBox1.Enabled = CBool(Me.CheckBox1.Value)
End Sub
 
It still allows the user to click in the textbox if they
dont check the box first. Once they check the box and
then remove the check, then the code works, but I need the
code to work even if the user doesnt check the check box.
 
I cannot get that method to work flawlessly. I think I
need an if then statement.
 
Rule no 2: don't make it complicated.

At design time, set the enabled property of the textbox to false, and the
value of the checkbox to unchecked.
Then add this code to the click event of the checkbox.

If Check1.Value = 1 Then
Text1.Enabled = True
Else
Text1.Enabled = False
End If
 
That still does not work properly. It works before I
check it, but after I check it and then take the check
out, I can still type in the box and its not supposed to
do that because there is no check in the box.
 
Got it! Changed the code to the following. All I had to
do was set the checkbox1.value to true instead of 1.

Thanx.

If Checkbox1.Value = True Then
Textbox2.Enabled = True
Else
Textbox2.Enabled = False
End If
 
Chip's code works perfectly for me. Are you sure you're following it exactly
and have provided all the information?
 
Yes It was working for me but it would not work before I
checked the check box. I am not sure why. I did get it
working with the if then code. Thank you.
 
Just out of curiosity, why the CBool?

Force of habit. In VB6, the Value property of a CheckBox returns an
Integer, not a Boolean. I like to do the conversion to boolean myself --
self-documenting code and all that.

Just in case the TripleState property of the CheckBox is True, the better
code would be as follows:

Private Sub CheckBox1_Click()
With Me.TextBox1
If IsNull(Me.CheckBox1.Value) Then
.Enabled = False ' or True, as desired
Else
.Enabled = Me.CheckBox1.Value
End If
End With
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Back
Top