各位朋友,请看下面代码?(为什么不能实现真正的复合多条件查询?)此问题十分着急……
Private Function prvWhereSQL() As String
  Dim strResult As String  If Check1.Value Then
    If Trim(strResult) = "" Then
       strResult = " (子公司名='" & Combo5.Text & "') order by 日期"
    Else
       strResult = strResult & " AND (子公司名='" & Combo5.Text & "')"
    End If
  End If
  '其它条件类似
  If Check2.Value Then
    If Trim(strResult) = "" Then
        strResult = "(客户名称='" & Combo4.Text & "') order by 日期"
    Else
        strResult = strResult & " AND (客户名称='" & Combo4.Text & "')"
    End If
  End If
 If Check3.Value Then
    If Trim(strResult) = "" Then
        strResult = "(账户名='" & Combo6.Text & "') order by 日期"
    Else
        strResult = strResult & " AND (账户名='" & Combo6.Text & "')"
    End If
 End If
  If Check4.Value Then
    If Trim(strResult) = "" Then
        strResult = "(科目名称='" & Combo8.Text & "')order by 日期"
    Else
        strResult = strResult & " AND (科目名称='" & Combo8.Text & "')"
    End If
 End If
  If Check5.Value Then
   If Trim(strResult) = "" Then
        strResult = "金额 between  " & Val(Text1.Text) & " And  " & Val(Text3.Text)
    Else
        strResult = strResult & " AND 金额 between " & Val(Text1.Text) & " And  " & Val(Text3.Text)
    End If
 End If
  If Check6.Value Then
    If Trim(strResult) = "" Then
        strResult = "(经手人='" & Combo7.Text & "') order by 日期"
    Else
        strResult = strResult & " AND (经手人='" & Combo7.Text & "')"
    End If
 End If
  If Check7.Value Then
    If Trim(strResult) = "" Then
        strResult = "(备注 like '%" + Text2.Text + "%' or 科目名称 like '%" + Text2.Text + "%')"
    Else
        strResult = strResult & " AND (备注 like '%" + Text2.Text + "%' or 科目名称 like '%" + Text2.Text + "%')"
    End If
 End If
  If Check8.Value Then
    If Trim(strResult) = "" Then
        strResult = "(日期 between # " & DTPicker1.Value & "#" & " and " & "#" & DTPicker2.Value & "# ) order by 日期"
    Else
        strResult = strResult & " AND (日期 between # " & DTPicker1.Value & "#" & " and " & "#" & DTPicker2.Value & "# )"
    End If
 End If  If Trim(strResult) = "" Then
     prvWhereSQL = " "
  Else
     prvWhereSQL = " WHERE " & strResult
  End If
End Function

解决方案 »

  1.   

    是order by 的问题
    应该在所有的判断完成后,再在strResult后加order by
      

  2.   

    把 Order By 日期  放在组织的SQL语句最后
      

  3.   

    你把最后形成的SQL语句写到查询分析上调试,这样比较容易看出错误所在
      

  4.   

    把order by 日期放在这儿可以吗?但是这样写不行,不知要如何才好
    If Trim(strResult) = "" Then
         prvWhereSQL = " "
      Else
        strResult = strResult & "order by 日期"
         prvWhereSQL = " WHERE " & strResult
      End If
    End Function
      

  5.   

    在调用时我的sql是这样写的:
    sql = "select 子公司名,科目名称,客户名称,金额,支票,账户名,经手人,日期,备注,进出账 from 资料管理 " & prvWhereSQL
    那么,我应该把order by 日期 放在哪儿呢?先谢谢各位了
      

  6.   

    为什么不行?你把组织好的SQL语句贴出来看看
      

  7.   

    组织好应该是这种格式sql = "select 子公司名,科目名称,客户名称,金额,支票,账户名,经手人,日期,备注,进出账 from 资料管理 " & prvWhereSQL
    sql=sql & order by 日期
      

  8.   

    阿九你好,非常感谢你的帮助,当然还有楼上几位好朋友
    在我这几天的发贴中,每次几乎都有阿九的帮助,非常希望能和你进一步请教,我的QQ是6492323,如果可能的话,希望能得到你的QQ号,谢谢
      

  9.   

    将查询分两部分..一部分是 WHERESTR,  一部分是 ORDERSTR
    取最后再组合即可.
      

  10.   

    nifeng2008(山水) 你好!我这端口被封MSN和QQ都上不了:(只能上CSDN等少数几个网站
      

  11.   

    帮你改了一下.
    Private Function prvWhereSQL() As String
      Dim strResult As String  If Check1.Value = 1 Then
        If Trim(strResult) = "" Then
           strResult = " (子公司名='" & Combo5.Text & "') "
        Else
           strResult = strResult & " AND (子公司名='" & Combo5.Text & "')"
        End If
      End If
      '其它条件类似
      If Check2.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(客户名称='" & Combo4.Text & "') "
        Else
            strResult = strResult & " AND (客户名称='" & Combo4.Text & "')"
        End If
      End If
     If Check3.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(账户名='" & Combo6.Text & "') "
        Else
            strResult = strResult & " AND (账户名='" & Combo6.Text & "')"
        End If
     End If
      If Check4.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(科目名称='" & Combo8.Text & "') "
        Else
            strResult = strResult & " AND (科目名称='" & Combo8.Text & "')"
        End If
     End If
      If Check5.Value = 1 Then
       If Trim(strResult) = "" Then
            strResult = "金额 between  " & Val(Text1.Text) & " And  " & Val(Text3.Text)
        Else
            strResult = strResult & " AND 金额 between " & Val(Text1.Text) & " And  " & Val(Text3.Text)
        End If
     End If
      If Check6.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(经手人='" & Combo7.Text & "') "
        Else
            strResult = strResult & " AND (经手人='" & Combo7.Text & "')"
        End If
     End If
      If Check7.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(备注 like '%" + Text2.Text + "%' or 科目名称 like '%" + Text2.Text + "%')"
        Else
            strResult = strResult & " AND (备注 like '%" + Text2.Text + "%' or 科目名称 like '%" + Text2.Text + "%')"
        End If
     End If
      If Check8.Value = 1 Then
        If Trim(strResult) = "" Then
            strResult = "(日期 between # " & DTPicker1.Value & "#" & " and " & "#" & DTPicker2.Value & "# ) "
        Else
            strResult = strResult & " AND (日期 between # " & DTPicker1.Value & "#" & " and " & "#" & DTPicker2.Value & "# )"
        End If
     End If  If Trim(strResult) = "" Then
         prvWhereSQL = "  ORDER BY 日期"
      Else
         prvWhereSQL = " WHERE " & strResult & " ORDER BY 日期"
      End If
      
    End Function