其实不用多条一条查询语句就够了,就是采用判断你输入的条件有几个,动态的位sql 语句设置查询的参数。就像楼上说的,用if判断,如过你传递过来的参数有两个就把那两个设置为sql语句的查询值。
dim objSql As New StringBuilder
objSql.Append(vbCrLf & "SELECT ")
objSql.Append(vbCrLf & "A, ") '
objSql.Append(vbCrLf & "B, ") '
objSql.Append(vbCrLf & "FROM ")
objSql.Append(vbCrLf & TAVLE001")
objSql.Append(vbCrLf & "WHERE ")
‘动态添加查询的值 ,
If frm001.text <> "" Then
objSql.Append(vbCrLf & " C = '" & frm001.text & "' ")
End If
If frm002.text <> "________" Then
objSql.Append(vbCrLf & "AND D = '" & frm002.text & "' ")
End If
'......
dim sql as String = objSql.ToString
还有一种方法就是事物。不过不推荐查询时使用。
Dim flag As Integer
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String
Dim sql4 As String
Dim sql As String
sql = ""
sql1 = ""
sql2 = "
sql3 = ""
sql4 = ""
sql5 = ""
On Error GoTo UpdateErr:
flag = con.BeginTrans
执行多条sql语句。
con.Execute sql
' con.Execute sql2
' con.Execute sql3
' con.Execute sql4
' con.Execute sql5
con.CommitTrans
flag = 0
MsgBox "UpdateSuccessful !"
Exit Function
UpdateErr: If flag = 1 Then con.RollbackTrans
MsgBox "Update Error !"
错误原因:
控件没有加载数据源,在Adodc1的属性中进行加载
语句精简:
If Check1.Value = 1 Then '歌曲名查询
DBstr = "select * from audioinfo where 歌曲名 like '%" & txt歌曲名.Text & "'"
elseIf Check2.Value = 1 Then '歌手名查询
DBstr = "select * from DBstr1 where 歌手名 like '%" & txt歌手名.Text & "'"
elseIf Check3.Value = 1 Then '专辑名查询
DBstr = "select * from DBstr2 where 专辑名 like '%" & txt专辑名.Text & "'"
Else
DBstr = "select * from audioinfo"
End If
用FIND和FILTER的效率都很低,我以前作过一个查找,就干脆
N=RS.RECORDCOUNT;
FOR I=0 TO N DO
IF XXX=RS.FIELD("XXX") THEN
....
ELSE
RS.MOVENEXT;
...
END IF
经过测试,效率反而高些,但RS的字段要少...