any vb experts out there?

Soldato
Joined
2 Oct 2004
Posts
4,362
Location
N.W London
Visual Basic 6

I have this code which finds a numeric value inputted into a text box and highlights it in a listbox, but now i want to amend it to so that i can enter either a numeric value or text into the text box but dont know how, can anyone help?

PHP:
Private Sub cmdFind_Click()
  If lstMarks.ListIndex <> -1 Then
    lstMarks.Selected(lstMarks.ListIndex) = False
  End If
  If lstNames.ListIndex <> -1 Then
    lstNames.Selected(lstNames.ListIndex) = False
  End If

    Dim lngIndex As Long
    
    For lngIndex = 0 To lstMarks.ListCount - 1
        If Val(lstMarks.List(lngIndex)) = Val(txtFind.Text) Then
            lstNames.Selected(lngIndex) = True
            lstMarks.Selected(lngIndex) = True
            LblMessage.Caption = "Record Found !"
            Exit Sub
        End If
    Next
    LblMessage.Caption = "Record Not Found !"
End Sub

please help
 
You can leave the code you have, but add another for/next loop after the existing one to check for a text match.
 
I may be missing something because I've never used VB, but can't you just stop converting the two values to numbers in the compare, i.e. change:

Code:
If Val(lstMarks.List(lngIndex)) = Val(txtFind.Text) Then
to

Code:
If lstMarks.List(lngIndex) = txtFind.Text Then
Then it should match both, because a number is stored as a string in a listbox anyway.

Or, do you mean search the Names list for the string values? like (assuming both lists have the same number of entries)...

Code:
If (Val(lstMarks.List(lngIndex)) = Val(txtFind.Text)) Or (lstNames.List(lngIndex) =  txtFind.Text) Then
 
Last edited:
Back
Top Bottom