我有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,则计算组合,那么编写组合将是非常困难的....
任何其他替代方法可用...?
自己的代码,我相信世界上的任何编程语言都有一个名为'array'的数据结构。 – 0605002 2012-02-01 06:56:48
另外,像这样构建sql字符串是一个非常糟糕的主意**。你的代码很容易受到SQL注入攻击。 – 2012-02-01 14:30:22