在SQL“IN”操作符中使用checkbox值

我有一个表单中有几个checkbox。 在包括Excelmacros的SQL查询中使用勾选checkbox的值。 我在SQL“IN”-operator中使用这些值。 所以,everythig工作。 但是我不喜欢我的macros的代码。

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

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中只使用一个参数。

谢谢。

您可以使用“控件”function并将您的值从checkbox写入“标记”

 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 

问候拉尔夫

创build返回带逗号分隔stringexpression式的函数,并在checkbox的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 

如果在用户窗体仍然被加载的时候调用了“制作”查询(或者构build查询string)的子程序,那么你可以编写如下代码:

 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 

这看起来有点不对劲,因为当你取消选中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)