中易网

vb里用SQL的多条件查询,请教

答案:1  悬赏:70  
解决时间 2021-01-16 02:15
  • 提问者网友:战魂
  • 2021-01-15 01:52
vb里用SQL的多条件查询,请教
最佳答案
  • 二级知识专家网友:千杯敬自由
  • 2021-01-15 02:37
什么意思   7各控件存的是字段名称?  然后要求查询出这7各字段均不为空的所有数据?
假设字段名称存在Text1.Text,Text2.Text,Text3.Text,Text4.Text,Text5.Text,Text6.Text,Text7.Text
Dim sqlStr as String
sqlStr = "Select * from myTable where " & Text1.Text & " is not null and " & _
sqlStr = sqlStr & Text2.Text & " is not null and " & _
sqlStr = sqlStr & Text3.Text & " is not null and " & _
sqlStr = sqlStr & Text4.Text & " is not null and " & _
sqlStr = sqlStr & Text5.Text & " is not null and " & _
sqlStr = sqlStr & Text6.Text & " is not null and " & _
sqlStr = sqlStr & Text7.Text & " is not null"追问这七个控件,分别绑定一个SQL表的七个字段,查询的时候可能只会选择一部分,所以想怎样来实现随机查询不为空的那几个字段,用什么方法好呢,
追答Dim sqlStr as String
sqlStr = "Select * from myTable where 1=1 "
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text1.Text & " is not null"
End If
If Text2.Text<>"" Then
    sqlStr = sqlStr & "and " & Text2.Text & " is not null"
End If
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text3.Text & " is not null"
End If
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text4.Text & " is not null"
End If
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text5.Text & " is not null"
End If
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text6.Text & " is not null"
End If
If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text7.Text & " is not null"
End If 如果同时判断不为null  也不为“”   每个IF块里的语句加上“”的判断


If Text1.Text<>"" Then
    sqlStr = sqlStr & "and " & Text1.Text & " is not null And " & Text1.Text &"<>''"
End If
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息