Public UserFormName As String ' 定义公共变量UserFormName,表示用户窗体名称 Public DisplayCount As Integer ' 定义公共变量DisplayCount,表示每页显示的数据数量 Public QueryOnAction As String ' 定义公共变量QueryOnAction,表示查询按钮的执行方法名称 Public QueryOnAction1 As String ' 定义公共变量QueryOnAction1,表示查询按钮的执行方法名称 Public ListControls As Collection ' 定义公共变量ListControls,表示控件集合

Private AddMethodName_ As String ' 定义私有变量AddMethodName_,表示添加方法名称 Private DataSource_ As Variant ' 定义私有变量DataSource_,表示数据源 Private StartRow_ As Integer ' 定义私有变量StartRow_,表示当前页的起始行数 Private EndRow_ As Integer ' 定义私有变量EndRow_,表示当前页的结束行数 Private Count As Integer ' 定义私有变量Count,表示数据总量 Private WithEvents ListView_ As ListView ' 定义有事件的变量ListView_,表示展示数据的列表框 Private WithEvents Left_ As MSForms.CommandButton ' 定义有事件的变量Left_,表示左箭头按钮 Private WithEvents Right_ As MSForms.CommandButton ' 定义有事件的变量Right_,表示右箭头按钮 Private WithEvents HomePage_ As MSForms.CommandButton ' 定义有事件的变量HomePage_,表示首页按钮 Private WithEvents LastPage_ As MSForms.CommandButton ' 定义有事件的变量LastPage_,表示末页按钮 Private WithEvents Newly_ As MSForms.CommandButton ' 定义有事件的变量Newly_,表示新增按钮 Private WithEvents Query_ As MSForms.CommandButton ' 定义有事件的变量Query_,表示查询按钮 Private WithEvents Reset_ As MSForms.CommandButton ' 定义有事件的变量Reset_,表示重置按钮 Private WithEvents StartDate_ As MSForms.TextBox ' 定义有事件的变量StartDate_,表示开始日期文本框 Private WithEvents EndDate_ As MSForms.TextBox ' 定义有事件的变量EndDate_,表示结束日期文本框

Public Property Let Newly(ByVal x As MSForms.CommandButton) Set Newly_ = x ' 设置Newly_变量为x End Property

Public Property Let Query(ByVal x As MSForms.CommandButton) Set Query_ = x ' 设置Query_变量为x End Property

Public Property Let Reset(ByVal x As MSForms.CommandButton) Set Reset_ = x ' 设置Reset_变量为x End Property

Public Property Let ListView(ByVal x As ListView) Set ListView_ = x ' 设置ListView_变量为x End Property

Public Property Let Left(ByVal x As MSForms.CommandButton) Set Left_ = x ' 设置Left_变量为x End Property

Public Property Let Right(ByVal x As MSForms.CommandButton) Set Right_ = x ' 设置Right_变量为x End Property

Public Property Let HomePage(ByVal x As MSForms.CommandButton) Set HomePage_ = x ' 设置HomePage_变量为x End Property

Public Property Let LastPage(ByVal x As MSForms.CommandButton) Set LastPage_ = x ' 设置LastPage_变量为x End Property

Public Property Let StartDate(ByVal x As MSForms.TextBox) Set StartDate_ = x ' 设置StartDate_变量为x End Property

Public Property Let EndDate(ByVal x As MSForms.TextBox) Set EndDate_ = x ' 设置EndDate_变量为x End Property

Public Property Let AddMethodName(ByVal x As String) AddMethodName_ = x ' 设置AddMethodName_变量为x End Property

Public Property Get GetDatSourceID() As String GetDatSourceID = ListView_.SelectedItem.Tag ' 获取ListView_的选中项的Tag属性值 End Property

Public Property Let DataSource(ByVal x As Variant) DataSource_ = x ' 设置DataSource_变量为x Count = UBound(DataSource_, 2) ' 获取DataSource_的列数,即数据总量 StartRow_ = 0 ' 将StartRow_变量设置为0 If Count > DisplayCount Then ' 如果数据总量大于每页显示数量 EndRow_ = DisplayCount ' 将EndRow_变量设置为每页显示数量 Right_.Enabled = True ' 启用右箭头按钮 LastPage_.Enabled = True ' 启用末页按钮 Else ' 否则 EndRow_ = Count ' 将EndRow_变量设置为数据总量 Right_.Enabled = False ' 禁用右箭头按钮 LastPage_.Enabled = False ' 禁用末页按钮 End If Left_.Enabled = False ' 禁用左箭头按钮 HomePage_.Enabled = False ' 禁用首页按钮 AddListView ' 调用AddListView子程序 End Property

Public Sub CommandButtonEnabled_False() ' 定义CommandButtonEnabled_False子程序,用于禁用所有按钮并清空ListView_ Right_.Enabled = False Left_.Enabled = False HomePage_.Enabled = False LastPage_.Enabled = False ListView_.ListItems.Clear End Sub

Private Sub AddListView() ' 定义AddListView子程序,用于更新ListView_的数据 Dim i As Integer, r As Integer, iCount As Integer With ListView_.ListItems ' 获取ListView_的ListItems属性 .Clear ' 清空ListItems iCount = UBound(DataSource_) ' 获取DataSource_的列数 For i = StartRow_ To EndRow_ ' 循环遍历当前页的数据 If Not IsNull(DataSource_(0, i)) Then ' 如果数据不为空 With .Add ' 在ListItems中添加一行数据 .Tag = DataSource_(0, i) ' 设置Tag属性值为数据的第一列 .Text = DataSource_(1, i) ' 设置Text属性值为数据的第二列 For r = 2 To iCount ' 循环遍历数据的除第一、二列以外的其他列 If Not IsNull(DataSource_(r, i)) Then ' 如果数据不为空 .SubItems(r - 1) = DataSource_(r, i) ' 将数据设置为SubItems的属性值 End If Next End With End If Next End With End Sub

