2012-02-01 152 views
0

我有4个不同的值的另一种方法,我想检查4个值4种不同的组合为“For循环”

代码

Dim tot1, tot2, tot3, to4 As Variant 
Dim item1, item2, item3, item4 As Variant  

    For Each item1 In tot1 
     For Each item2 In tot2 
      For Each item3 In tot3 
       For Each item4 In tot4 
      If item1 = "All" And item2 <> "All" And item3 <> "All" And item4 <> "All" Then '1 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 = "All" And item3 <> "All" And item4 <> "All" Then '2 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 <> "All" And item3 = "All" And item4 <> "All" Then '3 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 <> "All" And item3 <> "All" And item4 = "All" Then '4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 = "All" And item2 = "All" And item3 <> "All" And item4 <> "All" Then '1 & 2 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 = "All" And item2 <> "All" And item3 = "All" And item4 <> "All" Then '1 & 3 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 = "All" And item2 <> "All" And item3 <> "All" And item4 = "All" Then '1 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 = "All" And item3 = "All" And item4 <> "All" Then '2 & 3 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 = "All" And item3 <> "All" And item4 = "All" Then '2 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 <> "All" And item3 = "All" And item4 = "All" Then '3 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 = "All" And item2 = "All" And item3 = "All" And item4 <> "All" Then '1 & 2 & 3 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'" 
       ElseIf item1 = "All" And item2 = "All" And item3 <> "All" And item4 = "All" Then '1 & 2 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 = "All" And item2 <> "All" And item3 = "All" And item4 = "All" Then '1 & 3 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 = "All" And item3 = "All" And item4 = "All" Then '2 & 3 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'" 
       ElseIf item1 <> "All" And item2 <> "All" And item3 <> "All" And item4 <> "All" Then '1 & 2 & 3 & 4 
        sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'" 
       End If 
     Next 
      Next 
     Next 
    Next 

上面提到的代码工作,但代码是很辛苦用于检查组合的4个值,
如果检查值是6,则计算组合,那么编写组合将是非常困难的....

任何其他替代方法可用...?

+3

自己的代码,我相信世界上的任何编程语言都有一个名为'array'的数据结构。 – 0605002 2012-02-01 06:56:48

+0

另外,像这样构建sql字符串是一个非常糟糕的主意**。你的代码很容易受到SQL注入攻击。 – 2012-02-01 14:30:22

回答

1

我想你havnt提到你的数据的确切结构,否则会是一个更好的代码示例,然而,尝试用少量的修改

Dim tot1 As New Collection, tot2 As New Collection, tot3 As New Collection, tot4 As New Collection 
Dim item1, item2, item3, item4 As Variant 
Dim str As String 

tot1.Add "All" 
tot1.Add "2" 
tot1.Add "3" 
tot1.Add "4" 

tot2.Add "1" 
tot2.Add "2" 
tot2.Add "3" 
tot2.Add "All" 
tot2.Add "5" 

tot3.Add "1" 
tot3.Add "All" 
tot3.Add "3" 
tot3.Add "4" 
tot3.Add "5" 
tot3.Add "6" 

tot4.Add "1" 
tot4.Add "All" 
tot4.Add "3" 


For Each item1 In tot1 
    For Each item2 In tot2 
     For Each item3 In tot3 
      For Each item4 In tot4 
       str = vbNullString 
       If item1 = "All" Then str = str & "& 1 " 
       If item2 = "All" Then str = str & "& 2 " 
       If item3 = "All" Then str = str & "& 3 " 
       If item4 = "All" Then str = str & "& 4 " 
       Debug.Print item1 & "," & item2 & "," & item3 & "," & item4 & " -> " & Mid$(Trim$(str), 2) 
      Next 
     Next 
    Next 
Next 
+0

这不是我的实际代码,现在我发布了我的实际代码。我将根据组合的值插入表中...... – JetJack 2012-02-01 07:59:35

+0

如何更改代码以将值插入表中...... – JetJack 2012-02-01 07:59:58

+0

只需在相关的条件下构建SQL子句。您不需要添加“&1”,而是添加“和分区<>'”&item2&“'”或类似内容。 – Deanna 2012-02-01 09:16:17