Userform OK button activate

AMAS

Active Member
Joined
Apr 11, 2010
Messages
472
Hi everyone,

This is a problem that I will do my best to describe because I just don't know the right terminology for.

I have a userform with a textbox and a combobox. The user is supposed to type something in the textbox and choose one of the options from the combobox. I want to be able to deactivate the combobox until there is something in the textbox and one of the options brom the combobox are chosen.

It should be something like:

Code:
If Controls("TextBox1").Text <> "" AND Controls("Combobox").Value <> "" Then
CommandButton1_Click.Activate

I know that this code is completely off, but I hope it will help examplify what I am after.

Any ideas?

AMAS
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
it really depends your buttons are ordered but maybe you can try something like this...

as the form load you can disable it...though visible and as the user keep on writing/selecting/choosing it..you can make the macro be trigered in change event...:)

All the best!

Code:
[/FONT]
[FONT=Courier New]Option Explicit
Private Sub ComboBox1_Change()
  If Me.OptionButton1.Value = False Or Me.OptionButton2.Value = False Then
  If Me.TextBox1.Value = "" Then
  Me.CommandButton1.Enabled = False
  Else
  Me.CommandButton1.Enabled = True
  End If
 End If
End Sub
Private Sub UserForm_Initialize()
 Me.CommandButton1.Enabled = False
End Sub
 
Upvote 0
Hi Pedie,

This is really helpful and has put me on the right track.

Here is what I have so far:

Code:
Private Sub UserForm_Initialize()
    CommandButton1.Enabled = False
End Sub
Private Sub ComboBox1_Change()
      If Controls("TextBox1").Text <> "" And Controls("Combobox1").Value = False Then
        CommandButton1.Enabled = False
      Else
        CommandButton1.Enabled = True
      End If
    
End Sub
Private Sub ComboBox2_Change()
      If Controls("TextBox2").Text <> "" And Controls("Combobox2").Value = False Then
        CommandButton1.Enabled = False
      Else
        CommandButton1.Enabled = True
      End If
    
End Sub

This keeps the OK button dimmed until I put something in textbox and the combobox. The problem is that I have several textbox/ combobox groups. Is there a way to make sure all the groups have fulfilled the criteria before initializing?

AMAS
 
Upvote 0
Okies in the last control event i think we can use this kinda code...

try it check if option is choosen and if yes loops through all textbox and check is value is entered...

if condition is met then enable the button...

Code:
[/FONT]
[FONT=Courier New]If Me.OptionButton1.Value = True Or Me.OptionButton2.Value = True Then
   Dim x As Control
        For Each x In Me.Controls
         If InStr(x.Name, "TextBox") Then
                If x.Value = "" Then
                   MsgBox x.Name & " cannot be blank"
                   Exit Sub
                End If
             End If
     Next
    Else
    MsgBox "Options not selected - pls select to proceed..."[/FONT]
[FONT=Courier New]    Exit Sub[/FONT][FONT=Courier New]
 End If
 
 Me.CommandButton1.Enabled = True
 
Upvote 0
Hi Pedie,

I tried to make your solution work but with no luck :(. I also tried a few twists to it using Select Case but also no luck...

Here is the only thing I got to work, but its not pretty:

Code:
Private Sub UserForm_Initialize()
 
    CommandButton1.Enabled = False
End Sub
Private Sub TextBox1_Change()
    If Len(Controls("TextBox1").Text) > 0 Then
        CommandButton1.Enabled = False
    End If
 
End Sub
Private Sub ComboBox1_Change()
      If Len(Controls("TextBox1").Text) > 0 And Controls("Combobox1").Value = True Then
        CommandButton1.Enabled = False
      Else
        CommandButton1.Enabled = True
      End If
 
End Sub

Everytime I put something in textbox then it deactivates the OK button until I put something in the combobox. Do you see any way to streamline this process? There seems to be a problem with the name of the Private Sub that is forcing me to use so much repition, but its just a guess because I have little experience fiddling with the controls of forms.

AMAS
 
Upvote 0
AMAS

Try repeating the code you have in the Combobox change event in the Textbox change event.

I think you might need to reverse the True/False as well.
Code:
Private Sub TextBox1_Change()

    If Len(TextBox1.Text) > 0 And Len(ComboBox1.Value) > 0 Then
        CommandButton1.Enabled = True
      Else
        CommandButton1.Enabled = False
      End If
    
End Sub

Private Sub ComboBox1_Change()
      If Len(TextBox1.Text) > 0 And Len(ComboBox1.Value) > 0 Then
        CommandButton1.Enabled = True
      Else
        CommandButton1.Enabled = False
      End If
    
End Sub
That code should only enable the command button if something is entered in the textbox and the combobox.

If nothing is entered in either then it will disable the command button.

Is that what you are looking for?

If you have multiple groups of textboxes and comboboxes then I think we'll need more explanation.

Do you want the button enabled if both the textbox and combobox in any group has something in it?

Or enable only if every group has something in it's textbox and combobox?
 
Upvote 0
Code:
Private Sub TextBox1_Change()
    ComboBox1.Enabled = (TextBox1.Text <> vbNullString)
    CommandButton1.Enabled = CommandButton1.Enabled And ComboBox1.Enabled
End Sub

Private Sub ComboBox1_Change()
    CommandButton1.Enabled = (ComboBox1.Text <> vbNullString)
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.Enabled = False
    CommandButton1.Enabled = False
    
    With ComboBox1
        .AddItem "Alpha"
        .AddItem "Beta"
    End With
End Sub
 
Last edited:
Upvote 0
Thanks Mike. This has been very helpful. With your example I have been able to modify the code to meet my needs.

I do have no question. Is there a way to combine the _Change () subs in a userform. Currently I have seven textbox/ combobox combinations and therefore I have seven TextBox_Change subs and seven ComboBox_Change subs. Is there a way to combine them since the code?

AMAS
 
Upvote 0
AMAS

There is code that can handle certain events of a group of controls.

Can you explain a bit more what you are actually trying to do?

Why do you have all these textboxes/comboboxes?
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top