首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

奇怪现象:分页设置如何不好使呀

2012-02-13 
奇怪现象:分页设置怎么不好使呀?用程序读取dbf数据,由于数据量太大,需要分页显示,怎么发现分页设置不好使,

奇怪现象:分页设置怎么不好使呀?
用程序读取dbf数据,由于数据量太大,需要分页显示,怎么发现分页设置不好使,程序老出错!
        Conn.ConnectionString   =   "Provider=MSDASQL.1;Persist   Security   Info=False;Driver={Microsoft   Visual   FoxPro   Driver};SourceType=DBF;SourceDB= "   &   FilePath
        Conn.Open
        Rs.Open   "select   *   from   "   &   File,   Conn,   1,   3
        Rs.PageSize   =   50
        Rs.AbsolutePage   =   2
        Print   Rs.RecordCount           '发现记录数仍是7万多条
        Set   Grid.DataSource   =   Rs   '该行出错

[解决办法]
Set Grid.DataSource = Rs
会不会是Grid名称不对呢?
是什么错误呢?
[解决办法]
给你个分页的例子
Option Explicit

Dim conn As ADODB.Connection

Dim lCurrentPage As Long


Private Sub cmdNext_Click()

lCurrentPage = lCurrentPage + 1

Call Loadcontrol(lCurrentPage)

End Sub




Private Sub cmdPrevious_Click()

If lCurrentPage > 1 Then

lCurrentPage = lCurrentPage - 1

Call Loadcontrol(lCurrentPage)

End If

End Sub



Private 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)
On Error Resume Next

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.Fields( "id ") = adoPrimaryRS.Fields( "id ")
objrs.Fields( "anumber ") = adoPrimaryRS.Fields( "anumber ")
adoPrimaryRS.MoveNext

Next

'绑定

Set DataGrid1.DataSource = objrs



'在文本框显示页数

txtPage = lPage & "/ " & adoPrimaryRS.PageCount

End Sub



Private Sub Form_Unload(Cancel As Integer)

If Not conn Is Nothing Then

conn.Close

End If



Set conn = Nothing

End Sub

'文本框中输入页数,回车跳转到指定位置

Private Sub txtPage_KeyDown(KeyCode As Integer, Shift As Integer)

lCurrentPage = Val(txtPage.Text)

Call Loadcontrol(lCurrentPage)

End Sub


热点排行