ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 25480|回复: 59

[原创]listview和treeview的组合应用小例子

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-5-2 23:20 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:控件

Option Explicit
Dim CNN As New ADODB.Connection
Dim RST As New ADODB.Recordset
Dim txtSQL As String
Dim iCount As Integer, i As Integer, j As Integer, k As Integer
Dim F_Key As String, S_Key As String
Dim XArray

Private Sub CommandButton1_Click()
    Unload Me
End Sub

Private Sub CommandButton2_Click()
    txtSQL = "select 产品类别,销售区域,订单类型,付款方式,订单日期,订单号码,客户名称,项目,产品规格,订单数量," _
            & "订单价格,订单基准价,出货日期,出货量,批次,出货日期基准价,业务员 from " _
            & "[sheet1$A2:Q2000]"
    RST.Open txtSQL, CNN, adOpenKeyset, adLockPessimistic
    iCount = RST.RecordCount
    On Error Resume Next
    For i = 1 To iCount
        For j = 1 To 5
            For k = 0 To j - 1
                F_Key = F_Key & RST.Fields(k) & ","
                S_Key = S_Key & RST.Fields(k) & ","
            Next k
            F_Key = Left(F_Key, Len(F_Key) - 1)
            S_Key = S_Key & RST.Fields(j)
            TreeView1.Nodes.Add F_Key, tvwChild, S_Key, RST.Fields(j), 3, 1
            S_Key = ""
            F_Key = ""
        Next j
        RST.MoveNext
    Next i
    RST.Close
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If InStr(Node.Key, ",") = 0 Then Exit Sub
    Dim Temp
    txtSQL = ""
    Temp = Split(Node.Key, ",")
    For i = LBound(Temp) To UBound(Temp) - 1
        If XArray(i) <> "订单日期" Then
            txtSQL = txtSQL & XArray(i) & "='" & Temp(i) & "' and "
        Else
            txtSQL = txtSQL & XArray(i) & "=#" & Temp(i) & "# and "
        End If
    Next i
   
    If XArray(i) <> "订单日期" Then
        txtSQL = "SELECT 产品类别,销售区域,订单类型,付款方式,订单日期,订单号码,客户名称,项目,产品规格,订单数量," _
                & "订单价格,订单基准价,出货日期,出货量,批次,出货日期基准价,业务员 FROM [sheet1$A2:Q2000] WHERE " & txtSQL & XArray(i) & "='" & Temp(i) & "'"
    Else
        txtSQL = "SELECT 产品类别,销售区域,订单类型,付款方式,订单日期,订单号码,客户名称,项目,产品规格,订单数量," _
                & "订单价格,订单基准价,出货日期,出货量,批次,出货日期基准价,业务员 FROM [sheet1$A2:Q2000] WHERE " & txtSQL & XArray(i) & "=#" & Temp(i) & "#"
    End If
   
    RST.Open txtSQL, CNN, adOpenKeyset, adLockPessimistic
    iCount = RST.RecordCount
   
    LstvwInit
    With ListView1
        For j = UBound(Temp) + 1 To RST.Fields.Count - 1
            .ColumnHeaders.Add , , RST.Fields(j).Name, 80
        Next j
        For i = 1 To iCount
            .ListItems.Add , , RST.Fields(UBound(Temp) + 1)
            For j = UBound(Temp) + 2 To RST.Fields.Count - 1
                .ListItems(i).SubItems(j - UBound(Temp) - 1) = RST.Fields(j)
            Next j
        RST.MoveNext
        Next i
    End With
    RST.Close
End Sub

Private Sub UserForm_Initialize()
    XArray = Array("产品类别", "销售区域", "订单类型", "付款方式", "订单日期", "订单号码")
    TrvwInit
    With TreeView1
        .Nodes.Add , , "订单汇总", "订单汇总", 4, 1
        .Nodes.Item(1).Expanded = True
        .Nodes.Add "订单汇总", tvwChild, "CHIP", "CHIP", 4, 1
        .Nodes.Add "订单汇总", tvwChild, "POY", "POY", 4, 1
        .Nodes.Add "订单汇总", tvwChild, "FDY", "FDY", 4, 1
        .Nodes.Add "订单汇总", tvwChild, "DTY", "DTY", 4, 1
    End With
    CNN.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=yes;imex=1';data source=" _
        & ThisWorkbook.Path & "\订单信息.xls"

