Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

listbox locating records

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

886 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