写了一个,应为datagrid是绑定记录集的 所以采用了间接的办法,定义另一个记录集objrs,将分页后的记录集付给objrs. 效果还不错,我加了详细地注释。像pagesize,AbsolutePage的用法可参考msdn 引用ado2.6 Option Explicit Dim conn As ADODB.Connection Dim lCurrentPage As LongPrivate Sub cmdNext_Click() lCurrentPage = lCurrentPage + 1 Call Loadcontrol(lCurrentPage) End SubPrivate Sub cmdPrevious_Click() If lCurrentPage > 1 Then lCurrentPage = lCurrentPage - 1 Call Loadcontrol(lCurrentPage) End If End SubPrivate Sub Form_Load()
Set conn = New ADODB.Connection conn.CursorLocation = adUseClient conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test2000.mdb;" lCurrentPage = 1 Call Loadcontrol(lCurrentPage)End Sub Private Sub Loadcontrol(lPage As Long) Dim adoPrimaryRS As ADODB.Recordset Dim lPageCount As Long Dim nPageSize As Integer Dim lCount As Long
'每页显示的纪录 nPageSize = 10 Set adoPrimaryRS = New ADODB.Recordset adoPrimaryRS.Open "select * from numbers", conn, adOpenStatic, adLockOptimistic adoPrimaryRS.PageSize = nPageSize '页数 lPageCount = adoPrimaryRS.PageCount If lCurrentPage > lPageCount Then lCurrentPage = lPageCount End If
adoPrimaryRS.AbsolutePage = lCurrentPage '定义另一个记录集 Dim objrs As New ADODB.Recordset '添加字段名称 For lCount = 0 To adoPrimaryRS.Fields.Count - 1 objrs.Fields.Append adoPrimaryRS.Fields(lCount).Name, adVarChar, adoPrimaryRS.Fields(lCount).DefinedSize Next '打开记录集 objrs.Open '将指定记录数循环添加到objrs中 For lCount = 1 To nPageSize objrs.AddNew objrs!id = adoPrimaryRS!id objrs!anumber = adoPrimaryRS!anumber adoPrimaryRS.MoveNext Next '绑定 Set DataGrid1.DataSource = objrs
'显示页数 txtPage = lPage & "/" & adoPrimaryRS.PageCount End SubPrivate Sub Form_Unload(Cancel As Integer) If Not conn Is Nothing Then conn.Close End If Set conn = Nothing End Sub
http://www.ccw.com.cn/htm/center/prog/02_4_4_3.asp
原理是一样的
所以采用了间接的办法,定义另一个记录集objrs,将分页后的记录集付给objrs.
效果还不错,我加了详细地注释。像pagesize,AbsolutePage的用法可参考msdn
引用ado2.6
Option Explicit
Dim conn As ADODB.Connection
Dim lCurrentPage As LongPrivate Sub cmdNext_Click()
lCurrentPage = lCurrentPage + 1
Call Loadcontrol(lCurrentPage)
End SubPrivate Sub cmdPrevious_Click()
If lCurrentPage > 1 Then
lCurrentPage = lCurrentPage - 1
Call Loadcontrol(lCurrentPage)
End If
End SubPrivate Sub Form_Load()
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test2000.mdb;" lCurrentPage = 1
Call Loadcontrol(lCurrentPage)End Sub
Private Sub Loadcontrol(lPage As Long)
Dim adoPrimaryRS As ADODB.Recordset
Dim lPageCount As Long
Dim nPageSize As Integer
Dim lCount As Long
'每页显示的纪录
nPageSize = 10
Set adoPrimaryRS = New ADODB.Recordset
adoPrimaryRS.Open "select * from numbers", conn, adOpenStatic, adLockOptimistic adoPrimaryRS.PageSize = nPageSize
'页数
lPageCount = adoPrimaryRS.PageCount
If lCurrentPage > lPageCount Then
lCurrentPage = lPageCount
End If
adoPrimaryRS.AbsolutePage = lCurrentPage
'定义另一个记录集
Dim objrs As New ADODB.Recordset
'添加字段名称
For lCount = 0 To adoPrimaryRS.Fields.Count - 1
objrs.Fields.Append adoPrimaryRS.Fields(lCount).Name, adVarChar, adoPrimaryRS.Fields(lCount).DefinedSize
Next
'打开记录集
objrs.Open
'将指定记录数循环添加到objrs中
For lCount = 1 To nPageSize
objrs.AddNew
objrs!id = adoPrimaryRS!id
objrs!anumber = adoPrimaryRS!anumber
adoPrimaryRS.MoveNext
Next
'绑定
Set DataGrid1.DataSource = objrs
'显示页数
txtPage = lPage & "/" & adoPrimaryRS.PageCount
End SubPrivate Sub Form_Unload(Cancel As Integer)
If Not conn Is Nothing Then
conn.Close
End If
Set conn = Nothing
End Sub
完全可以引用其他网格控件上去的