Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

listbox locating records

Posted on 2013-01-05
4
Medium Priority
?
338 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 1000 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 1000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

705 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