Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse ADO to page through records 10 at a time
KeywordsADO, CREATE TABLE, database, page
CategoriesDatabase
 
Connect to the database. Then use the Connection object's Execute method to execute SQL queries of the form:

    SELECT TOP 10 * FROM Employees
    WHERE LastName + ',' + FirstName > 
    'PreviousLastName,PreviousFirstName'
    ORDER BY LastName, FirstName

Update the PreviousLastName and PreviousFirstName values after each page.

 
Option Explicit

Private m_CombinedNames As String

' Display the first 10 records.
Private Sub cmdList_Click()
    ' Reset m_CombinedNames
    ' to select the first record.
    m_CombinedNames = ","

    ' Get the next 10 records.
    cmdNext.Enabled = True
    cmdNext_Click
End Sub

' Display the next 10 records.
Private Sub cmdNext_Click()
Dim db_file As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim txt As String
Dim i As Integer

    ' Get the database name.
    db_file = App.Path
    If Right$(db_file, 1) <> "\" Then db_file = db_file & _
        "\"
    db_file = db_file & "People.mdb"

    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & db_file & ";" & _
        "Persist Security Info=False"
    conn.Open

    ' Get the next 10 records.
    Set rs = conn.Execute( _
        "SELECT TOP 10 * FROM Employees " & _
        "WHERE LastName + ',' + FirstName > " & _
        "'" & m_CombinedNames & "' ORDER BY LastName, " & _
            "FirstName")

    ' Display the records.
    Do Until rs.EOF
        i = i + 1
        txt = txt & vbCrLf & _
            Format$(rs!EmployeeId, "@@@") & " " & _
            Format$(rs!LastName, "!@@@@@@@@@@@@@") & _
            Format$(rs!FirstName, "!@@@@@@@@@@@@@")
        m_CombinedNames = rs!LastName & "," & rs!FirstName
        rs.MoveNext
    Loop

    ' See if we ran out of records.
    If i < 10 Then
        txt = txt & vbCrLf & "<END>"
        cmdNext.Enabled = False
    End If

    ' Display the data.
    If Len(txt) > 0 Then txt = Mid$(txt, 3)
    txtEmployees.Text = txt
End Sub
 
If you want to allow the user to page forward and backward, you can either:
  1. Keep track of the combined name values for each page so you can use them to go back later
  2. Use a query like this one:
        SELECT TOP 20 * FROM Employees
        WHERE LastName + ',' + FirstName <= 
        'PreviousLastName,PreviousFirstName'
        ORDER BY LastName DESC, FirstName DESC

    Then display the first 10 records in reverse order.

For information on database programming in VB .NET, see my book Visual Basic .NET Database Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated