Solved

listbox locating records

Posted on 2013-01-05
4
326 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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