Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Box empty

Status
Not open for further replies.

tk7834

Programmer
Jul 9, 2002
15
US
I'm doing a validation statement to be sure that a couple of text boxes and list boxes and not null before continuing. This is my code:

If (txtitn.Text = vbNullString) And (lststate.ListIndex = False) And (txtowner.Text = vbNullString) And (lstteam.ListIndex = False) Then
MsgBox ("You must enter a search criteria!")
txtitn.SetFocus
Exit Sub

The problem is if the very first item in either list box is selected, it is still seing the list box as null and it returns the message. Any ideas? Thanks!
 
You can change your code into

If (txtitn.Text = "") And (lststate.Listcount = o0 And (txtowner.Text = ""and (lstteam.Listcount=0) Then
MsgBox ("You must enter a search criteria!")
txtitn.SetFocus
Exit Sub

Haijun
 
listindex is not a boolean it is a number like 0,1,2,.. or -1 if nothing selected i think.
 
not to keep nagging, but I didn't get this to work. I researched and I found that -1 works for ComboBoxes, DirListBoxes, and DriveListBoxes, but not just list boxes. I used the following code with no success:

If (txtitn.Text = vbNullString) And (lststate.ListIndex = -1) And (txtowner.Text = vbNullString) And (lstteam.ListIndex = -1) Then
MsgBox ("You must enter a search criteria!")
txtitn.SetFocus
Exit Sub

Thanks again!!
 
Well you're wrong... Listindex is the correct way to test for the current selection, and it is set to -1 if there is nothing selected.

The code you've read in MSDN that excludes list boxes actually relates to the default value for the property - not whether the property exists for listboxes. If you look in MSDN for listindex, then click "Applies To" - lo and behold- LISTBOXES!

Seeing as the sun is shinging, and I'm in good mood - not to mention stuck for something to do till the boss puts in an appearance, I'll take a stab at your code...

It looks to me like you are using the wrong logical operator - you are using AND, where as OR would seem more logical to me. Your code only flags if ALL the controls are empty, where as my code will trigger on any of them.

Try:

Code:
If (txtitn.Text = vbNullString) Or (lststate.ListIndex = -1) Or (txtowner.Text = vbNullString) Or (lstteam.ListIndex = -1) Then
    MsgBox ("You must enter a search criteria!")
    txtitn.SetFocus
    Exit Sub

endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top