Solved

Datagrid paging in Visual Basic

Posted on 2004-04-11
3
938 Views
Last Modified: 2013-12-25
Hello,

I want to know if there is any way to achieve datagrid paging in Visual
Basic... i.e., Next Page and Previous Page to navigate the data contained in
a datagrid.

I have searched the web, and can find plenty of articals about datagrid
paging in VB.NET and ASP.NET, but for the life of me I cannot find anything
relating to datagrid paging in Visual Basic 6.

Any help would be appreciated.

Thanks very much.

Note: The datagrid contains the data that is contained in a ADODC called datSalesReport.
0
Comment
Question by:glennston
3 Comments
 
LVL 8

Accepted Solution

by:
MYLim earned 500 total points
ID: 10804271
Displaying

 large amounts of data, typically thousands of records, can present a number of obstacles. Firstly, navigation can be problematic in that the scrollbar becomes hyper-sensitive, jumping hundreds of records at the slightest touch. Secondly, overheads arise as a result of retrieving and populating forms with vast amounts of data.
The simple solution is to offer a filter on the data such as some chronological selection or an alphabetical filter such as surname grouping. However, such filtering opportunities may not exist, in which case, the only option is to display the data as a fixed number of records at a time, in a similar manner to which web searches return their results. To aid this approach, ADO offers three properties, PageSize, AbsolutePage and PageCount designed to control and manage the retrieval of large recordsets. The PageSize property set the number of records returned at a time and AbsolutePage determines which 'page' is returned, where the page is the group of records of size set by PageSize. Then the number of pages is simply calculated by ADO and accessed through the PageCount property.

Eg., if a recordset contains 500 records, and the PageSize is set to 50, the AbsolutePage can take the values 1 to 10, and the PageCount is 10. The example below, assumes a large recordset, rsExample, containing 1000's of records.

  Private Function DisplayPage(lngMaxRecs as Long, _
    lngPage as Long, rsExample as ADODB.Recordset)

      Dim intRow as Integer
      Dim lngHowManyRecs as Long
      Dim lngMaxPages as Long

      rsExample.PageSize = lngMaxRecs
      rsExample.AbsolutePage = lngPage

      lngHowManyRecs = 0
      lngMaxPages = rsExample.PageCount

      lblPage = "Page " & lngPage & " of " & lngMaxPages

      Do Until rsExample.EOF Or lngHowManyRecs >= lngMaxRecs

          ' intRow included to determine the row number
          ' for use in DataGrids
          intRow = rsExample.AbsolutePosition - _
            (rsExample.PageSize * (rsExample.AbsolutePage - 1))

          'populate form with data here

          rsExample.MoveNext
           
          lngHowManyRecs = lngHowManyRecs + 1

      Loop

  End Function
In the example function here, the page index and page size are passed as parameters to the function, controlling which set of data is displayed. This type of function could be called from a navigation control such as a series of cmdbuttons representing movefirst, moveprevious, movenext and movelast actions. The PageSize property, lngMaxRecs, could be set in a menu option or combobox etc. and the AbsolutePage property, lngPage would be set to 1, decremented, incremented or set to PageCount, respectively with respect to the 4 navigation controls above.
This method represents far greater efficiency over returning the entire recordset or holding a global recordset open and progressed throughout it. By contrast, the method above allows the recordset to be closed and the object killed after the function is executed. One point to note, controls must be in place to ensure a valid AbsolutePage parameter is passed within  the range of 1 to PageCount, ideally in the calling code where the recordset is created.

==============================================================
Try this after the recordeset.open command

rs.PageSize = 100 'the number of records in a "page"
rs.AbsolutePage = 1 ' show the first 100
set datagrid.DataSource = rs

The code should display only 100 records a time in your datagrid, to display the second 100 simply change rs.AbsolutePage to 2 and so on...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

937 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

10 Experts available now in Live!

Get 1:1 Help Now