怎么点Cmd 都没反映DataGrid1.ClearFields
dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\testpaper\DB\testpaper.mdb" connstr = "select * from testdes"
tempsql = connstrIf Not Combo1(0).Text = "" Then
tempsql = tempsql & " where itemname like '" + Combo1(0).Text + "'"
End IfIf Not Combo1(1).Text = "" Then
If tempsql <> connstr Then
tempsql = tempsql & " and sttype like '" + Combo1(1).Text + "'"
Else
tempsql = tempsql & " where sttype like '" + Combo1(1).Text + "'"
End If
End IfIf Not Combo1(2).Text = "" Then
If tempsql <> sql Then
tempsql = tempsql & " and ledes like '" + Combo1(2).Text + "'"
Else
tempsql = tempsql & " where ledes like '" + Combo1(2).Text + "'"
End If
End IfDebug.Print tempsql
mrc.Open tempsql, dbconn, adOpenStatic, adLockOptimisticIf Not mrc.EOF Then
Debug.Print mrc.Fields(0)
End If
mrc.Close
dbconn.Close
dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=E:\testpaper\DB\testpaper.mdb" connstr = "select * from testdes"
tempsql = connstrIf Not Combo1(0).Text = "" Then
tempsql = tempsql & " where itemname like '" + Combo1(0).Text + "'"
End IfIf Not Combo1(1).Text = "" Then
If tempsql <> connstr Then
tempsql = tempsql & " and sttype like '" + Combo1(1).Text + "'"
Else
tempsql = tempsql & " where sttype like '" + Combo1(1).Text + "'"
End If
End IfIf Not Combo1(2).Text = "" Then
If tempsql <> sql Then
tempsql = tempsql & " and ledes like '" + Combo1(2).Text + "'"
Else
tempsql = tempsql & " where ledes like '" + Combo1(2).Text + "'"
End If
End IfDebug.Print tempsql
mrc.Open tempsql, dbconn, adOpenStatic, adLockOptimisticIf Not mrc.EOF Then
Debug.Print mrc.Fields(0)
End If
mrc.Close
dbconn.Close
tempsql = tempsql & " where ledes like '%" + Combo1(2).Text + "%'"
like '%aa' '查找以aa结束的内容
like '%aa%' '查找包含aa的内容
connstr = "select * from testdes"
tempsql = connstrIf Not Combo1(0).Text = "" Then
tempsql = tempsql & " where itemname like '%" + Combo1(0).Text + "%'"
End IfIf Not Combo1(1).Text = "" Then
If tempsql <> connstr Then
tempsql = tempsql & " and sttype like '%" + Combo1(1).Text + "%'"
Else
tempsql = tempsql & " where sttype like '%" + Combo1(1).Text + "%'"
End If
End IfIf Not Combo1(2).Text = "" Then
If tempsql <> connstr Then
tempsql = tempsql & " and ledes like '%" + Combo1(2).Text + "%'"
Else
tempsql = tempsql & " where ledes like '%" + Combo1(2).Text + "%'"
End If
End IfDebug.Print tempsql
mrc.CursorLocation = adUseClientmrc.Open tempsql, dbconn, adOpenDynamic, adLockBatchOptimisticSet DataGrid1.DataSource = mrcIf Not mrc.EOF Then
Debug.Print mrc.Fields(0)
End If
mrc.Close
End IfIf Not Combo1(1).Text = "" Then
If tempsql <> connstr Then
tempsql = tempsql & " and sttype like '%" + Combo1(1).Text + "%'"
Else
tempsql = tempsql & " where sttype like '%" + Combo1(1).Text + "%'"
End If
End If
这种写法有可能会造成一句SQL里有多个WHERE 所以会查不出结果楼主可以在执行SQL前加个断点
查看一下SQL语句是不是有问题
请将like '%
改成like'% 不留空格看看
connstr = "select * from testdes where 1=1 "
tempsql = connstrIf trim(Combo1(0).Text) <> "" Then
tempsql = tempsql & " and itemname like '%" & trim(Combo1(0).Text) & "%'"
End IfIf trim(Combo1(1).Text) <> "" Then
tempsql = tempsql & " and sttype like '%" & trim(Combo1(1).Text) & "%'"
end If
打开你的数据库,出来左边有个窗体,上面有"表","查询","窗体"等,选择"查询"-->"新建"-->出来的窗体"确定"-->再出来的窗体"关闭",不用理会这里,这时在左上角大窗口那里会出现个"SQL",点开他有个"SQL视图",进入这里就可以把你在程序中的SQL语句放到这里来调试了
'先写个条件占位符where 1=1 再在后面用and连接
connstr = "select * from testdes where 1=1 "
tempsql = connstrIf trim(Combo1(0).Text) <> "" Then
tempsql = tempsql & " and itemname like '*" & trim(Combo1(0).Text) & "*'"
End IfIf trim(Combo1(1).Text) <> "" Then
tempsql = tempsql & " and sttype like '*" & trim(Combo1(1).Text) & "*'"
end If