VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsPagination"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'=========================================
'=       LISTVIEW PAGINATION DEMO        =
'=---------------------------------------=
'=  Developed by P. G. B. Prasanna       =
'=  A Software Developer from Sri Lanka  =
'=  E-mail: pgbsoft@gmail.com            =
'=  Created: 02/01/2019                  =
'=  Licence: Free to use or modify       =
'=---------------------------------------=
'=        Copyright © 2019 PGBSoft       =
'=========================================

Option Explicit

Private mvarCurrentPage As Long 'local copy
Private mvarPageInfo As String 'local copy
Private mvarRecordInfo As String 'local copy
Private mvarPageSize As Long 'local copy
Private mvarPageCount As Long 'local copy
Private mvarDataSet As ADODB.Recordset  'local copy
Private mvarRecordFrom As Long 'local copy
Private mvarRecordTo As Long 'local copy
Private mvarTotalRowCount As Long 'local copy

'Set the number of records in the dataset
Public Property Let TotalRowCount(ByVal vData As Long)
    mvarTotalRowCount = vData
End Property

'Get the number of records in the dataset
Public Property Get TotalRowCount() As Long
    TotalRowCount = mvarTotalRowCount
End Property

'Set the end position in a page
Public Property Let RecordTo(ByVal vData As Long)
    mvarRecordTo = vData
End Property

'Get the end position in a page
Public Property Get RecordTo() As Long
    RecordTo = IIf(mvarRecordTo < 0, TotalRowCount, mvarRecordTo - 1)
End Property

'Set the start position in a page
Public Property Let RecordFrom(ByVal vData As Long)
    mvarRecordFrom = vData
End Property

'Set the start position in a page
Public Property Get RecordFrom() As Long
    RecordFrom = mvarRecordFrom
End Property

'Set dataset
Public Property Set DataSet(ByRef vData As ADODB.Recordset)
    Set mvarDataSet = vData
End Property

Private Property Get DataSet() As ADODB.Recordset
    Set DataSet = mvarDataSet
End Property

Private Property Let PageCount(ByVal vData As Long)
    mvarPageCount = vData
End Property

'Get number of pages
Public Property Get PageCount() As Long
    PageCount = mvarPageCount
End Property

'Set number of records in a page (page size)
Public Property Let Pagesize(ByVal vData As Long)
    mvarPageSize = vData
End Property

'Get number of records in a page (page size)
Public Property Get Pagesize() As Long
    Pagesize = mvarPageSize
End Property

'Get the current record display information
Public Property Get RecordInfo() As String
    RecordInfo = "Displaying records from " & RecordFrom & " to " & RecordTo
End Property

'Get the current page information
Public Property Get PageInfo() As String
    PageInfo = "Page " & CurrentPage & " of " & PageCount
End Property

'Return the last page
Public Property Get LastPage() As Long
    CurrentPage = PageCount
    LastPage = CurrentPage
End Property

'Return the first page
Public Property Get FirstPage() As Long
    CurrentPage = 1
    FirstPage = CurrentPage
End Property

Private Property Let CurrentPage(ByVal vData As Long)
    mvarCurrentPage = vData
End Property

'Return the current page
Public Property Get CurrentPage() As Long
    CurrentPage = mvarCurrentPage
End Property

'Return the previous page
Public Property Get PreviousPage() As Long
    CurrentPage = CurrentPage - 1
    PreviousPage = CurrentPage
End Property

'Return the next page
Public Property Get NextPage() As Long
    CurrentPage = CurrentPage + 1
    NextPage = CurrentPage
End Property

'Prepare the dataset for paging
Public Sub Setup()

    On Error GoTo Err
    
    With DataSet
        .Pagesize = Pagesize
        .CacheSize = Pagesize
        '<<We may use built-in (PageCount) property of the Recordset,
        'but it is SLOW for large datasets, hence we do a custom calculation>>
        PageCount = IIf(TotalRowCount Mod Pagesize = 0, _
        Fix((TotalRowCount / Pagesize)), _
        Fix((TotalRowCount / Pagesize)) + 1)
    End With
    
    Exit Sub
    
Err:
    
End Sub

'Find the page based on the record position
Public Function FindPage(ByVal recordPosition As Long) As Long

    On Error GoTo Err
    
    Dim lPos As Long
    
    If (recordPosition <= TotalRowCount) Then
        lPos = IIf(Abs(recordPosition) Mod Pagesize = 0, _
        Fix(Abs(recordPosition) / Pagesize), _
        Fix(Abs(recordPosition) / Pagesize) + 1)
    
        CurrentPage = lPos
        FindPage = CurrentPage
    Else
        FindPage = CurrentPage
    End If
    
    Exit Function
Err:
    CurrentPage = 1
    FindPage = CurrentPage
    
End Function

'Find the item index in a page based on the record position
Public Function FindItemIndex(ByVal recordPosition As Long) As Long

    On Error GoTo Err
    
    Dim lIdx As Long
    
    If (recordPosition <= TotalRowCount) Then
        lIdx = IIf(Abs(recordPosition) Mod Pagesize = 0, _
        Pagesize, _
        Abs(recordPosition) Mod Pagesize)
    
        FindItemIndex = lIdx
    Else
        FindItemIndex = 1
    End If
    
    Exit Function
Err:
    FindItemIndex = 1
    
End Function

'Go to a specified page
Public Function GoToPage(ByVal page As Long) As Long

    On Error GoTo Err
    
    If Abs(page) > PageCount Then
        CurrentPage = PageCount
    Else
        CurrentPage = page
    End If
        GoToPage = CurrentPage
    Exit Function
Err:
    CurrentPage = 1
    GoToPage = CurrentPage
    
End Function

Private Sub Class_Terminate()
    Set DataSet = Nothing
    'Debug.Print "Terminated"
End Sub

