|

楼主 |
发表于 2013-5-23 13:32
|
显示全部楼层
hwc2ycy 发表于 2013-5-23 11:50 
具体的字段值可通过fields(index).value来获取。
好像还是有错误。recordcount返回-1
这是我的整个代码
'On Error Resume Next '如果出现错误,忽略,然后执行下一行代码。
Application.ScreenUpdating = False '关闭屏幕刷新,成对出现,提高速度
Application.DisplayAlerts = False '关闭提示,,成对出现,避免出现提示框
CommandButton9.Enabled = True
CommandButton10.Enabled = True
CommandButton7.Enabled = True
'---------清空原有内容---------
TextBox10.Text = ""
TBgg.Text = ""
ComboBox2.Value = ""
'-----必须输入条件一-----
If TextBox6.Text = "" Or TextBox6.Text = "条件一" Then MsgBox ("请输入条件一"): Exit Sub '必须输入条件一
Select Case ComboBox4.Value
Case Is = "与"
lk4 = "AND"
Case Is = "或"
lk4 = "OR"
End Select
Select Case ComboBox5.Value
Case Is = "与"
lk5 = "AND"
Case Is = "或"
lk5 = "OR"
End Select
Select Case ComboBox6.Value
Case Is = "与"
lk6 = "AND"
Case Is = "或"
lk6 = "OR"
End Select
'--------------------------------------- 参数声明部分 ------------------- ------------------- -------------------
Dim cnn, SQL$ '定义数据库连接和SQL语句
Set cnn = CreateObject("adodb.connection") '创建数据库连接
Set rs = CreateObject("adodb.recordset") '创建一个数据集保存数据
'--------------------------------------- 设置数据库连接 ------------------- ------------------- -------------------
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
'--------------------------------------- 设置SQL语句 ------------------- ------------------- -------------------
SQL = "SELECT * FROM [mktnews$] WHERE mycontent LIKE '%" & TextBox6.Text & "%' " & vbCrLf
SQL = SQL & "AND mydate BETWEEN #" & DTPicker2.Value & "# AND #" & DTPicker3.Value & "# " & vbCrLf
If ComboBox2.Text <> "" Then
SQL = SQL & "AND mylevel = '" & ComboBox2.Text & "'" & vbCrLf
End If
If CheckBox1.Value = True Then
SQL = SQL & "AND myref <> " & "'" & "'" & vbCrLf
End If
If CheckBox2.Value = True Then
SQL = SQL & "AND hlink <> " & "'" & "'" & vbCrLf
End If
If TBgg.Text <> "" And TBgg.Text <> "关联个股" Then
SQL = SQL & "AND mystock LIKE '%" & TBgg.Text & "%' " & vbCrLf
End If
If TBfz.Text <> "" And TBfz.Text <> "非值条件" Then
SQL = SQL & "AND mycontent NOT LIKE '%" & TBfz.Text & "%' " & vbCrLf
End If
If TextBox7.Text <> "" Or TextBox7.Text <> "条件二" Then
SQL = SQL & lk4 & " mycontent LIKE '%" & TextBox7.Text & "%' " & vbCrLf
End If
If TextBox8.Text <> "" Or TextBox8.Text <> "条件三" Then
SQL = SQL & lk5 & " mycontent LIKE '%" & TextBox8.Text & "%' " & vbCrLf
End If
If TextBox9.Text <> "" Or TextBox7.Text <> "条件四" Then
SQL = SQL & lk6 & " mycontent LIKE '%" & TextBox9.Text & "%' " & vbCrLf
End If
'--------------------------------------- SQL结果处理 ------------------- ------------------- -------------------
Set rs = cnn.Execute(SQL)
rs.Open SqlStr, Conn, cursortype, LockType
rs.cursortype
With Worksheets(3)
.Cells.ClearContents '清理保存数据的区域
For i = 1 To rs.Fields.Count '获得SQL结果的列标题
.Cells(1, i) = rs.Fields(i - 1).Name '字段名,rs.Fields(i).name可以得到指定列的列名,I是从0开始计数的,第一列的I=0。
Next
MsgBox rs.RecordCount
rs.movenext
End With
cnn.Close '关闭数据库连接
Set cnn = Nothing '将CNN从内存中删除
|
|