|
Option Compare Text
'引用Microsoft ActiveX Data Objects 2.x Library
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub ListView1_DblClick()
'On Error Resume Next
ActiveCell.Value = ListView1.SelectedItem
ActiveCell.Offset(0, 1).Value = ListView1.SelectedItem.SubItems(1)
ActiveCell.Offset(0, 2).Value = ListView1.SelectedItem.SubItems(2)
ActiveCell.Offset(0, 3).Value = ListView1.SelectedItem.SubItems(3)
UserForm1.Hide
Me.TextBox1.Value = ""
End Sub
Private Sub TextBox1_Change()
Call Comm
End Sub
Private Sub TextBox2_AfterUpdate()
Call Comm
End Sub
Private Sub TextBox3_AfterUpdate()
Call Comm
End Sub
Private Sub TextBox4_AfterUpdate()
Call Comm
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.SetFocus
Me.TextBox1.Value = ""
ListView1.ColumnHeaders.Clear
ListView1.ListItems.Clear
ListView1.FullRowSelect = True
ListView1.Gridlines = True
With ListView1
.ColumnHeaders.Add , , Sheet1.Cells(1, 7), .Width / 4
.ColumnHeaders.Add , , Sheet1.Cells(1, 8), .Width / 4
.ColumnHeaders.Add , , Sheet1.Cells(1, 9), .Width / 5
.ColumnHeaders.Add , , Sheet1.Cells(1, 10), .Width / 5
.View = lvwReport
End With
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
Call Comm
TextBox1.SetFocus
Me.TextBox1.Value = ""
End Sub
Public Sub Comm()
Dim i&, j&, arr, s$, t$, SQL$
arr = [g1:j1&""]
For i = 1 To 4
t = Me.Controls("TextBox" & i).Text
If Len(t) Then s = s & " and " & arr(i) & " like '%" & t & "%'"
Next
SQL = "select * from [Sheet1$" & [g1].CurrentRegion.Address(0, 0) & "]"
If Len(s) Then SQL = SQL & " where " & Mid(s, 6)
On Error Resume Next
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
With ListView1
.ListItems.Clear
For i = 1 To rs.RecordCount
.ListItems.Add , , rs.Fields(0).Value
For j = 1 To rs.Fields.Count - 1
.ListItems(i).SubItems(j) = rs.Fields(j).Value
Next
rs.MoveNext
Next
End With
rs.MoveFirst
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
On Error Resume Next
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub |
|