用mshflexgrid控件实现数据库翻页
========================
打开vb6.0
在窗体中加入2个command控件,1个mshflexgrid控件
代码:
-----------------------------
Dim intpage As Integer
Private Sub Command1_Click()
intpage = intpage - 1
page intpage
End Sub
Private Sub Command2_Click()
intpage = intpage + 1
page intpage
End Sub
Sub page(intpagecount As Integer)
On Error Resume Next
Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim rs As New ADODB.Recordset
MSHFlexGrid1.Clear
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & App.Path & "\Students.mdb"
cn.Open
cm.ActiveConnection = cn
rs.CursorLocation = adUseClient
rs.Open "select * from 新生入学信息表 ", cn, adOpenDynamic, adLockBatchOptimistic
rs.PageSize = 10
If intpagecount > rs.PageCount Then
intpagecount = rs.PageCount
End If
If intpagecount < 1 Then
intpagecount = 1
End If
rs.AbsolutePage = intpagecount
'MsgBox rs.Fields.Count
With MSHFlexGrid1
.Cols = rs.Fields.Count
.Rows = 10
For i = 0 To 9
For j = 0 To rs.Fields.Count - 1
.TextMatrix(i, j) = rs.Fields(j)
Next
rs.MoveNext
Next
End With
End Sub
Private Sub Form_Load()
Command2.Caption = "下一页"
Command1.Caption = "上一页"
page 1
End Sub
作者主页:http://risk.y365.com