“For Loop”的替代方法

时间:2012-02-01 06:48:35

标签: vb6 for-loop

我有4个不同的值,我想用4种不同的组合检查4个值

代码

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,那​​么编写组合将非常困难....

还有其他替代方法......?

1 个答案:

答案 0 :(得分:1)

我认为你没有提到你的数据的确切结构,否则会有一个更好的代码示例,但是,只需稍加修改即可尝试自己的代码

    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