Solved

listbox locating records

Posted on 2013-01-05
4
334 Views
Last Modified: 2013-01-06
Hi All, I have a searchbox with comman button to search data in a listbox, the code below for the search and locating the records. The search works great but the record selected is usually not showing in the listbox. I always have to scroll one record down in the listbox.

I tried to change the height of the listbox but it is still not working. the search finds the right record but if the listbox showing 10 records, it is always the one above the first one from 1-10.

I am not sure if there is an easier or better way to do this.....

Dim strSearch As String
Dim RowIndex As Long
Dim numrows As Long
numrows = CInt((List96.Height) / 67)

strSearch = "*" & txtsearch.Value & "*"

With Me.List96
    If .ListCount > 0 Then
        For RowIndex = 0 To .ListCount - 1
            If .Column(0, RowIndex) Like strSearch Or .Column(1, RowIndex) Like strSearch Or .Column(2, RowIndex) Like strSearch Then
                .SetFocus '
               
                    If ((RowIndex + (numrows - 1)) <= List96.ListCount) Then
                        Me.List96.ListIndex = RowIndex + (numrows - 1)
                    Else
                        Me.List96.ListIndex = RowIndex
                    End If
                   
                        Me.List96.ListIndex = RowIndex
                       
                    If MsgBox("Do you want to continue searching?", vbQuestion + vbYesNo, "Searching...") = vbNo Then
                        Exit For
                    End If
            End If
        Next RowIndex
    End If
End With
0
Comment
Question by:the_Apple
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 38747456
I usually filter down the list box records like this (filtering out records that dont meet the criteria):


strSQL = "SELECT ... FROM YourTable WHERE YourField LIKE "
strSearch = "*" & txtsearch.Value & "*"
Me.ListBox.RowSource = strSQL & strSearch


Try out the sample database in my article here:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_10162-What-is-in-a-field-Value-vs-Text-and-the-differences-between-form-data-and-table-data.html
0
 

Author Comment

by:the_Apple
ID: 38747464
I see, the reason I am using it this way, because I may select more than one choice, i double click on the record in the list box, then it append to another table as selection
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38747813
This would still allow you to do that... youd just need to enter new search criteria in the txtSearch box.  If you clear the text box you will see all records again.
0
 
LVL 29

Assisted Solution

by:IrogSinta
IrogSinta earned 250 total points
ID: 38748088
I tried your code and it seems to work fine in Access 2007.  I just cleaned it up a bit here:
Private Sub btnChangeDefaultMarkup_Click()
    Dim strSearch As String
    Dim RowIndex As Long

    strSearch = "*" & txtSearch.Value & "*"
    
    With Me.List96
        If .ListCount > 0 Then
            For RowIndex = 0 To .ListCount - 1
                If .Column(0, RowIndex) Like strSearch Or .Column(1, RowIndex) Like strSearch Or .Column(2, RowIndex) Like strSearch Then
                    .SetFocus
                    Me.List96.ListIndex = RowIndex
                            
                    If MsgBox("Do you want to continue searching?", vbQuestion + vbYesNo, "Searching...") = vbNo Then
                        Exit For
                    End If
                End If
            Next RowIndex
        End If
    End With
End Sub

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question