combobox value blank

R

ranswert

I have a combo box on a form. Sometimes when the form is called the combo
box is blank. I have a button 'OK' that gets the value from the combo box.
I that value is blank my code for the 'OK' button doesn't work. There are no
blank cell in the row source for the combo box. Is there a property for the
combo box that can be set so that it won't have a blank value before the drop
arrow is pressed?
Thanks
 
D

Dave Peterson

You could have a subroutine that checks all the input to see if it's valid
before you enable the combobox.

I made a small userform with a combobox, textbox and two commandbuttons. I
wanted to make sure that there was something in the textbox and something in the
combobox before enabling the commandbutton2 button.

Option Explicit
Private Sub ComboBox1_Change()
Call CheckInput
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
Call CheckInput
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.ListIndex = -1
.Style = fmStyleDropDownList
End With
Me.CommandButton2.Enabled = False
End Sub
Private Sub CheckInput()

Dim Ok As Boolean

Ok = True
If Me.ComboBox1.ListIndex < 0 Then
Ok = False
ElseIf Me.TextBox1.Value = "" Then
Ok = False
End If

Me.CommandButton2.Enabled = Ok

End Sub
 
J

JLGWhiz

Is there a difference in:
'If x <> "" then' and 'If x <> " " then'?

Yes, "" is a null string. " " is a space. The space has a character value
whereas the null string has no value.
 
R

ranswert

Is a blank value in a combo box before the list rowsource is pulled up
concidered a null string. I thought I could do a 'if combobox.value <> ""
then'. Will this work?
Thanks
 
D

Dave Peterson

Why not use .listindex?

Is a blank value in a combo box before the list rowsource is pulled up
concidered a null string. I thought I could do a 'if combobox.value <> ""
then'. Will this work?
Thanks
 
J

JLGWhiz

If Not ComboBox1.Value < 0 Then

Or

If ComboBox1.Value <> "" Then

Either should work.
 
R

ranswert

Thanks
I used your code and it works great.

Dave Peterson said:
You could have a subroutine that checks all the input to see if it's valid
before you enable the combobox.

I made a small userform with a combobox, textbox and two commandbuttons. I
wanted to make sure that there was something in the textbox and something in the
combobox before enabling the commandbutton2 button.

Option Explicit
Private Sub ComboBox1_Change()
Call CheckInput
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
Call CheckInput
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.ListIndex = -1
.Style = fmStyleDropDownList
End With
Me.CommandButton2.Enabled = False
End Sub
Private Sub CheckInput()

Dim Ok As Boolean

Ok = True
If Me.ComboBox1.ListIndex < 0 Then
Ok = False
ElseIf Me.TextBox1.Value = "" Then
Ok = False
End If

Me.CommandButton2.Enabled = Ok

End Sub
 
D

Dave Peterson

..listindex is a number that represents which item in your list was selected.
The top item has a listindex of 0. So when nothing is selected, listindex is
-1.
What is 'listindex'?
 

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

Similar Threads


Top