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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need the RIGHT Loop. Can't figure out which one to use 1

Status
Not open for further replies.

cutestuff

Technical User
Sep 7, 2006
162
CA
hi,

I'm sure this is very simple. I just can't seem to figure it out.

I have a listbox with around 500+ items that the user chooses from. I have a button called "Find" that looks for a search string in the listbox (that way the users won't have to scroll through the items to look for something).

The "Find" button works well - the user enters a string to search, clicks ok and the row where the string exists is 'highlighted'. Here's my problem: I would like to keep asking the user if they want to continue searching for the word (just like the Find function in the menu bar).

Here is the code :
Code:
Private Sub cmdFind_Click()

    'The code below will search for a string and set the row to that string
    
    Const NumRows = 20
    Dim intDesiredRow As Integer
    
    Dim strSearch As String
    Dim i As Long
    Dim iContinueSearch As Integer
    
   
    
    strSearch = InputBox("Enter keyword to search")
    strSearch = "*" & strSearch & "*"
    
        If List1.ListCount > 0 Then
          For i = 0 To Me.List1.ListCount
            If List1.Column(1, i) Like strSearch Then
                intDesiredRow = i
                i = Me.List1.ListCount
                
                'Set Focus
                Me.List1.SetFocus
                Me.List1.ListIndex = 1
        
                'Offset
                 Me.List1.ListIndex = intDesiredRow + (NumRows - 1)
                 Me.List1.ListIndex = intDesiredRow - 1
  
            End If
          Next i
        End If
        
        iContinueSearch = MsgBox("Do you want to continue searching?", vbQuestion + vbYesNo)
                    If iContinueSearch = vbNo Then
                        Exit Sub
                    Else
                        cmdFind_Click
                    End If   
End Sub

I've tried setting flags, and now I'm calling the function again but this doesn't seem to select the row first before displaying the Inputbox again.

Can anyone help? Please?
Thank you in advance.
 
You have to ask inside the main loop search and play with an Exit For instruction when vbNo is clicked.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
hi PHV,

Thanks for the response.
I have been playing with it for a while before I posted it. :(
I can't seem to figure out where to position the continuesearch so that it keeps looking for the next matching word.

I know it's because of the Me.List1.SetFocus and subsequent lines. I need these lines though so the list would show the selected row as the first line of the list.

 
You could also add a goto-type line...

Code:
    'add here
    StartInput:

    strSearch = InputBox("Enter keyword to search")
...
Code:
    If iContinueSearch = vbNo Then GoTo StartInput

HTH

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top