Enable Command Button After Entering Text

G

Guest

I am a beginner with Excel VBA programming and would greatly appreciate any
advice on how to accomplish the following.

I created a user form with three text boxes (txtName, txtDescription,
txtProcess) and two command buttons (cmdAdd, cmdCancel). I do not want the
cmdAdd button enabled until a user enters valid text into both the txtName
AND txtDescription text boxes. I have the Enabled property for the cmdAdd
button set to False.

How and where do I code the procedure to set the cmdAdd control's Enabled
property to True as soon as the user has entered valid text in BOTH the
txtName and txtDescription controls?

Thank you in advance for any help on this question!
 
G

Guest

Ah! After a bit more digging I found the solution in the Excel Programming
forum:

Subject: Re: UserForms, mandatory completion of fields 1/13/2006 8:51 AM
PST

By: Dave Peterson In: microsoft.public.excel.programming


Another option would be to keep the ok button disabled until all your fields
are
ok:

Option Explicit
Private Sub TextBox1_Change()
Call CheckAllRules
End Sub
Private Sub TextBox2_Change()
Call CheckAllRules
End Sub
Private Sub TextBox4_Change()
Call CheckAllRules
End Sub
Private Sub UserForm_Initialize()
Me.CommandButton1.Enabled = False
End Sub
Private Sub CheckAllRules()
Dim myCtrl As Control
Dim OkToEnable As Boolean

OkToEnable = True
For Each myCtrl In Me.Controls
If TypeOf myCtrl Is MSForms.TextBox Then
If myCtrl.Object.Value = "" Then
OkToEnable = False
Exit For
End If
End If
Next myCtrl

Me.CommandButton1.Enabled = OkToEnable

End Sub


Reposting the info here for anyone else who may need it. Thank you!
 

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