VB6 数据库 分页显示记录集


做法:

1、取得原始的整个记录集rs1,并对其设置分页大小。

2、创建一个新记录集rs2,把rs1上相应页面上的记录复制到rs2上。

3、在表格上显示rs2,这样就是一个页面的记录集。





'20130504  白话魔法师


Dim cn      As New ADODB.Connection
Dim rs1     As New ADODB.Recordset '原始记录集
Dim rs2     As New ADODB.Recordset '每个页面的记录集
Dim intPage As Integer '当前页数

Private Sub Command1_Click()
    Dim s As String

    If intPage < rs1.PageCount Then  '指定页面,每点一下就是下一个页面
        intPage = intPage + 1
    Else
        MsgBox "已经是最后一页"
    End If
    Call rsPage(rs1, intPage)  '调用分页显示
End Sub

Private Sub Form_Load()
    intPage = 0
    hfg.FixedCols = 0
    hfg.Height = hfg.RowHeight(0) * 6 + 290 + 20
    hfg.Width = hfg.CellWidth * 6 + 30 * 6
    cn.Open "Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=book;Data Source=ZHENG"
    rs1.Open "select * from 书名", cn, adOpenKeyset, adLockOptimistic, adCmdText
    rs1.PageSize = 6
End Sub

Private Sub rsPage(ByRef rsFirst As Recordset, ByVal intPageNumber As Integer)
    Dim i As Integer, j As Integer

    For i = 0 To rsFirst.Fields.Count - 1 '创建新表rs2,加入相关字段
        rs2.Fields.Append rsFirst.Fields(i).Name, rsFirst.Fields(i).Type, rsFirst.Fields(i).DefinedSize, rsFirst.Fields(i).Attributes
    Next i

    rs2.Open
    rsFirst.AbsolutePage = intPageNumber


    For i = 1 To rsFirst.PageSize  '把相应页的数据复制到新表rs2中
        rs2.AddNew

        For j = 0 To rsFirst.Fields.Count - 1
            rs2.Fields(j) = rsFirst.Fields(j)
        Next j

        rs2.Update
        rsFirst.MoveNext

        If rsFirst.EOF Then Exit For    '判断是否到结尾,因为最后一面,可能不是整个大小
    Next i

    Set hfg.DataSource = rs2
    rs2.Close
End Sub





你可能感兴趣的:(VB6 数据库 分页显示记录集)