PDA

View Full Version : Solved: Setting CommandButton = False under a specific condition



Bob Blooms
12-15-2009, 10:33 AM
My knowledge of Excel VBA is growing with the help of this forum. Thanks!

Now I am really getting brave, you know a little bit of knowledge is dangerous. What I am trying to accomplish is to remove the ability of the user to click on a commandbutton prior to entering data into a textbox. So while TextBox1 = "" CommandButton3 = False.

I'm thinking that when the userform is initialized CommandButton3 would be set to False

Is this correct? But I have no idea of what the code should be. :dunno

lucas
12-15-2009, 10:37 AM
One example:

for the initialize:
Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub


for the textbox:
Private Sub Textbox1_Change()
If TextBox1.Value <> "" Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub

Bob Phillips
12-15-2009, 04:11 PM
Steve, keep it mean



Private Sub Textbox1_Change()
CommandButton1.Enabled = TextBox1.Value <> ""
End Sub

lucas
12-15-2009, 04:17 PM
Well of course. Lean and mean is always better...

Thanks Bob.

mdmackillop
12-15-2009, 04:21 PM
Meaner?

CommandButton1.Enabled = Len(TextBox1)

Bob Phillips
12-15-2009, 05:54 PM
Meaner?

CommandButton1.Enabled = Len(TextBox1)



Positively austere! Although I think it is cheating a bit by using the default property (you know my views on property defaults!).

Paul_Hossler
12-15-2009, 06:01 PM
All lean, mean, and austere solutions are indeed elegant, but Lucas' approach has --- IMHO --- the advantage of being easier to decypher in a year or two when you come back to it

Paul

Bob Blooms
12-16-2009, 07:28 AM
Wow! It is great to have options. However, being a VBA sub-neophyte I went with the code by Lucas. I need the practice of writing and tracing the code. I'll spend today applying this new knowledge to all the other buttons on my userform. Thanks to you all!!!! :)