Public SqlOperation As SqlOperation ' 定义 SqlOperation 对象 Public ListViewOperation1 As ListViewOperation ' 定义 ListViewOperation1 对象 Public ListViewOperation2 As ListViewOperation ' 定义 ListViewOperation2 对象 Public ListViewOperation3 As ListViewOperation ' 定义 ListViewOperation3 对象

Public Function Get_Code(ByVal Number As String) As String ' 定义函数 Get_Code,接受参数 Number,返回字符串类型 Get_Code = Number & WorksheetFunction.Text(Now, "-yymmdd-hhssmm-") & WorksheetFunction.RandBetween(1000, 9999) ' 生成指定格式的字符串并返回 End Function

Public Sub UserForm1Show() ' 定义 Sub 过程 UserForm1Show() Set SqlOperation = New SqlOperation ' 创建 SqlOperation 对象 SqlOperation.ExcelConnectionString = ThisWorkbook.FullName ' 设置 Excel 连接字符串为当前工作簿的全名

With UserForm1 ' 对 UserForm1 进行操作
    Set ListViewOperation1 = New ListViewOperation ' 创建 ListViewOperation1 对象
    ListViewOperation1.ListView = .ListView1 ' 设置 ListViewOperation1 的 ListView 属性为 UserForm1 中的 ListView1
    ListViewOperation1.HomePage = .CommandButton7 ' 设置 ListViewOperation1 的 HomePage 属性为 UserForm1 中的 CommandButton7
    ListViewOperation1.Left = .CommandButton8 ' 设置 ListViewOperation1 的 Left 属性为 UserForm1 中的 CommandButton8
    ListViewOperation1.Right = .CommandButton9 ' 设置 ListViewOperation1 的 Right 属性为 UserForm1 中的 CommandButton9
    ListViewOperation1.LastPage = .CommandButton10 ' 设置 ListViewOperation1 的 LastPage 属性为 UserForm1 中的 CommandButton10
    ListViewOperation1.Query = .CommandButton1 ' 设置 ListViewOperation1 的 Query 属性为 UserForm1 中的 CommandButton1
    ListViewOperation1.Reset = .CommandButton2 ' 设置 ListViewOperation1 的 Reset 属性为 UserForm1 中的 CommandButton2
    ListViewOperation1.ListControls.Add .TextBox1 ' 将 UserForm1 中的 TextBox1 添加到 ListViewOperation1 的 ListControls 集合中
    ListViewOperation1.ListControls.Add .TextBox2 ' 将 UserForm1 中的 TextBox2 添加到 ListViewOperation1 的 ListControls 集合中
    ListViewOperation1.QueryOnAction = "AddListView1" ' 设置 ListViewOperation1 的 QueryOnAction 属性为 AddListView1
    ListViewOperation1.DisplayCount = 39 ' 设置 ListViewOperation1 的 DisplayCount 属性为 39
    
    Set ListViewOperation2 = New ListViewOperation ' 创建 ListViewOperation2 对象
    ListViewOperation2.ListView = .ListView2 ' 设置 ListViewOperation2 的 ListView 属性为 UserForm1 中的 ListView2
    ListViewOperation2.HomePage = .CommandButton11 ' 设置 ListViewOperation2 的 HomePage 属性为 UserForm1 中的 CommandButton11
    ListViewOperation2.Left = .CommandButton12 ' 设置 ListViewOperation2 的 Left 属性为 UserForm1 中的 CommandButton12
    ListViewOperation2.Right = .CommandButton13 ' 设置 ListViewOperation2 的 Right 属性为 UserForm1 中的 CommandButton13
    ListViewOperation2.LastPage = .CommandButton14 ' 设置 ListViewOperation2 的 LastPage 属性为 UserForm1 中的 CommandButton14
    ListViewOperation2.Query = .CommandButton3 ' 设置 ListViewOperation2 的 Query 属性为 UserForm1 中的 CommandButton3
    ListViewOperation2.Reset = .CommandButton4 ' 设置 ListViewOperation2 的 Reset 属性为 UserForm1 中的 CommandButton4
    ListViewOperation2.StartDate = .TextBox7 ' 设置 ListViewOperation2 的 StartDate 属性为 UserForm1 中的 TextBox7
    ListViewOperation2.EndDate = .TextBox8 ' 设置 ListViewOperation2 的 EndDate 属性为 UserForm1 中的 TextBox8
    ListViewOperation2.ListControls.Add .ComboBox1 ' 将 UserForm1 中的 ComboBox1 添加到 ListViewOperation2 的 ListControls 集合中
    ListViewOperation2.ListControls.Add .TextBox3 ' 将 UserForm1 中的 TextBox3 添加到 ListViewOperation2 的 ListControls 集合中
    ListViewOperation2.ListControls.Add .TextBox4 ' 将 UserForm1 中的 TextBox4 添加到 ListViewOperation2 的 ListControls 集合中
    ListViewOperation2.ListControls.Add .TextBox7 ' 将 UserForm1 中的 TextBox7 添加到 ListViewOperation2 的 ListControls 集合中
    ListViewOperation2.ListControls.Add .TextBox8 ' 将 UserForm1 中的 TextBox8 添加到 ListViewOperation2 的 ListControls 集合中
    ListViewOperation2.QueryOnAction = "AddListView2" ' 设置 ListViewOperation2 的 QueryOnAction 属性为 AddListView2
    ListViewOperation2.DisplayCount = 39 ' 设置 ListViewOperation2 的 DisplayCount 属性为 39
    
    Set ListViewOperation3 = New ListViewOperation ' 创建 ListViewOperation3 对象
    ListViewOperation3.ListView = .ListView3 ' 设置 ListViewOperation3 的 ListView 属性为 UserForm1 中的 ListView3
    ListViewOperation3.HomePage = .CommandButton15 ' 设置 ListViewOperation3 的 HomePage 属性为 UserForm1 中的 CommandButton15
    ListViewOperation3.Left = .CommandButton16 ' 设置 ListViewOperation3 的 Left 属性为 UserForm1 中的 CommandButton16
    ListViewOperation3.Right = .CommandButton17 ' 设置 ListViewOperation3 的 Right 属性为 UserForm1 中的 CommandButton17
    ListViewOperation3.LastPage = .CommandButton18 ' 设置 ListViewOperation3 的 LastPage 属性为 UserForm1 中的 CommandButton18
    ListViewOperation3.Query = .CommandButton5 ' 设置 ListViewOperation3 的 Query 属性为 UserForm1 中的 CommandButton5
    ListViewOperation3.Reset = .CommandButton6 ' 设置 ListViewOperation3 的 Reset 属性为 UserForm1 中的 CommandButton6
    ListViewOperation3.StartDate = .TextBox10 ' 设置 ListViewOperation3 的 StartDate 属性为 UserForm1 中的 TextBox10
    ListViewOperation3.EndDate = .TextBox9 ' 设置 ListViewOperation3 的 EndDate 属性为 UserForm1 中的 TextBox9
    ListViewOperation3.ListControls.Add .ComboBox2 ' 将 UserForm1 中的 ComboBox2 添加到 ListViewOperation3 的 ListControls 集合中
    ListViewOperation3.ListControls.Add .TextBox5 ' 将 UserForm1 中的 TextBox5 添加到 ListViewOperation3 的 ListControls 集合中
    ListViewOperation3.ListControls.Add .TextBox6 ' 将 UserForm1 中的 TextBox6 添加到 ListViewOperation3 的 ListControls 集合中
    ListViewOperation3.ListControls.Add .TextBox9 ' 将 UserForm1 中的 TextBox9 添加到 ListViewOperation3 的 ListControls 集合中
    ListViewOperation3.ListControls.Add .TextBox10 ' 将 UserForm1 中的 TextBox10 添加到 ListViewOperation3 的 ListControls 集合中
    ListViewOperation3.QueryOnAction = "AddListView3" ' 设置 ListViewOperation3 的 QueryOnAction 属性为 AddListView3
    ListViewOperation3.DisplayCount = 39 ' 设置 ListViewOperation3 的 DisplayCount 属性为 39
    
    NewlyListView .ListView1, Sheet4.ListObjects("ListView1").DataBodyRange.value ' 调用 NewlyListView 过程,将 Sheet4 中的 ListView1 数据添加到 UserForm1 的 ListView1 中
    NewlyListView .ListView2, Sheet4.ListObjects("ListView2").DataBodyRange.value ' 调用 NewlyListView 过程,将 Sheet4 中的 ListView2 数据添加到 UserForm1 的 ListView2 中
    NewlyListView .ListView3, Sheet4.ListObjects("ListView3").DataBodyRange.value ' 调用 NewlyListView 过程,将 Sheet4 中的 ListView3 数据添加到 UserForm1 的 ListView3 中
   
    AddListView1 ' 调用 AddListView1 过程
    Addlistview2 ' 调用 Addlistview2 过程
    Addlistview3 ' 调用 Addlistview3 过程
    
    .Show 0 ' 显示 UserForm1
End With

End Sub

Public Sub NewlyListView(ByVal ListView As ListView, ByVal Arr As Variant) ' 定义 Sub 过程 NewlyListView,接受参数 ListView 和 Arr Dim i As Integer ' 定义 i 变量 With ListView ' 对 ListView 进行操作 .View = lvwReport ' 设置 View 属性为 lvwReport .Gridlines = True ' 设置 Gridlines 属性为 True .FullRowSelect = True ' 设置 FullRowSelect 属性为 True .LabelEdit = lvwManual ' 设置 LabelEdit 属性为 lvwManual .AllowColumnReorder = True ' 设置 AllowColumnReorder 属性为 True .ColumnHeaders.Clear ' 清除所有的 ColumnHeaders For i = 1 To UBound(Arr) ' 循环设置 ColumnHeaders .ColumnHeaders.Add Arr(i, 1), , Arr(i, 2), Arr(i, 3), 0 ' 添加 ColumnHeaders Next End With End Sub

Public Sub SetWidth() ' 定义 Sub 过程 SetWidth() Dim i As Integer ' 定义 i 变量 With UserForm1.ListView2.ColumnHeaders ' 对 UserForm1 的 ListView2 的 ColumnHeaders 进行操作 For i = 1 To .Count ' 循环设置每个 ColumnHeader 的宽度 Sheet4.ListObjects("ListView2").ListRows(i).Range(, 3).value = .Item(i).Width ' 设置 Sheet4 中的 ListView2 的每个 ListRow 的第 3 列为 ListView2 中对应的 ColumnHeader 的宽度 Next End With End Sub

Public Sub AddListView1() ' 定义 Sub 过程 AddListView1() Dim sql As String, Arr As Variant ' 定义 sql 字符串和 Arr 数组 With UserForm1 ' 对 UserForm1 进行操作 sql = "select ID,货号,名称,厂家,规格型号,存放温度,单位,入库数量,出库数量,库存数量 from [试剂$] where 1=1" ' 设置 sql 语句 If .TextBox1.Text <> "" Then ' 如果 UserForm1 中的 TextBox1 不为空 sql = sql & " and 名称 like '%" & .TextBox1.Text & "%'" ' 向 sql 语句中添加条件 End If If .TextBox2.Text <> "" Then ' 如果 UserForm1 中的 TextBox2 不为空 sql = sql & " and 厂家 like '%" & .TextBox2.Text & "%'" ' 向 sql 语句中添加条件 End If SqlOperation.SelectCommand = sql ' 设置 SqlOperation 的 SelectCommand 属性为 sql 语句 If SqlOperation.GetRstDataBoolean(Arr) Then ' 如果 SqlOperation 的 GetRstDataBoolean 方法返回 True ListViewOperation1.DataSource = Arr ' 设置 ListViewOperation1 的 DataSource 属性为 Arr 数组 Else ListViewOperation1.CommandButtonEnabled_False ' 设置 ListViewOperation1 的 CommandButtonEnabled_False 属性为 True End If End With End Sub

