Solved

listbox locating records

Posted on 2013-01-05
4
317 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
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now