[ACCEPTED]-How do I implement pagination in SQL for MS Access?-pagination

Accepted answer
Score: 11

If you wish to apply paging in MS Acces 7 use this

SELECT *
FROM (
    SELECT Top 5 sub.ClientCode
    FROM (
        SELECT TOP 15 tblClient.ClientCode
        FROM tblClient
        ORDER BY tblClient.ClientCode
    ) sub
   ORDER BY sub.ClientCode DESC
) subOrdered
ORDER BY subOrdered.ClientCode

Where 15 is the StartPos + PageSize, and 5 is the PageSize.

EDIT to comment:

The 6 error you are receiving, is because you 5 are trying to reference a column name assign 4 in the same level of the query, namely rownumber. If 3 you were to change your query to:

SELECT *
FROM (
    SELECT ClientCode,
           (SELECT COUNT(c2.ClientCode)
            FROM tblClient AS c2
            WHERE c2.ClientCode <= c1.ClientCode) AS rownumber                
    FROM tblClient AS c1
)
WHERE rownumber BETWEEN 0 AND 15

It should 2 not give you an error, but i dont think 1 that this is the paging result you want.

Score: 4

See astander's answer for the original answer, but here's 8 my final implementation that takes into 7 account some ODBC parser rules (for the 6 first 15 records after skipping 30):

SELECT *
FROM (
  SELECT Top 15 -- = PageSize
  *
  FROM
  (
   SELECT TOP 45 -- = StartPos + PageSize
   *
   FROM tblClient
   ORDER BY Client
  ) AS sub1
  ORDER BY sub1.Client DESC
 ) AS clients
ORDER BY Client

The 5 difference here is that I need the pagination 4 to work when sorted by client name, and 3 I need all columns (well, actually just 2 a subset, but I sort that out in the outer-most 1 query).

Score: 2

I use this SQL code to implement the pagination 2 with Access

Select TOP Row_Per_Page * From [
Select TOP (TotRows - ((Page_Number - 1) * Row_Per_Page)
From SampleTable Order By ColumnName DESC
] Order By ColumnName ASC

I've published an article 1 with some screenshots on my blog

Score: 2

This is the simple method of pagination 6 using OleDbDataAdapter and Datatable classes. I 5 am using a different SQL command for simplicity.

        Dim sSQL As String = "select Name, Id from Customer order by Id"
        Dim pageNumber As Integer = 1
        Dim nTop As Integer = 20
        Dim nSkip As Integer = 0
        Dim bContinue As Boolean = True
        Dim dtData as new Datatable
        Do While bContinue

            dtData = GetData(sSQL, nTop, nSkip, ConnectionString)

            nSkip = pageNumber * nTop
            pageNumber = pageNumber + 1

            bContinue = dtData.Rows.Count > 0
            If bContinue Then
                For Each dr As DataRow In dtData.Rows
                    'do your work here
                Next
            End If
        Loop

Here 4 is the GetData Function.

    Private Function GetData(ByVal sql As String, ByVal RecordsToFetch As Integer, ByVal StartFrom As Integer, ByVal BackEndTableConnection As String) As DataTable
    Dim dtResult As New DataTable
    Try
        Using conn As New OleDb.OleDbConnection(BackEndTableConnection)
            conn.Open()
            Using cmd As New OleDb.OleDbCommand
                cmd.Connection = conn
                cmd.CommandText = sql
                Using da As New OleDb.OleDbDataAdapter(cmd)
                    If RecordsToFetch > 0 Then
                        da.Fill(StartFrom, RecordsToFetch, dtResult)
                    Else
                        da.Fill(dtResult)
                    End If
                End Using
            End Using
        End Using
    Catch ex As Exception
    End Try
    Return dtResult
End Function

The above codes 3 will return 10 rows from the table Customer 2 each time the loop operate till the end 1 of file.

Score: 0

One easy way to use limit or get pagination 5 working in access is to use ADODB library 4 which support pagination for many DBs with 3 same syntax. http://phplens.com/lens/adodb/docs-adodb.htm#ex8 Its easy to modify/override 2 pager class to fetch required number of 1 rows in array format then.

Score: 0
SELECT  *
FROM BS_FOTOS AS TBL1
WHERE ((((select COUNT(ID) AS DD FROM BS_FOTOS AS TBL2 WHERE TBL2.ID<=TBL1.ID)) BETWEEN  10 AND 15 ));

Its result 10 to 15 records only.

0

More Related questions