Results 1 to 10 of 10

Thread: Is this the fastest / Best way to display Data?

  1. #1

    Thread Starter
    Addicted Member thekurbster's Avatar
    Join Date
    Mar 2006
    Posts
    173

    Question Is this the fastest / Best way to display Data?

    hello all,
    I wanted to know if this is the best way to display about 9,000 + songs on a listview from a database. here's my code... any advice, suggestions would be great.

    Code:
    Private Sub cmdSearch_Click()
    On Error Resume Next
    Dim rstNewSearch As ADODB.Recordset
    Set rstNewSearch = New ADODB.Recordset
    Text3.Enabled = True
    Text3.BackColor = &HFFFFFF
    Dim strmp3name As String
    
    
    
    
    
    
            ListViewMP3.ListItems.Clear
            IsSearchResult = True
    
    
    
    
    rstNewSearch.Open "SELECT DISTINCTROW * FROM Songs ORDER BY Song ASC", cnnKerb, adOpenForwardOnly, adLockReadOnly
    
    
    
    tmpChangecolors = fileGetFromIni("Settings", "ChangeColors", App.Path + "\settings.ini")
     
     If tmpChangecolors = "" Then
        tmpChangecolors = "0"
     Else
    
    End If
    
    If tmpChangecolors = 1 Then
            Do While Not rstNewSearch.EOF
    
                  strmp3name = rstNewSearch!song
                  ListViewMP3.ListItems.Add , , strmp3name
                              
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(1) = rstNewSearch!IDFolder
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(2) = rstNewSearch!LastPlayed
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(3) = rstNewSearch!LastPlayedDate
    
                ChangeColors
                    
                  rstNewSearch.MoveNext
                 
                Loop
                ListViewMP3.Refresh
        Else
        
            Do While Not rstNewSearch.EOF
    
                  strmp3name = rstNewSearch!song
                  ListViewMP3.ListItems.Add , , strmp3name
                              
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(1) = rstNewSearch!IDFolder
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(2) = rstNewSearch!LastPlayed
                  ListViewMP3.ListItems(ListViewMP3.ListItems.Count).SubItems(3) = rstNewSearch!LastPlayedDate
    
    
                  rstNewSearch.MoveNext
                Loop
                ListViewMP3.Refresh
    End If
                  
                 
        rstNewSearch.Close
        Set rstNewSearch = Nothing
    End Sub

    Thank you guys in advance.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Is this the fastest / Best way to display Data?

    Instead of doing this:
    ListViewMP3.ListItems(ListViewMP3.ListItems.Count)

    You could declare an object of the ListItem type which is returned by the Add method:
    vb Code:
    1. Dim itm As ListItem
    2. '....other code
    3.     Set itm = ListViewMP3.ListItems.Add(Text:=strmp3name)
    4.     itm.SubItems(1) = rstNewSearch!IDFolder
    5.     '... and so on

  3. #3

    Thread Starter
    Addicted Member thekurbster's Avatar
    Join Date
    Mar 2006
    Posts
    173

    Re: Is this the fastest / Best way to display Data?

    Quote Originally Posted by Joacim Andersson
    Instead of doing this:
    ListViewMP3.ListItems(ListViewMP3.ListItems.Count)

    You could declare an object of the ListItem type which is returned by the Add method:
    vb Code:
    1. Dim itm As ListItem
    2. '....other code
    3.     Set itm = ListViewMP3.ListItems.Add(Text:=strmp3name)
    4.     itm.SubItems(1) = rstNewSearch!IDFolder
    5.     '... and so on

    Is this a faster way or just a cleaner way to write the code? I tried it and it works the same? I just wanted to know cause when I press the command button it takes like 5-14 secs for the data to show depending on what system I use. And while it's doing this code the listview is blank. Is there a way to make it a least show the rows that are visible first in the listview then if i scroll pass that it will then show the next 200 and so on?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Is this the fastest / Best way to display Data?

    It would be sleightly faster since it doesn't have to lookup the item. However adding 9000 items to a listview would be a slow process. You could add a DoEvents call after each 200 item or so. That will make it take even longer to process the loop but at least the interface will be updated and it wont looked all freezed up. Simply declare a counter (as long) and add code simular to the following just before the Loop call.
    Code:
    nCounter = nCounter + 1
    If nCounter Mod 200 = 0 Then
        DoEvents
    End If

  5. #5

    Thread Starter
    Addicted Member thekurbster's Avatar
    Join Date
    Mar 2006
    Posts
    173

    Re: Is this the fastest / Best way to display Data?

    Quote Originally Posted by Joacim Andersson
    It would be sleightly faster since it doesn't have to lookup the item. However adding 9000 items to a listview would be a slow process. You could add a DoEvents call after each 200 item or so. That will make it take even longer to process the loop but at least the interface will be updated and it wont looked all freezed up. Simply declare a counter (as long) and add code simular to the following just before the Loop call.
    Code:
    nCounter = nCounter + 1
    If nCounter Mod 200 = 0 Then
        DoEvents
    End If
    Works better thanks, just one problem... it's flickering while doing it, anyway to prevent that?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Is this the fastest / Best way to display Data?

    Try using the LockWindowUpdate API right before your ListView code.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Is this the fastest / Best way to display Data?

    Maybe thinking a bit differently would help. Do you need to display 9000 db records? What about displaying 500 at a time. The ado recordset has paging properties where you can read x number, then go back and get the next x number and so on. Requires far more code than simply dumping them all in a list, but also would appear lightning fast.

  8. #8

    Thread Starter
    Addicted Member thekurbster's Avatar
    Join Date
    Mar 2006
    Posts
    173

    Re: Is this the fastest / Best way to display Data?

    Quote Originally Posted by LaVolpe
    Maybe thinking a bit differently would help. Do you need to display 9000 db records? What about displaying 500 at a time. The ado recordset has paging properties where you can read x number, then go back and get the next x number and so on. Requires far more code than simply dumping them all in a list, but also would appear lightning fast.

    How would this be done?

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Is this the fastest / Best way to display Data?

    Look at this link and see if you are interested in pursuing the idea. The MSDN link uses a MSFlexGrid for its example, but the logic would be very similar to listviews or any other "data" control. Re-thinking is the key, your listview scrollbar won't be for 9000+ records, only n number per page. Therefore, you would need to implement a Next & Previous button or a slider control/scrollbar (as done in the link) to move from page to page.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Is this the fastest / Best way to display Data?

    DoEvents - in my opinion - is not a solution for a slow loading control.

    DoEvents will offer up to the operating system the processing of events - clicks on other buttons and what not. This can cause unexpected bugs and problems elsewhere...

    Managing the refreshing and redrawing - locking the window - as Hack suggested - those are cleaner ideas.

    Who wants to scroll through 9000 items? How about two controls - one to select the letter of the alphabet so that the user "drills down" to what they are looking for.

    Then you only load that letter into your listview.

    Or maybe try a tree view or some other control that naturally adds a layer for drilling down into.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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