(查错)请版主来帮个忙
sql语句:Private Sub Command1_Click()
'创建Connection对象cnn,关键New用于创建新对象
Dim cnn As New ADODB.Connection
'创建Recordset对象rs1
Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
'设置连接字符串ConnectionString属性
cnn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=jchisn;Data Source=JXJXYY"
'打开到数据库的连接
cnn.Open
Dim sql As String
Dim sum As String
sql = "select offname as 科室,hoscode as 住院号,patname as 病人姓名,medname As 药品名称, medspec As 规格, roomunit As 单位, roomprice As 单价, number As 数量, selfallmoney As 金额, sendopecode As 发药人, exectime As 发药时间 from AdviceListView where roomcode=2000 and sendopecode>0 " & _
"and hoscode like '" & Text1.Text & "%" & "' " & _
" and offname like'" & Combo1.Text & "%" & "' " & _
" and medname like'" & Text2.Text & "%" & "' " & _
" order by exectime desc"
sum = "select sum(number) As 数量, sum(selfallmoney) As 金额 from AdviceListView where roomcode=2000 and hoscode like '" _
& Text1.Text & "%" & "' "
'判断连接的状态
If cnn.State = adStateOpen Then
Set rs1.ActiveConnection = cnn
rs1.CursorType = adOpenStatic
rs1.Open sql
Set rs2.ActiveConnection = cnn
rs2.CursorType = adOpenStatic
rs2.Open sum
'打开记录集,将从表Users中读取的结果集保存到记录集Adodc1中
Adodc1.ConnectionString = cnn
Adodc1.RecordSource = sql
Adodc1.Refresh
'打开记录集,将从表Users中读取的结果集保存到记录集Adodc2中
Adodc2.ConnectionString = cnn
Adodc2.RecordSource = sum
Adodc2.Refresh
'显示记录集中记录的数据
If rs2.Fields(0) <> "" Then
Label1.Caption = rs2.Fields(0)
Else
Label1.Caption = ""
End If
If rs2.Fields(1) <> "" Then
Label2.Caption = rs2.Fields(1)
Else
Label2.Caption = ""
End If
'关闭记录集
rs1.Close
rs2.Close
'关闭cnn对象
cnn.Close
End If
End Sub
以前我用Adodc绑定时,sql = "select offname as 科室,hoscode as 住院号,patname as 病人姓名,medname As 药品名称, medspec As 规格, roomunit As 单位, roomprice As 单价, number As 数量, selfallmoney As 金额, sendopecode As 发药人, exectime As 发药时间 from AdviceListView where roomcode=2000 and sendopecode>0 " & _
"and hoscode like '" & Text1.Text & "%" & "' " & _
" and offname like'" & Combo1.Text & "%" & "' " & _
" and medname like'" & Text2.Text & "%" & "' " & _
" order by exectime desc"
运行可以,现用字符串连接数据库,只有
sql = "select offname as 科室,hoscode as 住院号,patname as 病人姓名,medname As 药品名称, medspec As 规格, roomunit As 单位, roomprice As 单价, number As 数量, selfallmoney As 金额, sendopecode As 发药人, exectime As 发药时间 from AdviceListView where roomcode=2000 and sendopecode>0 " & _
"and hoscode like '" & Text1.Text & "%" & "' " & _
" order by exectime desc"
时可以查询到数据,加上 " and offname like'" & Combo1.Text & "%" & "' " & _
" and medname like'" & Text2.Text & "%" & "' " & _
后,查询不到数据,也没有报错,当三个条件同时满足时才行,为什么不可单个查询。
在查询分析器上运行可以,请版主帮我看一下。