MS Access VBA 循环列表

kvi*_*rtj 2 ms-access vba

我认为这应该很简单,但我找不到正确的方法。我有几个我试图解决的情况的例子,所以我将描述这两个例子。我认为它们可能可以用相同的解决方案来处理,但如果不能,请告诉我。我正在尝试循环遍历值列表,并为每个值运行 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
Run Code Online (Sandbox Code Playgroud)

另一种情况涉及 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
Run Code Online (Sandbox Code Playgroud)

设置循环的正确语法是什么?当我尝试使用 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
Run Code Online (Sandbox Code Playgroud)

小智 5

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

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
Run Code Online (Sandbox Code Playgroud)