界面上有个ComboBox,我想如果ComboBox为空(就是没有设定查询条件)的时候,返回所有记录,否则就按条件查询
-------------------------------------------------------------
Dim AAA As String
If ComboBox.Text = "" Then
AAA = "*"
Else
AAA = ComboBox.Text
End If"select * from 表 where 字段='" & AAA & "'"
-------------------------------------------------------------
这样写的话,有条件查询就没有问题,但是如果条件为空,什么数据都没有返回请指教

解决方案 »

  1.   

    Dim AAA As String
    If ComboBox.Text = "" Then
    "select * from 表
    Else
    "select * from 表 where 字段='" & AAA & "'"
    End If
      

  2.   

    谢谢楼上大侠的热心解答
    这个方法我也想过,但是查询的时候可能还有其他的条件,我省略了比如:where 条件1 and 条件2 and 条件3 这样
      

  3.   

    dim l_strSQl as stringl_strsql="Select * from Tables Where 1=1"If ComboBox.Text <> "" Then
         l_strsql = l_strsql & "And Filed=" ComboBox.Text
    End If如果还有其他条件,继续如上
      

  4.   

    请问一下l_strsql="Select * from Tables Where 1=1"中的1=1怎么理解啊  谢谢
      

  5.   

    "select * from 表 where 字段='" & AAA & "'"这一句有问题,如果条件为空,则这句变成:
    "select * from 表 where 字段='*'"
    当然不会有记录。你应该这样:
    dim sql as string
    sql=你的条件   '在这里插入你的Combo相关的代码if sql="" then
       sql="Select * From 表"
    else
       sql="Select * From 表 Where " & sql
    end if