MS Access VBA循环列表

时间:2014-03-03 14:37:29

标签: vba ms-access

我认为这应该很简单,但我找不到正确的方法。我有几个我想解决的情况的例子,所以我将描述两者。我认为他们可能会使用相同的解决方案处理,但如果没有,请告诉我。我正在尝试循环遍历值列表,并为每个值运行SQL查询。在一种情况下,值是文本,而在另一种情况下,它们是非连续的整数。

编辑添加:我应该说,值列表是动态的,它来自组合框中选择的ID值列表。现在,这些存储在我名为item_in_my_text_list和item_in_my_ID_list的变量中的逗号分隔列表中。

这样的事情:

item_in_my_text_list = "User-defined, mean, upper-bound"
For Each item_in_my_text_list(i)
    'run a query where i is part of the condition
    'I want to run the same query first the user-defined metric, then for the mean, then for the upper-bound
    'SQL should be "select metric, age, value into scenario where metric = 'user-defined'"
          SQL= "Select metric, age, value  into scenario WHERE metric = [i]
      DoCmd.SetWarnings False

          DoCmd.RunSQL SQL

          DoCmd.SetWarnings True

          SQL = ""
          rs.MoveNext

Loop

另一种情况涉及ID值的非连续数字列表:

item_in_my_ID_list = "7, 9, 15, 88"
For Each item_in_my_ID_list(i)
    'run a query where i is part of the condition
    'I want to run the same query first for 7, then for 9, then 15, and so on
          SQL= "Update thistable set x = y where ID = " & [i]
      DoCmd.SetWarnings False

          DoCmd.RunSQL SQL

          DoCmd.SetWarnings True

          SQL = ""
          rs.MoveNext

Loop

设置循环的正确语法是什么?当我尝试使用Split时,似乎它给了我列表中项目的计数,所以当我尝试使用[i]作为查询条件的一部分时,我得到,1,2,3(取决于<列表中的项目数),而不是“用户定义的”或正确的ID值。

编辑添加我的解决方案。来自@mehow的建议给了我完成这项工作所需要的东西。

Set db = CurrentDb
'Delete records for algorithms that aren't selected

    Dim DeleteSQL As String
    DeleteSQL = "Delete from Scenario WHERE ID not in (" & Me.ListSelect & ")"

    db.Execute DeleteSQL, dbFailOnError

    DeleteSQL = ""

'for each of the IDs selected, copy the whole scenario
Dim i As Long
For i = 0 To UBound(Split(Me.ListSelect, ","))
    'find the records from the scenario table, and copy them for the "upper bound"
    SQL = "insert into scenario (a, b, c)"
    SQL = SQL & " SELECT a, b, c
    SQL = SQL & " FROM Scenario "
    SQL = SQL & " WHERE Scenario.Id= " & Trim(Split(Me.ListSelect, ",")(i)) 

    db.Execute SQL, dbFailOnError
    SQL = ""

    'reset user-defined values for both mean and upper bound
    Dim allMetrics As String
    allMetrics = "Central tendency, Upper bounding"

    Dim thisMetric As Long
    For thisMetric = 0 To UBound(Split(allMetrics, ","))
         Dim rs As DAO.Recordset
         Set rs = CurrentDb.OpenRecordset("SELECT Parameter FROM AllParameters WHERE Id= " & Trim(Split(Me.ListSelect, ",")(i)))

         Dim DefaultSQL As String
         If Not (rs.EOF And rs.BOF) Then
             Do Until rs.EOF = True
                DefaultSQL = "UPDATE defaults LEFT JOIN scenario ON defaults.Age = Scenario.Age SET Scenario." & rs("Parameter") & " = [defaults].[Value] "
                DefaultSQL = DefaultSQL & " WHERE defaults.ID =" & Trim(Split(Me.ListSelect, ",")(i))
                DefaultSQL = DefaultSQL & " And defaults.metric = '" & Trim(Split(allMetrics, ",")(thisMetric)) & "'"


                db.Execute DefaultSQL, dbFailOnError
                DefaultSQL = ""
                rs.MoveNext
             Loop

         End If

         rs.Close
         Set rs = Nothing
    Next
Next

3 个答案:

答案 0 :(得分:4)

For Each i In VBA.Split(item_in_my_ID_list, ",")是一种方式。 i是字符串类型。

您可能必须使用VBA.Trim(i)来填充逗号之间的空格。

答案 1 :(得分:3)

我认为以下是您可能正在寻找的语法

Sub Main()

    Dim myList As String
    myList = "7, 9, 15, 88"

    Dim i As Long
    For i = 0 To UBound(Split(myList, ","))
        SQL = "Select metric, age, value  into scenario " & _
              "WHERE metric = " & Trim(Split(myList, ",")(i))
        ' the rest of your code...
    Next

End Sub

答案 2 :(得分:1)

你需要使用一个数组然后循环它。

Dim str(2) As String
Dim i as Integer

str = Array("one", "two", "three")

For i = 0 To UBound(str)
    ...str(i)


Next