【问题标题】:Hints to determine which combo boxes were selected?确定选择了哪些组合框的提示?
【发布时间】:2015-10-09 21:35:16
【问题描述】:

我在 Access 数据库中有 8 个组合框。每个组合框可以一个值或没有一个值(2个选项)。总共可以有 256 种组合 (2^8)。我正在尝试在 VBA 中创建一些代码,循环遍历这些组合以确定当前存在的组合,最终目标是基于该组合在 VBA 中编写 SQL 查询。例如,假设combo1 和combo2 都有选择,但combo3 到combo8 没有。如果 的组合,我希望我的 SQL 查询执行 SELECT FROM 查询,其中 db = combo1 中的列和 db = combo2 中的列。任何人都可以提供有关我将如何构建代码的提示吗?

谢谢!

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:
    Dim a as string, b as string 
    const myAND as string = "AND "
    
    a = ""
    a = "SELECT * FROM a table "
    
    b = ""
    if cbo1.value <> "" then
      b = b & myAND & "AND field1 = '" & cbo1.value & "'"
    end if
    
    if cbo2.value <> "" then
      b = b & myAND & "field2 = '" & cbo2.value & "'"
    end if
    
    etc for each cbo box
    
    If b <> "" Then
    
       ' Lazy way 
       ' a = a & "WHERE 1=1 " & b 
    
       ' remove the first AND way
       a = a & "WHERE 1=1 " & mid(b,len(myAND))
    
    End if
    
    ' a now contains the SQL you need.
    

    【讨论】:

    • 这会评估多个组合吗?例如,如果combo1、combo3和combo 7 ""
    • 是的。有点担心你问这个,因为代码很简单。
    • a = a &amp; "WHERE 1=1 " &amp; b这太好了我喜欢这个
    • “WHERE 1=1”是什么意思?
    • @Chris2015 你有很多事情要做。看 yutube 或买书:youtube.com/user/ProgrammingMadeEZ
    【解决方案2】:
    Dim where_condtion as String
    Dim sqlquery as String
    
    where_condtion = ""
    
    IF combo1 <>"" then
        where_condtion = where_condtion + "~fieldname~ = " & combo1
    End IF
    IF combo2 <>"" then
        where_condtion = where_condtion + "AND ~fieldname~ = " & combo2
    End IF
    *
    *
    *
    IF combo8 <>"" then
        where_condtion = where_condtion + "AND ~fieldname~ =" & combo8
    End IF
    
    IF where_condtion <> "" then
        sqlquery = "Select * from ~table name~ where" + where_condtion
    ELSE
        sqlquery = "Select * from ~table name~
    End IF
    
    sqlquery = Replace(sqlquery, "where AND ", "where ")
    
    DoCmd.OpenQuery "sqlquery", acViewNormal, acEdit
    
    OR
    
    CurrentDb.OpenRecordset("sqlquery")
    

    【讨论】:

    • 这会评估多个组合吗?例如,如果 combo1、combo3 和 combo7 ""
    • 是的,它检查所有条件。如果像我为“组合框 2”所做的那样调整剩余的组合框,你必须再写 5 个
    • 好的,我想我明白了。谢谢。
    • 如果 combo1="" 和例如combo2"" - 它将创建一个无效的 WHERE 子句。请参阅 HarveyFrench 的回答中的“WHERE 1=1”技巧。
    • 我已经编辑了我的答案。现在不会出现这个问题了。
    【解决方案3】:

    Am 选项将是一个连接字符串

    代码示例

    Dim strSQL as String
    'basic string
    strSQL = "SELECT tbl.fieldA, tbl.fieldB FROM tbl "
    
    Dim strSQLwhere as String
    strSQLwhere = ""
    'Combobox cmbbox1
    If Not isNull(cmbbox1) And cmbbox1.ListIndex <> -1 then
        strSQLwhere = strSQLwhere & "tbl.fieldToFilter1=" & cmbbox1
    End if
    
    'Combobox cmbbox2
    If Not isNull(cmbbox2) And cmbbox2.ListIndex <> -1 then
        iF NOT strSQLwhere = "" then     
           strSQLwhere = strSQLwhere & " AND "
        end if
        strSQLwhere = strSQLwhere & "tbl.fieldToFilter2=" & cmbbox2
    End if
    
    'And so on until cmbBox 8
    
    'Combine all Strings
    if not strSQLwhere = "" then
        strSQL  = strSQL  & " WHERE (" & strSQLwhere & ")"
    End if
    
    'Add here further thing like ORDER BY, GROUP BY
    'Show SQL sting if it is well fomratted, change string concatenation if not
    debug.print strSQL  
    

    如果您能够在 VBA 中执行此操作,您可以在单独的函数中执行组合框 if-then-(else) 案例。

    【讨论】:

    • 哇,三个相似的答案......很高兴直接看到罗马的其他方式
    猜你喜欢
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2015-02-13
    • 1970-01-01
    相关资源
    最近更新 更多