Results 1 to 10 of 10

Thread: Need help to speed up my search <Resolved>

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Resolved Need help to speed up my search <Resolved>

    Hi

    can anyone guru out there help me speed up this search from a textbox to search at MSHFlexGrid. Cause this search is very slow when it comes to 10,000 records. And a search is done from every keypress.
    VB Code:
    1. Private Sub Textbox1_KeyUp(KeyCode As Integer, Shift As Integer)
    2. Call search(1, Textbox1, MSHFlexGrid1)
    3. End Sub
    4.  
    5. Public Sub search(ByVal pCol As Integer, ByRef pObject As TextBox, ByRef Grid As Object)
    6. Dim temp As Integer
    7. If Grid.Rows > 1 Then
    8.  
    9.     Grid.ColHeader(0) = flexColHeaderOff
    10.     Grid.Col = pCol
    11.     Grid.Sort = 5
    12.     Grid.ColHeader(0) = flexColHeaderOn
    13.  
    14.         For temp = 1 To Grid.Rows - 1
    15.             If LCase(pObject) = LCase(Left(Grid.TextMatrix(temp, Grid.Col), Len(pObject.Text))) Then
    16.                 Grid.highlight = flexHighlightAlways
    17.                 Grid.Row = temp
    18.                 Grid.SelectionMode = flexSelectionByRow
    19.                 Grid.Col = 0
    20.                 Grid.ColSel = Grid.Cols - 1
    21.                 Grid.TopRow = Grid.Row
    22.                 Exit For
    23.             Else
    24.                 Grid.highlight = flexHighlightWithFocus
    25.             End If
    26.  
    27.         Next
    28.  
    29. End If
    30. End Sub
    Last edited by Hack; Mar 24th, 2006 at 07:13 AM. Reason: Added green "resolved" checkmark

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Need help to speed up my search

    Will you try the ff...

    VB Code:
    1. Public Sub GridSearch(ByVal pCol As Integer, ByVal pString As String, ByRef Grid As Object)
    2.     Dim lngTemp         As Long
    3.     Dim lngLimit        As Long
    4.     Dim intLength       As Integer
    5.    
    6.     If Grid.rows > 1 Then
    7.  
    8.         Grid.ColHeader(0) = flexColHeaderOff
    9.         Grid.Col = pCol
    10.         Grid.Sort = 5
    11.         Grid.ColHeader(0) = flexColHeaderOn
    12.        
    13.         lngLimit = Grid.rows - 1
    14.         pString = LCase$(pString)
    15.         intLength = Len(pString)
    16.        
    17.         For lngTemp = 1 To lngLimit
    18.             If pString = LCase$(Left$(Grid.TextMatrix(lngTemp, pCol), intLength)) Then
    19.                 Grid.Highlight = flexHighlightAlways
    20.                 Grid.Row = lngTemp
    21.                 Grid.SelectionMode = flexSelectionByRow
    22.                 Grid.Col = 0
    23.                 Grid.ColSel = Grid.cols - 1
    24.                 Grid.TopRow = Grid.Row
    25.                 Exit For
    26.             Else
    27.                 Grid.Highlight = flexHighlightWithFocus
    28.             End If
    29.         Next
    30.     End If
    31. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Re: Need help to speed up my search

    hi dee-u

    thanks for the code. Sorry but the searching is still quite slow when the number of character type increases. Maybe you could give me some idea. This is what i really need. I want to do a search function which will scan through a column of the MSHFlexGrid which consist of over 10,000+ record and the total lenght of a column record is around 100 characters for every character is it type on the textbox.

    For example when i type "A" it will search through the record and search for any record that starts with A. Then when i type "Ap", it will again search through the MSHFlexgrid.

    The solution i posted is the only way i know. But it is slow when i type very fast and also when the number of characters increase.

    Hope you could give me some sample code ideas..

    Thanks

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: Need help to speed up my search

    If you created an array that stored the positions of every record starting with A, B etc would that speed up your search?

    Or you could store the locations of all the cells that started with A, then when the next letter's typed in, you only search those cells. You'd need to check that the new search string is just the old one, with another character appended, or else it'll stuff up if they insert characters before the current one. i.e. Ap -> Asp or some such

    if left(newstring, len(oldstring)) = oldstring
    ' only search the stored cells
    end if

    and you could possibly store the cells in a multidimensional array
    search(0,1) = 1
    search(0,2) = 5
    -> first cell is at 1,5!

    Just something for you to consider

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Re: Need help to speed up my search

    hi da_silvy,

    Thank you for your input, if u ask me to store the information in multi dimension array, first thing how will i know from where to where the item is starting with "A" and even if i do wouldn't it be slower than because i have to scan through the array and then look at the grid??

    Isn't it the same as u scan through the grid then in the array??. I'm not sure..

    Anyway i'm not really good in doing multi dimensional array. if u know how to make it faster then by all means please show me how..

    Thanks.

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: Need help to speed up my search

    I meant something like this:

    VB Code:
    1. Dim iSearch(10, 2) As Integer
    2. Dim strSearch(10) As String
    3. Dim lastSearch As String
    4. Public Sub Search()
    5.     pString = "chie"
    6.     Dim iTmp(10, 2) As Integer
    7.     Dim strTmp(10) As String
    8.     Dim iLimit As Integer
    9.     Dim iCol As Integer
    10.     Dim iLength As Integer
    11.     Dim j As Integer
    12.    
    13.     iLimit = 10
    14.     j = 0
    15.     iCol = 0
    16.      
    17.     iLength = Len(pString)
    18.     If lastSearch = "" Then
    19.         lastSearch = pString
    20.     Else
    21.         If Left(pString, Len(lastSearch)) = lastSearch Then
    22.             ' We can use our old results
    23.             For i = 0 To UBound(strSearch)
    24.                  If LCase(Left(strSearch(i), iLength)) = pString Then
    25.                     Debug.Print strSearch(i)
    26.                     iTmp(j, 0) = iCol
    27.                     iTmp(j, 1) = i
    28.                     strTmp(j) = strSearch(i)
    29.                 End If
    30.             Next
    31.             ' You'll need to copy these over
    32.             'strSearch = strTmp
    33.             'iSearch = iTmp
    34.             lastSearch = pString
    35.             Exit Sub
    36.         End If
    37.     End If
    38.    
    39.     lastSearch = pString
    40.     For i = 1 To iLimit
    41.         If LCase(Left(Sheet1.Range("A" & i).Value, iLength)) = pString Then
    42.             Debug.Print Sheet1.Range("A" & i).Value
    43.             iSearch(j, 0) = iCol
    44.             iSearch(j, 1) = i
    45.             strSearch(j) = Sheet1.Range("A" & i).Value
    46.             j = j + 1
    47.         End If
    48.     Next
    49.    
    50. End Sub

    I'm not sure how to just copy over the arrays, maybe CopyMemory ??

    I don't have VB, so that was done in VBA, hopefully you get the gyst and can alter it to suit!

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Need help to speed up my search

    How about the ff...
    VB Code:
    1. Option Compare Text
    2.  
    3. Private Sub Text1_Change()
    4.     GridSearch 0, Text1.Text, mshfStatus
    5. End Sub
    6.  
    7. Public Sub GridSearch(ByVal pCol As Integer, ByVal pString As String, ByRef Grid As Object)
    8.     Dim lngTemp     As Long
    9.     Dim lngLimit    As Long
    10.    
    11.     If Grid.rows > 1 Then
    12.  
    13.         Grid.ColHeader(0) = flexColHeaderOff
    14.         Grid.Col = pCol
    15.         Grid.Sort = 5
    16.         Grid.ColHeader(0) = flexColHeaderOn
    17.         Grid.SelectionMode = flexSelectionByRow
    18.         Grid.Highlight = flexHighlightAlways
    19.        
    20.         lngLimit = Grid.rows - 1
    21.         pString = LCase$(pString) & "*"
    22.        
    23.         For lngTemp = 1 To lngLimit
    24.             'If pString = LCase$(Left$(Grid.TextMatrix(lngTemp, pCol), intLength)) Then
    25.             'If LCase$(Grid.TextMatrix(lngTemp, pCol)) Like pString Then
    26.             If Grid.TextMatrix(lngTemp, pCol) Like pString Then
    27.                 Grid.Row = lngTemp
    28.                 Grid.TopRow = lngTemp
    29.                 Exit For
    30.             End If
    31.         Next
    32.     End If
    33. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    Fanatic Member eimroda's Avatar
    Join Date
    Jul 2000
    Location
    Philippines
    Posts
    642

    Re: Need help to speed up my search

    or use ListView instead, i guess? ...
    On Error GoTo Hell

    Hell:
    Kill Me


    Food For Thought:

    - Do not judge a book... if you're not a judge!


  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    85

    Re: Need help to speed up my search

    Hi dee-u,

    Thank you so much for your input. It works..

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Need help to speed up my search <Resolved>

    Is the speed difference significant?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width