End Sub

Sub LstvwInit()
    With ListView1
        .ColumnHeaders.Clear
        .ListItems.Clear
        .View = lvwReport
        .FullRowSelect = True
        .Gridlines = True
        .LabelEdit = lvwManual
    End With
End Sub

Sub TrvwInit()
    With TreeView1
        .LabelEdit = tvwManual
        .ImageList = ImageList1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusPictureText
        .Nodes.Clear
    End With
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CNN.State = 1 Then CNN.Close
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
Dim i%
    With ListView1
        If (ColumnHeader.Index - 1) = .SortKey Then
            If ColumnHeader.Text = "订单数量" Or ColumnHeader.Text = "出货量" Then
                For i = 1 To .ListItems.Count
                    .ListItems(i).SubItems(ColumnHeader.Index - 1) = Format(.ListItems(i).SubItems(ColumnHeader.Index - 1), "000000000.000")
                Next i
            End If
                 .SortOrder = (.SortOrder + 1) Mod 2
            If ColumnHeader.Text = "订单数量" Or ColumnHeader.Text = "出货量" Then
                For i = 1 To .ListItems.Count
                    .ListItems(i).SubItems(ColumnHeader.Index - 1) = Val(.ListItems(i).SubItems(ColumnHeader.Index - 1))
                Next i
            End If
        Else
            If ColumnHeader.Text = "订单数量" Or ColumnHeader.Text = "出货量" Then
                For i = 1 To .ListItems.Count
                    .ListItems(i).SubItems(ColumnHeader.Index - 1) = Format(.ListItems(i).SubItems(ColumnHeader.Index - 1), "000000000.000")
                Next i
            End If
            .Sorted = False
            .SortOrder = 0
            .SortKey = ColumnHeader.Index - 1
            .Sorted = True
            If ColumnHeader.Text = "订单数量" Or ColumnHeader.Text = "出货量" Then
                For i = 1 To .ListItems.Count
                    .ListItems(i).SubItems(ColumnHeader.Index - 1) = Val(.ListItems(i).SubItems(ColumnHeader.Index - 1))
                Next i
            End If
        End If
    End With
End Sub

 

G2kmet2Q.rar (54.91 KB, 下载次数: 1877)
[此贴子已经被作者于2007-5-4 12:12:14编辑过]

7JKnyOgb.rar

52.78 KB, 下载次数: 1389

[原创]listview和treeview的组合应用小例子

TA的精华主题

TA的得分主题

发表于 2007-5-3 07:18 | 显示全部楼层
这么好的东西怎么没人顶呢!这才叫功夫!

TA的精华主题

TA的得分主题

发表于 2007-5-3 07:26 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

这么好的东东建议加精

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-5-4 12:14 | 显示全部楼层

重新修改了一下,增加了listview的排序功能,以及listview的数值排序功能(listview只支持按文本方式排序),附件已在一楼重新更新过了。

TA的精华主题

TA的得分主题

发表于 2007-5-4 14:12 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-5-4 14:16 | 显示全部楼层
这个东西太强了嘛,界面也舒服!

TA的精华主题

TA的得分主题

发表于 2007-5-16 10:57 | 显示全部楼层
正用的上,顶一个!!!!!11

TA的精华主题

TA的得分主题

发表于 2007-8-1 10:00 | 显示全部楼层
不错不错,但为什么必须刷新一下才能显示下面的节点呢?

TA的精华主题

TA的得分主题

发表于 2007-8-1 17:24 | 显示全部楼层

请问为何我打开的时候显示"找不到工程或库"呢?/

TA的精华主题

TA的得分主题

发表于 2007-8-1 22:38 | 显示全部楼层

就喜欢这个东东,做查询界面超经典,向楼主学习,早日做出自己的东东来!

[em17]
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-12-22 11:05 , Processed in 0.036189 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表