here i am again...

hypnotik
03-11-2003, 06:33 PM
My form contains a listbox which contains 200 names extracted from a class.txt file..
anyway,
When a match is typed in the textbox and user pressess Search, a match is found in the listbox, and everything is well.

Yet, when a match is not found, nothing happens, i want it so a msgbox says" Surname not found".

this is my code,,
So far, if theres a match or no match, a msgbox comes up saying "surname not found". I only want it to happen when match isnt found..
heres my code:

Private Sub Command1_Click()
If Text2.Text = "" Then
MsgBox "Please enter your Surname", , "Error"
Text2.SetFocus
End If
X = findStudent(Text2.Text)
List1.ListIndex = X

If Text2.Text = X Then
List1.ListIndex = True
Else
MsgBox "Surname not found"
End If



End Sub

Bucky
03-11-2003, 06:36 PM
I assume that X is an Integer. Because of this, you need to refer to the
List property of the listbox to get the item text at that index. Your final
If statement should be the following:


If Not Text2.Text = List1.List(X) Then
MsgBox "Surname not found"
End If

hypnotik
03-11-2003, 06:57 PM
well,,i tried it as the final statement (i think), but now i get no msgbox either way...
is this wat you intended?


Private Sub Command1_Click()
If Text2.Text = "" Then
MsgBox "Please enter your Surname", , "Error"
Text2.SetFocus
End If

X = findStudent(Text2.Text)
List1.ListIndex = X

If Text2.Text = X Then
List1.ListIndex = True

If Not Text2.Text = List1.List(X) Then
MsgBox "Surname not found"
End If
End If

tahnx 4 your help..apreaciate it

Bucky
03-11-2003, 06:59 PM
No... not quite... ListIndex is not a Boolean value, and you don't compare
the Text (Text2.Text) with an Integer (X).


If Text2.Text = "" Then
MsgBox "Please enter your Surname", , "Error"
Text2.SetFocus
End If

X = findStudent(Text2.Text)
List1.ListIndex = X

If Not Text2.Text = List1.List(X) Then
MsgBox "Surname not found"
End If

hypnotik
03-11-2003, 07:48 PM
well,, bucky your code loox perfect to me,,but still not working,
I used your code, but same problem, the msgbox is being displayed either way

Bucky
03-11-2003, 08:04 PM
I have an idea. Make your findStudent function return -1 if the student
is not found. That way, when you set the ListIndex it will be -1, which
means that no items are seleceted, and then you can just check the
ListIndex to see if it's -1 to see if the surname was found.

hypnotik
03-11-2003, 08:09 PM
hmmm....i dont completely follow that,,,

but is this wat you meant?

Function findStudent(surname)
For X = 0 To UBound(clsList) - 1
If UCase$(clsList(X).surname) = UCase$(surname) Then
findStudent = X
If Not findStudent = X Then
findStudent = -1
Exit Function
End If
Next X
findStudent = -1

End Function

hypnotik
03-11-2003, 11:08 PM
well anyways,,i worked it out,,

Private Sub Command1_Click()

If Text2.Text = "" Then
MsgBox "Please enter your Surname", , "Error"
Text2.SetFocus
End If

x = findStudent(Text2.Text)
If x = -1 Then
MsgBox "Surname Not Found"
Else
List1.ListIndex = x
End If
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum