1
votes

I need a help, i made a userform with 1 CommandButton and 4 TextBox, what im trying to do is. i want to disable the CommanButton if all 4 TextBox is still empty.

i already found a code that help for only 1 text box, i use code

Private Sub TextBox1_Change()

If TextBox1.Value = "" Then

CommandButton1.Enabled = False

Else

CommandButton1.Enabled = True

End If

End Sub

when i use the same code in textbox2 i endup stuck, the commandbutton is enable, when i use code (if TextBox1.value & textbox2.value & TextBox3.value & TextBox4.value = "" then commandbutton1.enabled = false) the command button is stil enable after filling the textbox1.

any idea ?

2
Do you want disable if all boxes are empty or if any box is empty ??Gary's Student
i want to disable if all box are empty..Eka Oktavianus

2 Answers

4
votes

You don't need any IF condition.

CommandButton1.Enabled = cbool(Len(Textbox1.Text) + Len(Textbox2.Text) _
                          + Len(Textbox3.Text) + Len(Textbox4.Text))

CBool(x) = True while x is any number other than 0 in which case the result is False. If any of the text boxes has any content their combined length will be greater than 0 and the result of the CBool, therefore, True.

0
votes

Use:

If TextBox1.Value & TextBox2.Value & TextBox3.Value & TextBox4.Value = "" Then

If the concatenation of the values is empty, then all the values must be empty.