Conditional Disable ActiveX button on Userform

R

Ray

Hi -

Tried searching for this with no luck .... then again, not sure I was
using the proper search terms either ...

I've got a userform that's used to make weekly entries to a hidden
worksheet ... the userform contains 6 textboxes, a combo-box, an 'Add'
button, and a 'close' button. I'd like to add some code that disables
the 'Add' button until all 7 user-fields have some sort of data in
them -- seems like it should be a 'userform_change' type of procedure,
but I don't see that as an option in the VBE.

Can anyone give an idea how to implement my idea?

TIA,
ray
 
D

Dave Peterson

One way...

Option Explicit
Private Sub ComboBox1_Change()
Call CheckInput
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
Call CheckInput
End Sub
Private Sub TextBox2_Change()
Call CheckInput
End Sub
Private Sub TextBox3_Change()
Call CheckInput
End Sub
Private Sub TextBox4_Change()
Call CheckInput
End Sub
Private Sub TextBox5_Change()
Call CheckInput
End Sub
Private Sub TextBox6_Change()
Call CheckInput
End Sub
Private Sub UserForm_Initialize()

With Me.ComboBox1
.AddItem "a"
.AddItem "B"
.AddItem "C"
End With

With Me.CommandButton1
.Caption = "Ok"
.Default = True
.Enabled = False
End With

With Me.CommandButton2
.Caption = "Cancel"
.Cancel = True
.Enabled = True
End With
End Sub
Sub CheckInput()

Dim iCtr As Long
Dim OkToContinue As Boolean

OkToContinue = True
'I used textbox1, ..., textbox6, so I can loop through them
For iCtr = 1 To 6
If Me.Controls("Textbox" & iCtr).Value = "" Then
OkToContinue = False
Exit For 'stop looking
End If
Next iCtr

If Me.ComboBox1.Value = "" Then
OkToContinue = False
End If

Me.CommandButton1.Enabled = OkToContinue

End Sub

If you've named the textboxes differently, you could just check by name:

if TBInput1.value = "" then oktocontinue = false
if TBSSN.value = "" then oktocontinue = false
if TBAmount.value = "" then oktocontinue = false
 
R

Ray

Perfect Dave ... thanks VERY much!

As an added bonus, I think I can apply this same logic to another
(unrelated) 'problem' I have ... so I' ve learned to 'fish' a bit
better!
 

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