楼主 allall |
UrserForm1的代碼:
- Option Explicit
- Private Sub Calendar1_Click()
- Me.ComboBox3.Clear
- Me.ComboBox3.AddItem Me.Calendar1.Value
- Me.ComboBox3.ListIndex = 0
- Me.Calendar1.Visible = False
- If Me.ComboBox4.Value = "" Then Exit Sub
- If CDate(Me.ComboBox4.Value) <= CDate(Me.ComboBox3.Value) Then
- MsgBox "第二個日期必須大於第一個日期", vbInformation, "友情提示"
- Me.ComboBox3.Clear
- Exit Sub
- End If
- End Sub
- Private Sub Calendar2_Click()
- Me.ComboBox4.Clear
- Me.ComboBox4.AddItem Me.Calendar2.Value
- Me.ComboBox4.ListIndex = 0
- Me.Calendar2.Visible = False
- If Me.ComboBox3.Value = "" Then Exit Sub
- If CDate(Me.ComboBox4.Value) <= CDate(Me.ComboBox3.Value) Then
- MsgBox "第二個日期必須大於第一個日期", vbInformation, "友情提示"
- Me.ComboBox4.Clear
- Exit Sub
- End If
- End Sub
- Private Sub CheckBox1_Click()
- Dim r&, lvwCount&
- Dim MyCol1 As New Collection
- Dim MyCol2 As New Collection
- Dim MyCol5 As New Collection
- If Me.CheckBox1.Value = True Then
- Me.CheckBox2.Value = False
- Me.ComboBox1.Enabled = True
- Me.ComboBox2.Enabled = True
- Me.ComboBox5.Enabled = True
- Me.Label2.Enabled = True
- Me.Label4.Enabled = True
- Me.Label13.Enabled = True
- lvwCount = Me.ListView1.ListItems.Count
- On Error Resume Next
- For r = 1 To lvwCount
- MyCol1.Add Me.ListView1.ListItems(r).SubItems(1), Me.ListView1.ListItems(r).SubItems(1)
- MyCol2.Add Me.ListView1.ListItems(r).SubItems(2), Me.ListView1.ListItems(r).SubItems(2)
- MyCol5.Add Me.ListView1.ListItems(r).SubItems(4), Me.ListView1.ListItems(r).SubItems(4)
- Err.Clear
- Next
- For r = 1 To MyCol1.Count
- Me.ComboBox1.AddItem MyCol1(r)
- Next
- For r = 1 To MyCol2.Count
- Me.ComboBox2.AddItem MyCol2(r)
- Next
- For r = 1 To MyCol5.Count
- Me.ComboBox5.AddItem MyCol5(r)
- Next
- Else
- Me.ComboBox1.Clear
- Me.ComboBox1.Enabled = False
- Me.ComboBox2.Clear
- Me.ComboBox2.Enabled = False
- Me.ComboBox5.Clear
- Me.ComboBox5.Enabled = False
- Me.Label2.Enabled = False
- Me.Label4.Enabled = False
- Me.Label13.Enabled = False
- End If
- End Sub
- Private Sub CheckBox2_Click()
- If Me.CheckBox2.Value = True Then
- Me.CheckBox1.Value = False
- Me.ComboBox3.Enabled = True
- Me.ComboBox4.Enabled = True
- Me.Label1.Enabled = True
- Me.Label3.Enabled = True
- Else
- Me.ComboBox3.Clear
- Me.ComboBox3.Enabled = False
- Me.ComboBox4.Clear
- Me.ComboBox4.Enabled = False
- Me.Label1.Enabled = False
- Me.Label3.Enabled = False
- End If
- End Sub
- Private Sub CommandButton2_Click()
- Dim lvwArr
- Dim r&, lvwCount&
- Dim DateSet As Date, DateSet1 As Date, DateSet2 As Date
- Dim txtFind1 As String, txtFind2 As String, txtFind5 As String
- On Error GoTo Errhander
- If Me.CheckBox2.Value = True Then
- DateSet1 = CDate(Me.ComboBox3.Value)
- DateSet2 = CDate(Me.ComboBox4.Value)
- lvwCount = Me.ListView1.ListItems.Count
- If lvwCount = 0 Then Exit Sub
- 'ReDim lvwArr(1 To lvwCount, 1 To 8)
- For r = lvwCount To 1 Step -1
- DateSet = CDate(Me.ListView1.ListItems(r).SubItems(5))
- If DateSet < DateSet1 Then
- Me.ListView1.ListItems.Remove (r)
- ElseIf DateSet > DateSet2 Then
- Me.ListView1.ListItems.Remove (r)
- End If
- Next
- ElseIf Me.CheckBox1.Value = True Then
- txtFind1 = Me.ComboBox1.Text
- txtFind2 = Me.ComboBox2.Text
- txtFind5 = Me.ComboBox5.Text
- lvwCount = Me.ListView1.ListItems.Count
- If lvwCount = 0 Then Exit Sub
- For r = lvwCount To 1 Step -1
- If txtFind1 = Me.ListView1.ListItems(r).SubItems(1) And _
- txtFind2 = Me.ListView1.ListItems(r).SubItems(2) And _
- txtFind5 = Me.ListView1.ListItems(r).SubItems(4) Then
- Else
- Me.ListView1.ListItems.Remove (r)
- End If
- Next
- lvwCount = Me.ListView1.ListItems.Count
- If lvwCount = 0 Then MsgBox "未找到匹配記錄!", vbInformation, "友情提示"
- End If
- Errhander:
- If Me.CheckBox2.Value = True Then
- MsgBox "未找到匹配記錄!" & Chr(13) & "請檢查輸入的日期範圍是否存在!", vbInformation, "友情提示"
- End If
- Err.Clear
- End Sub
- Private Sub Label5_Click()
- Me.Calendar1.Visible = True
- End Sub
- Private Sub Label6_Click()
- Me.Calendar2.Visible = True
- End Sub
- Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
- Dim cnn As ADODB.Connection
- Dim rst As ADODB.Recordset
- Dim sql, i
- Set cnn = New ADODB.Connection
- Set rst = New ADODB.Recordset
- On Error Resume Next
- With cnn
- .Provider = "Microsoft.Jet.OLEDB.4.0"
- .ConnectionString = "Data source=" & ActiveWorkbook.FullName & ";Extended properties= Excel 8.0"
- .CursorLocation = adUseClient
- .Open
- End With
- Me.ListView1.ListItems.Clear
- If Node.Children = 0 Then
- sql = "select * from [材料表$] where 材料編號 like'" & Node.Text & "'" '
- rst.Open sql, cnn, adOpenKeyset
- If rst.RecordCount = 0 Then MsgBox "未找到匹配記錄!", vbInformation, "友情提示": Exit Sub
- Else
- sql = "select * from [材料表$] where 材料編號 like'" & Node.Text & "%'" '如某某字段內,包含“***”,則用 LIKE ‘%***%’,使用兩個百分號(%)
- rst.Open sql, cnn, adOpenKeyset
- End If
- For i = 1 To rst.RecordCount
- With Me.ListView1.ListItems.Add()
- .Text = rst.Fields(0)
- .SubItems(1) = rst.Fields(1)
- .SubItems(2) = rst.Fields(2)
- .SubItems(3) = rst.Fields(3)
- .SubItems(4) = rst.Fields(4)
- .SubItems(5) = rst.Fields(5)
- .SubItems(6) = rst.Fields(6)
- .SubItems(7) = rst.Fields(7)
- End With
- Err.Clear
- rst.MoveNext
- Next
- Set cnn = Nothing
- Set rst = Nothing
- End Sub
- Private Sub UserForm_Initialize()
- Dim introw&, intcol&, i&, r&, n&
- Dim str$, str1$
- Dim tvwArr
- On Error Resume Next
- Me.TreeView1.Style = tvwTreelinesPlusMinusPictureText
- Me.TreeView1.LineStyle = tvwRootLines
- Me.TreeView1.CheckBoxes = False
- Me.TreeView1.Nodes.Clear
- Me.TreeView1.Nodes.Add , , "top", "產品名稱"
- introw = Sheet1.UsedRange.Rows.Count
- intcol = Sheet1.UsedRange.Columns.Count
- tvwArr = Sheet1.UsedRange
- For r = 1 To intcol
- For i = 2 To introw
- If Not IsEmpty(tvwArr(i, r)) Then
- If r = 1 Then
- Me.TreeView1.Nodes.Add "top", tvwChild, tvwArr(i, r), tvwArr(i, r)
- ElseIf Not IsEmpty(tvwArr(i, r - 1)) Then
- Me.TreeView1.Nodes.Add tvwArr(i, r - 1), tvwChild, tvwArr(i, r), tvwArr(i, r)
- Else
- Me.TreeView1.Nodes.Add CStr(Sheet1.Cells(i, r - 1).End(xlUp)), tvwChild, tvwArr(i, r), tvwArr(i, r)
- End If
- End If
- Next
- Next
- Me.ListView1.View = lvwReport
- Me.ListView1.FullRowSelect = True
- Me.ListView1.Gridlines = True
- Me.ListView1.FlatScrollBar = False
- Me.ListView1.ColumnHeaders.Clear
- For i = 1 To 8
- Me.ListView1.ColumnHeaders.Add , , Sheet3.Cells(1, i)
- Next
- Me.Calendar1.Visible = False
- Me.Calendar2.Visible = False
- Me.CheckBox1.Value = False
- Me.CheckBox2.Value = True
- Me.ComboBox1.Enabled = False
- Me.ComboBox2.Enabled = False
- Me.ComboBox5.Enabled = False
- Me.Label2.Enabled = False
- Me.Label4.Enabled = False
- Me.Label13.Enabled = False
- End Sub
詳細請見附件Treeview.jpg
TreeView.rar |