Private Sub Query__Click() ' 定义Query__Click事件,用于执行查询操作 Application.Run QueryOnAction ' 执行QueryOnAction方法 If QueryOnAction1 <> '' Then ' 如果QueryOnAction1不为空 Application.Run QueryOnAction1 ' 执行QueryOnAction1方法 End If End Sub

Private Sub Reset__Click() ' 定义Reset__Click事件,用于重置数据 Dim i As Integer For i = 1 To ListControls.Count ' 循环遍历ListControls ListControls(i).Text = '' ' 将控件的文本清空 Next Query__Click ' 执行Query__Click事件 End Sub

Private Sub Newly__Click() ' 定义Newly__Click事件,用于新增数据 Dim i As Integer For i = 0 To VBA.UserForms.Count - 1 ' 循环遍历当前文档中的所有用户窗体 If VBA.UserForms(i).Name = UserFormName Then ' 如果存在同名的用户窗体 Unload VBA.UserForms(i) ' 卸载该窗体 Exit For ' 退出循环 End If Next VBA.UserForms.Add(UserFormName).Newly ' 创建名为UserFormName的用户窗体并执行其中的Newly事件 End Sub

'Private Sub ListView__DblClick() ' Dim i As Integer ' If ListView_.SelectedItem Is Nothing Then Exit Sub ' For i = 0 To VBA.UserForms.Count - 1 ' If VBA.UserForms(i).Name = UserFormName Then ' Unload VBA.UserForms(i) ' Exit For ' End If ' Next ' CallByName VBA.UserForms.Add(UserFormName), AddMethodName_, VbMethod, ListView_.SelectedItem.Tag 'End Sub

Private Sub HomePage__Click() ' 定义HomePage__Click事件,用于回到首页 Right_.Enabled = True ' 启用右箭头按钮 LastPage_.Enabled = True ' 启用末页按钮 Left_.Enabled = False ' 禁用左箭头按钮 HomePage_.Enabled = False ' 禁用首页按钮 StartRow_ = 0 ' 将StartRow_变量设置为0 EndRow_ = DisplayCount ' 将EndRow_变量设置为每页显示数量 AddListView ' 调用AddListView子程序 End Sub

Private Sub Left__Click() ' 定义Left__Click事件,用于向左翻页 Right_.Enabled = True ' 启用右箭头按钮 LastPage_.Enabled = True ' 启用末页按钮 StartRow_ = StartRow_ - (DisplayCount + 1) ' 将StartRow_变量向左移动DisplayCount+1 If StartRow_ = 0 Then ' 如果StartRow_变量为0 Left_.Enabled = False ' 禁用左箭头按钮 HomePage_.Enabled = False ' 禁用首页按钮 EndRow_ = DisplayCount ' 将EndRow_变量设置为每页显示数量 Else ' 否则 EndRow_ = StartRow_ + DisplayCount ' 将EndRow_变量设置为StartRow_+每页显示数量 End If AddListView ' 调用AddListView子程序 End Sub

Private Sub Right__Click() ' 定义Right__Click事件,用于向右翻页 StartRow_ = StartRow_ + DisplayCount + 1 ' 将StartRow_变量向右移动DisplayCount+1 Left_.Enabled = True ' 启用左箭头按钮 HomePage_.Enabled = True ' 启用首页按钮 If StartRow_ + DisplayCount < Count Then ' 如果当前页的结束行数小于数据总量 EndRow_ = StartRow_ + DisplayCount ' 将EndRow_变量设置为StartRow_+每页显示数量 Right_.Enabled = True ' 启用右箭头按钮 LastPage_.Enabled = True ' 启用末页按钮 Else ' 否则 EndRow_ = Count ' 将EndRow_变量设置为数据总量 Right_.Enabled = False ' 禁用右箭头按钮 LastPage_.Enabled = False ' 禁用末页按钮 End If AddListView ' 调用AddListView子程序 End Sub

Private Sub LastPage__Click() ' 定义LastPage__Click事件,用于跳转到末页 Dim S As Integer Right_.Enabled = False ' 禁用右箭头按钮 LastPage_.Enabled = False ' 禁用末页按钮 Left_.Enabled = True ' 启用左箭头按钮 HomePage_.Enabled = True ' 启用首页按钮 S = Int(Count / (DisplayCount + 1)) * (DisplayCount + 1) ' 计算末页的起始行数 StartRow_ = S ' 将StartRow_变量设置为末页的起始行数 EndRow_ = Count ' 将EndRow_变量设置为数据总量 AddListView ' 调用AddListView子程序 End Sub

Private Sub StartDate__MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single) Dim dateValue As Date If Button = 1 Then dateValue = CalendarForm.GetDate If dateValue > 0 Then StartDate_.Text = dateValue End If End If End Sub

Private Sub EndDate__MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single) Dim dateValue As Date If Button = 1 Then dateValue = CalendarForm.GetDate If dateValue > 0 Then EndDate_.Text = dateValue End If End If End Sub

Private Sub Class_Initialize() AddMethodName_ = 'Add' Set ListControls = New Collection End Sub

VBA代码逐句翻译:用户窗体数据分页显示

原文地址: https://www.cveoy.top/t/topic/lCBG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录