vb里用SQL的多条件查询,请教
有七个控件,分别对应SQL表里七个字段,想查询其中不为空的若干个,在VB里,SQL语句该怎么写呢请教大侠们...
有七个控件,分别对应SQL表里七个字段,想查询其中不为空的若干个,在VB里,SQL语句该怎么写呢
请教大侠们 展开
请教大侠们 展开
1个回答
展开全部
什么意思 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"
追答
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询