PDA

View Full Version : Solved: Validating ComboBox on Userform



PKisielewski
04-08-2008, 02:46 PM
Does anyone know how to validation a combobox?
I have a userform which has multiple items on it but there is one combobox that I would like to validate that it is not blank. I have completed this in javascript but I can't figure out how to do this in Word. I would like that after the user selects the ok (commanbutton) that it will check that combobox; verify that it is not blank - if blank - it could open up a message prompt telling the user that they need to select a questionnaire. The userform will not close and the user can complete that field and select ok again. (I did look at the exit code for the combobox but if the user skips over the combobox then the exit code will not run.)

The following is the start of the code
If ComboBox16.Text = "" Then 'If questionnaire is blank
Application.ScreenUpdating = False
MsgBox "You are required to select a Questionnaire - Please try again.", , "Message"

Else
Continue....... ' Field is not blank

End If


Hope that I have explained myself so you can understand what I am trying to accomplish.
Thanks in advance for your help.

jasoncw
04-09-2008, 06:41 AM
Just use:
Exit Sub
after the message box, but before Else.

HTH

Jason

fumei
04-09-2008, 10:12 AM
I would like to know why a combobox value would be blank.

However, jason is correct, just add an Exit.

PKisielewski
04-09-2008, 12:37 PM
Don't you just love those easy questions!
Thank you so much - that works. I was close I tried End sub (of course that did not work but after working on this whole document for 8 hours I was getting tired.
Again thanks so much, I really appreciate the quick answer.

Also, in regards to fumei's question about why it is empty. I want the user to select something and by default I put in spaces so the text is blank. Fumei - since you are questioning this, what do you think would be better?

fumei
04-10-2008, 01:37 PM
Using Select Case, rather than an IF.

First off, are you setting the default display? If you do not use ComboBox1.ListIndex = 0, then the default display is indeed blank.

However, the point is
If ComboBox16.Text = "" Then


is a Boolean logic statement. It is either True, or False.

If the user typed in: "I hate these bloody things", then If ComboBox16.Text = "" would return False. No, .Text is NOT blank.

However, it is not what you want either. What to do? I am trying to get you to think of it logically. The first step in doing that is listing what you want/do not want.

1. I want the user to select one of the items
2. I do NOT want the user to leave it as "" (blank)
3. I do NOT want the user to make it blank
4. I do NOT want the user to make it anything else

Logically, if you test for #1 and get a positive result (i.e. an item IS selected), then #2, #3, and #4 can not be true.

So? Test for #1.

To repeat, If ComboBox16.Text = "" Then, does NOT test for #1 (is an item selected). It ONLY tests (True or False) if .Text is "".

Say you initialize the userform and populate the combobox like this:

Sub UserForm_Initialize()
Dim whatever()
Dim var
whatever = Array("Please select an item", _
"yadda_1", "blah_2", _
"ho-hum_3", "Need a nap")
For var = 0 To UBound(whatever)
ComboBox1.AddItem whatever(var)
Next
ComboBox1.ListIndex = 0
End Sub


The combobox will display with Please select an item (ListIndex = 0).

However, you do not want that. You want ListIndex to be from 1 to 4....and NOTHING ELSE.

ListIndex(0) = Please select an item
ListIndex(1) = yadda_1
ListIndex(2) = blah_2
ListIndex(3) = ho-hum_3
ListIndex(4) = Need a nap

So......see #1 again - I want the user to select one of the items.


Sub CommandButton1_Click()
Select Case ComboBox1.ListIndex
Case 1 To 4
' any ListIndex from 1 to 4 IS selected
MsgBox "Good dog!"
' and whatever else you are going to do
Case Else
' ListIndex 1 to 4 is NOT selected
' this includes "" (blank), OR "I hate these things"
MsgBox "BAD dog! Try again."
ComboBox1.ListIndex = 0
Exit Sub
End Select

Unload Me
End Sub


I put the ListIndex = 0 back in if ListIndex not = 1 to 4. So if they type "I hate these things", or make it blank, the combobox goes back to displaying "Please select an item"

Demo attached.