Public Sub Addlistview2() ' 定义 Sub 过程 Addlistview2() Dim sql As String, Arr As Variant ' 定义 sql 字符串和 Arr 数组 With UserForm1 ' 对 UserForm1 进行操作 sql = "select ID,货号,名称,厂家,规格型号,存放温度,单位,批号,有效期,入库人员,数量,日期,出库数量,库存数量 from [入库$] where 1=1" ' 设置 sql 语句 If .TextBox3.Text <> "" Then ' 如果 UserForm1 中的 TextBox3 不为空 sql = sql & " and 名称 like '%" & .TextBox3.Text & "%'" ' 向 sql 语句中添加条件 End If If .TextBox4.Text <> "" Then ' 如果 UserForm1 中的 TextBox4 不为空 sql = sql & " and 厂家 like '%" & .TextBox4.Text & "%'" ' 向 sql 语句中添加条件 End If If .TextBox7.Text <> "" Then ' 如果 UserForm1 中的 TextBox7 不为空 sql = sql & " and 日期>=#" & .TextBox7.Text & "#" ' 向 sql 语句中添加条件 End If If .TextBox8.Text <> "" Then ' 如果 UserForm1 中的 TextBox8 不为空 sql = sql & " and 日期<=#" & .TextBox8.Text & "#" ' 向 sql 语句中添加条件 End If If .ComboBox1.Text <> "" Then ' 如果 UserForm1 中的 ComboBox1 不为空 sql = sql & " and 入库人员='" & .ComboBox1.Text & " '" ' 向 sql 语句中添加条件 End If SqlOperation.SelectCommand = sql & " order by 日期 desc" ' 设置 SqlOperation 的 SelectCommand 属性为 sql 语句 If SqlOperation.GetRstDataBoolean(Arr) Then ' 如果 SqlOperation 的 GetRstDataBoolean 方法返回 True ListViewOperation2.DataSource = Arr ' 设置 ListViewOperation2 的 DataSource 属性为 Arr 数组 Else ListViewOperation2.CommandButtonEnabled_False ' 设置 ListViewOperation2 的 CommandButtonEnabled_False 属性为 True End If End With End Sub

Public Sub Addlistview3() ' 定义 Sub 过程 Addlistview3() Dim sql As String, Arr As Variant ' 定义 sql 字符串和 Arr 数组 With UserForm1 ' 对 UserForm1 进行操作 sql = "select ID,货号,名称,厂家,规格型号,存放温度,单位,批号,有效期,出库人员,数量,日期,备注 from [出库$] where 1=1" ' 设置 sql 语句 If .TextBox5.Text <> "" Then ' 如果 UserForm1 中的 TextBox5 不为空 sql = sql & " and 名称 like '%" & .TextBox5.Text & "%'" ' 向 sql 语句中添加条件 End If If .TextBox6.Text <> "" Then ' 如果 UserForm1 中的 TextBox6 不为空 sql = sql & " and 厂家 like '%" & .TextBox6.Text & "%'" ' 向 sql 语句中添加条件 End If If .TextBox10.Text <> "" Then ' 如果 UserForm1 中的 TextBox10 不为空 sql = sql & " and 日期>=#" & .TextBox10.Text & "#" ' 向 sql 语句中添加条件 End If If .TextBox9.Text <> "" Then ' 如果 UserForm1 中的 TextBox9 不为空 sql = sql & " and 日期<=#" & .TextBox9.Text & "#" ' 向 sql 语句中添加条件 End If If .ComboBox2.Text <> "" Then ' 如果 UserForm1 中的 ComboBox2 不为空 sql = sql & " and 出库人员='" & .ComboBox2.Text & " '" ' 向 sql 语句中添加条件 End If SqlOperation.SelectCommand = sql & " order by 日期 desc" ' 设置 SqlOperation 的 SelectCommand 属性为 sql 语句 If SqlOperation.GetRstDataBoolean(Arr) Then ' 如果 SqlOperation 的 GetRstDataBoolean 方法返回 True ListViewOperation3.DataSource = Arr ' 设置 ListViewOperation3 的 DataSource 属性为 Arr 数组 Else ListViewOperation3.CommandButtonEnabled_False ' 设置 ListViewOperation3 的 CommandButtonEnabled_False 属性为 True End If End With End Sub

VBA 代码逐句翻译:使用 ListView 和 SQL 操作

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

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