关于读取数据并分页显示在表格中的问题,请指教,谢谢!
在网上获得一份读取数据并分页显示在表格中的代码,现在利用在了我的小软件中,我想在他的基础上进行改进,但一直不成功,目前达到的分页效果是

想达到的效果是 就是想加入首页和尾页


Private CN As ADODB.Connection Private RS As ADODB.Recordset Private PageCount As Long '总页数
Private Sub Build_Results(Optional Start_From = 0) On Error GoTo Err_1 Dim LI As ListItem ' ListItem object Dim Temp_Counter As Long Dim Last_Page As Long ' 当前记录集的最后一页 Dim Start_Page As Long '当前记录集的第一页 Dim X As Long LV_Results.ListItems.Clear Temp_Counter = 0 With RS If .RecordCount > 0 Then .Move Start_From * 13, 1 End If Do While Not .EOF And Temp_Counter < 13 ' DoEvents Set LI = LV_Results.ListItems.Add(, "K" & !Cur_ID, !Cur_Name) LI.SubItems(1) = !Country_Name LI.SubItems(2) = IIf(IsNull(!Alpha2_Code) = True, " ", !Alpha2_Code) LI.SubItems(3) = IIf(IsNull(!Currency_CodeA) = True, " ", !Currency_CodeA) LI.SubItems(4) = IIf(IsNull(!Currency_CodeN) = True, " ", !Currency_CodeN) .MoveNext Temp_Counter = Temp_Counter + 1 Loop T_Results.Caption = CStr(.RecordCount) ' 计算显示的记录数 If .RecordCount > 0 Then T_Showing_Records.Caption = (Start_From * 13) + 1 & " - " If (Start_From * 13) + 1 + 13 >= .RecordCount Then T_Showing_Records.Caption = T_Showing_Records.Caption & .RecordCount Else T_Showing_Records.Caption = T_Showing_Records.Caption & (Start_From * 13) + 13 End If Else T_Showing_Records.Caption = "0" End If ' 删除原来的记录导航显示 For T = 1 To T_Page.Count - 1 Unload T_Page(T) Next ' 获得最后一页 If .RecordCount Mod 13 > 0 Then Last_Page = Int(.RecordCount / 13) + 1 Else Last_Page = Int(.RecordCount / 13) End If PageCount = Last_Page '保存总页数,尾页需要使用 '获得要显示的第一页记录 For y = 1 To Last_Page Step 10 If Start_From + 1 >= y And Start_From + 1 <= y + 9 Then Exit For End If Next Start_Page = y X = 1 ' If we are showing pages not from first 20... <<- [ Previous ] If y > 1 Then '加载首页,并放到 <<- 之前 Load T_Page(T_Page.Count) T_Page(T_Page.Count - 1).Caption = "首页" T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(T_Page.Count - 2).Top T_Page(T_Page.Count - 1).Visible = True Load T_Page(T_Page.Count) T_Page(T_Page.Count - 1).Caption = "<<-" T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(T_Page.Count - 2).Top T_Page(T_Page.Count - 1).Visible = True End If For T = Start_Page To Last_Page Load T_Page(T_Page.Count) If X > 10 Then ' If there are more pages then we can show... ->> [ Next ] T_Page(T_Page.Count - 1).Caption = "->>" T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(T_Page.Count - 2).Top T_Page(T_Page.Count - 1).Visible = True '加载尾页,并放到 ->> 之后 Load T_Page(T_Page.Count) T_Page(T_Page.Count - 1).Caption = "尾页" T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(T_Page.Count - 2).Top T_Page(T_Page.Count - 1).Visible = True Exit For Else T_Page(T_Page.Count - 1).Caption = CStr(T) T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(T_Page.Count - 2).Top If T = Start_From + 1 Then ' If this is a current page T_Page(T_Page.Count - 1).ForeColor = &HFF& End If T_Page(T_Page.Count - 1).Visible = True End If X = X + 1 Next End With Exit_Sub: Exit Sub Err_1: MsgBox Err.Description, vbOKOnly + vbCritical + vbApplicationModal, "StaCS : System error # " & Err.Number Resume Exit_Sub End Sub Private Sub T_Page_Click(Index As Integer) On Error GoTo Err_1 Me.AutoRedraw = False 'AutoRedraw 就是控制是否自动重绘的属性 Select Case T_Page(Index).Caption Case "->>" Call Build_Results(Val(T_Page(Index - 1).Caption)) Case "<<-" Call Build_Results(Val(T_Page(Index + 1).Caption) - 2) Case "首页" Call Build_Results(0) Case "尾页" Call Build_Results(PageCount - 1) Case Else Call Build_Results(Val(T_Page(Index).Caption) - 1) End Select ' 都是对一个值进行判断,改用多分支选择。原代码注释掉 ' If T_Page(Index).Caption = "->>" Then '自动生成的页码 ' Call Build_Results(Val(T_Page(Index - 1).Caption)) ' ElseIf T_Page(Index).Caption = "<<-" Then ' Call Build_Results(Val(T_Page(Index + 1).Caption) - 2) ' Else ' Call Build_Results(Val(T_Page(Index).Caption) - 1) ' End If Me.AutoRedraw = True Exit_Sub: Exit Sub Err_1: Resume Exit_Sub End Sub
Select Case T_Page(Index).Caption Case "->>" Call Build_Results(Val(T_Page(Index - 1).Caption)) Case "<<-" Call Build_Results(Val(T_Page(Index + 1).Caption) - 2) Case "首页" Call Build_Results(0) Case "尾页" Call Build_Results(PageCount - 1) Case "上一页" Case "下一页" Case Else Call Build_Results(Val(T_Page(Index).Caption) - 1) End Select
Private CN As ADODB.Connection Private RS As ADODB.Recordset Private PageCount As Long '总页数 Private Page As Long '当前页数
Private Sub Build_Results(Optional Start_From As Long = 1) On Error GoTo Err_1 Dim LI As ListItem ' ListItem object Dim Temp_Counter As Long Dim Last_Page As Long ' 当前记录集的最后一页 Dim Start_Page As Long '当前记录集的第一页 LV_Results.ListItems.Clear Temp_Counter = 0 With RS .PageSize = 13 '设置每页大小 PageCount = .PageCount If .PageCount >= Start_From And Start_From > 0 Then '传递的页码有效 Page = Start_From '保存当前页码 .AbsolutePage = Page '翻页 Else Page = 1 '页码无效,忽略,从第1页 .AbsolutePage = Page '翻页 End If '显示记录 Do While Not .EOF And Temp_Counter < 13 ' DoEvents Set LI = LV_Results.ListItems.Add(, "K" & !Cur_ID, !Cur_Name) LI.SubItems(1) = !Country_Name LI.SubItems(2) = IIf(IsNull(!Alpha2_Code) = True, " ", !Alpha2_Code) LI.SubItems(3) = IIf(IsNull(!Currency_CodeA) = True, " ", !Currency_CodeA) LI.SubItems(4) = IIf(IsNull(!Currency_CodeN) = True, " ", !Currency_CodeN) .MoveNext Temp_Counter = Temp_Counter + 1 Loop T_Results.Caption = CStr(.RecordCount) ' 计算显示的记录数 If .RecordCount > 0 Then If (Page) * 13 > .RecordCount Then '如果是最后一页 T_Showing_Records.Caption = (Page - 1) * 13 + 1 & " - " & .RecordCount & " 共:" & .PageCount & " 页" Else T_Showing_Records.Caption = (Page - 1) * 13 + 1 & " - " & Page * 13 & " 共:" & .PageCount & " 页" End If Else T_Showing_Records.Caption = "0" End If ' 删除原来的记录导航显示 For T = 1 To T_Page.Count - 1 Unload T_Page(T) Next If Int(Page / 10) * 10 = Page Then '10的整数倍 Start_Page = Int(Page / 10 - 1) * 10 + 1 '首页 Else Start_Page = Int(Page / 10) * 10 + 1 '首页 End If If Start_Page + 10 > PageCount Then '尾页 Last_Page = PageCount Else Last_Page = Start_Page + 9 End If If Start_Page > 10 Then '当前页超过了第一屏 Call loadTPage("首页", False) '重复代码定义为过程调用 Call loadTPage("<<-", False) End If If Page > 1 Then Call loadTPage("前一页", False) End If For T = Start_Page To Last_Page Call loadTPage(CStr(T), T = Page) Next T If Page <> PageCount Then Call loadTPage("后一页", False) End If If Start_Page + 10 < PageCount Then '当前页与最后一页差一屏 Call loadTPage("->>", False) Call loadTPage("尾页", False) End If End With Exit_Sub: Exit Sub Err_1: MsgBox Err.Description, vbOKOnly + vbCritical + vbApplicationModal, "StaCS : System error # " & Err.Number Resume Exit_Sub End Sub Private Sub T_Page_Click(Index As Integer) On Error GoTo Err_1 Me.AutoRedraw = False 'AutoRedraw 就是控制是否自动重绘的属性 Select Case T_Page(Index).Caption Case "->>" Call Build_Results(Page + 10) '翻10页 ' '这是另一种翻法 ' If Int(Page / 10) * 10 = Page Then ' Call Build_Results(Int(Page / 10) * 10 + 1) ' Else ' Call Build_Results(Int(Page / 10 + 1) * 10 + 1) ' End If Case "<<-" Call Build_Results(Page - 10) ' '这是另一种翻法 ' If Int(Page / 10) * 10 = Page Then ' Call Build_Results(Int(Page / 10 - 2) * 10 + 1) ' Else ' Call Build_Results(Int(Page / 10 - 1) * 10 + 1) ' End If Case "首页" Call Build_Results(1) Case "尾页" Call Build_Results(PageCount) Case "前一页" Call Build_Results(Page - 1) Case "后一页" Call Build_Results(Page + 1) Case Else Call Build_Results(Val(T_Page(Index).Caption)) End Select Me.AutoRedraw = True Exit_Sub: Exit Sub Err_1: Resume Exit_Sub End Sub Public Sub loadTPage(T As String, Optional RedTF As Boolean = False) '代码重复很多,所以定义为过程 Load T_Page(T_Page.Count) T_Page(T_Page.Count - 1).Caption = T T_Page(T_Page.Count - 1).Left = T_Page(T_Page.Count - 2).Left + T_Page(T_Page.Count - 2).Width + 90 T_Page(T_Page.Count - 1).Top = T_Page(0).Top T_Page(T_Page.Count - 1).Visible = True If RedTF Then '如果是指定的页,改红色 T_Page(T_Page.Count - 1).ForeColor = &HFF& End If End Sub