在SQL" IN" -operator中使用复选框值

时间:2016-08-01 08:20:48

标签: excel vba excel-vba checkbox

我有一个包含多个复选框的表单。勾选复选框的值在SQL查询之后使用,包括Excel-macro。我在SQL" IN" -operator中使用这些值。所以,每一个都有效。但我不喜欢我宏的代码。

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

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-part中使用另一个运算符;也许我可以重写我的VBA部分,因为在SQl中只使用一个参数。

谢谢。

4 个答案:

答案 0 :(得分:0)

您可以使用“控件”功能,并通过复选框

将您的值写入“标签”
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 

Greets Ralf

答案 1 :(得分:0)

创建以逗号分隔返回字符串表达式的函数,并在复选框的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

答案 2 :(得分:0)

如果&#34;生成&#34;在仍然加载Userform时调用查询(或构建查询字符串),然后您可以按如下方式编写代码:

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

答案 3 :(得分:0)

这似乎有点不对,因为你在取消选中时会得到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)