【问题标题】:Using checkboxes values in SQL "IN"-operator在 SQL“IN”运算符中使用复选框值
【发布时间】:2016-12-06 05:59:57
【问题描述】:

我有一个表格,里面有几个复选框。在 SQL 查询(包括 Excel 宏)中使用勾选复选框的值。我在 SQL“IN”运算符中使用这些值。所以,一切都有效。但我不喜欢我的宏的代码。

对于勾选复选框,我使用这样的代码(如果有更多的价值,列表会非常大):

Public Location1 As String
Public Location2 As String
Public Location3 As String
Public Location4 As String

Private Sub OKCommandButton2_Click()
If CheckBox1.Value = True Then Location1 = "LocationValue1"
If CheckBox2.Value = True Then Location2 = "LocationValue2"
If CheckBox3.Value = True Then Location3 = "LocationValue3"
If CheckBox4.Value = True Then Location4 = "LocationValue4"
...

为了在 SQl 中使用它,我使用这样的代码:

query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 IN ('" & _
LocationDefinition.Location1 & "','" & LocationDefinition.Location2 & "','" & LocationDefinition.Location3 & "','" & _
LocationDefinition.Location4 & "')" & _
"ORDER BY Param2, Param3"

问题是:我能否以更紧凑、简洁和复杂的方式重写我的代码?也许我应该在 SQL 部分使用另一个运算符;也许我可以重写我的 VBA 部分,只使用 SQl 中的一个参数。

谢谢。

【问题讨论】:

  • 什么是LocationDefinition
  • 这是一个带有注释复选框的表单名称。
  • 我猜到了。看我的回答

标签: vba excel checkbox


【解决方案1】:

您可以使用“控件”功能并将您的值写入复选框中的“标签”

Dim TB As Control
Dim ChkBoxString As String
ChkBoxString = "("

For Each TB In Me.Controls    
    If TypeOf TB Is CheckBox Then
        If TB.Value = True Then
            ChkBoxString = ChkBoxString & TB.Tag & ", "
        End If
    End If
Next TB
ChkBoxString = ChkBoxString & ")"
ChkBoxString = Replace(ChkBoxString, ", )", ")")

所以你可以使用你的脚本:

  query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
"WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 " _
IN " & ChkBoxString 

向拉尔夫问好

【讨论】:

  • 感谢您的解决方案。宏适用于您的示例中的一些添加。 1)我在必要的地方添加了一个符号“'”。 2)但最大的问题是“If TypeOf Is CheckBox” - 条件。这种结构只有在我输入“MSForms”时才有效。在“复选框”之前。
【解决方案2】:

创建返回以逗号分隔的字符串表达式的函数,并在复选框的 Tag 属性中设置值

Function GetExpression() As String
Dim contr As Control
Dim comma  As String
Dim str As String
str = ""
For Each contr In UserForm1.Controls
  comma = IIf(str <> "", ",", "")
  If TypeName(contr) = "CheckBox" Then
    If contr.Value = True Then str = str + comma + contr.Tag
  End If
Next
GetExpression = str
End Function

【讨论】:

    【解决方案3】:

    如果在用户窗体仍在加载时调用“进行”查询(或构建查询字符串)的子程序,那么您可以编写如下代码:

    Option Explicit
    
    Dim Locations As String '<--| UserForm scoped variable: all subs in the userform code pane can "see" it
    
    Private Sub OKCommandButton2_Click()
        Dim ctrl As Control
    
        For Each ctrl In Me.Controls '<--| loop through userform controls
            If TypeName(ctrl) = "CheckBox" Then '<--| consider only checkboxes
                If ctrl.value Then Locations = Locations & "LocationValue" & Mid(ctrl.Name, 9, Len(ctrl.Name) - 8) & "','" '<--| if checkbox is checked then update 'Location' string
            End If
        Next ctrl    
    End Sub
    
    Private Sub QuerySub() '<-- name of any Sub inside Userfom code pane that has to make the query
        Dim Query As String
    
        If Locations <> "" Then '<--| this Sub can "see" 'Locations' even if it has been initialized in a different Sub of the same Userform code pane
            Query = "SELECT Param1, Param2, Param3, Param4, 0, 0, Param5, 0 FROM Table1 " & _
            "WHERE Param1 like'" & "%" & CraftDefinition.Craft & "%" & "'AND Param6>0 AND Param2 IN ('" & _
            Left(Locations, Len(Locations) - 3) & "')" & _
            "ORDER BY Param2, Param3"
        Else
            ' code to handle 'Locations' empty string
        End If
    End Sub
    

    【讨论】:

      【解决方案4】:

      这似乎有点错误,因为您在未选中时会得到IN ('', '', '', ''),但如果您不介意,那么可能是这样的:

      locations$ = Mid$( _
          IIf(CheckBox1, "','LocationValue1", "") & _
          IIf(CheckBox2, "','LocationValue2", "") & _
          IIf(CheckBox3, "','LocationValue3", "") & _
          IIf(CheckBox4, "','LocationValue4", ""), 4) ' works even is all unchecked
      
      query = " ... AND Param2 IN ('" & locations & "') ORDER BY Param2, Param3"
      

      或者如果所有值都以“LocationValue”开头,那么

      Locations$ = Mid$(Replace(IIf(CheckBox1, ",1", "") & _
          IIf(CheckBox2, ",2", "") & IIf(CheckBox3, ",3", "") & _
          IIf(CheckBox4, ",4", ""), ",", "','LocationValue"), 4) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-09
        • 2020-06-13
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多