|
本帖最后由 335081548 于 2013-10-28 21:26 编辑
- Private Sub CommandButton2_Click()
- ListView1.ListItems.Clear
- Dim Conn As Object, Rst As Object
- Dim strConn As String, strSQL As String
- Dim i As Integer, PathStr As String, arr
- Set Conn = CreateObject("ADODB.Connection"): Set Rst = CreateObject("ADODB.Recordset")
- PathStr = ThisWorkbook.FullName '设置工作簿的完整路径和名称
- Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
- Conn.Open strConn '打开数据库链接
- '设置SQL查询语句
- strSQL = "Select 发生日期,证券代码,证券名称,委托方向 from [Sheet1$] where 发生日期>=#" & ComboBox1.Text & "# And 发生日期<= #" & ComboBox2.Text & "# And 证券名称='" & ComboBox3.Text & "'"
- Set Rst = Conn.Execute(strSQL) '执行查询,并将结果输出到记录集对象
- arr = Rst.getrows
- arr = Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Transpose(arr))
- Rst.Close: Conn.Close '关闭数据库连接
- Set Conn = Nothing: Set Rst = Nothing '释放变量
- If UBound(arr) < 1 Then Exit Sub
- With ListView1
- For i = 1 To UBound(arr, 2)
- Set itm = .ListItems.Add()
- itm.Text = arr(1, i)
- itm.SubItems(1) = arr(2, i)
- itm.SubItems(2) = arr(3, i)
- itm.SubItems(3) = arr(4, i)
- Next
- End With
- End Sub
- Private Sub UserForm_Initialize()
- Dim r As Integer, c As Integer
- With ListView1
- .ColumnHeaders.Add , , "发生日期", 100, 0
- .ColumnHeaders.Add , , "证券代码", 100, 1
- .ColumnHeaders.Add , , "证券名称", 100, 1
- .ColumnHeaders.Add , , "委托方向", 100, 1
- .View = lvwReport
- .Gridlines = True
- End With
- End Sub
复制代码
|
|