2016-08-01 16 views
1

我有一个表单中有几个复选框。勾选复选框的值在包含Excel宏的SQL查询中使用。我在SQL“IN” - 操作符中使用这些值。所以,everythig工作。但我不喜欢我的宏的代码。使用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部分中使用另一个运算符;也许我可以重写我的VBA部分,因为在SQl中只使用一个参数。

谢谢。

+0

什么是'LocationDefinition'? – user3598756

+0

这是一个带有注释复选框的表单名称。 – Mikhail

+0

我猜对了。看到我的回答 – user3598756

回答

0

您可以使用“控制”功能,并从复选框写你的价值进入“TAG”

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 

电贺拉尔夫

+0

感谢您的解决方案。 宏在您的示例中使用了一些添加。 1)我在必要的地方添加了符号“'”。 2)但最大的问题是“If TypeOf Is CheckBox” - 条件。只有我把“MSForm”放在这个构造才有效。之前“CheckBox”。 – Mikhail

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 
0

如果子说,“让”查询(或建立查询字符串)被调用,而用户窗体仍然加载,那么你可以编写如下:

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 
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)