gpt4 book ai didi

vba - MS Access VBA 循环遍历列表

转载 作者:行者123 更新时间:2023-12-05 08:43:51 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于vba - MS Access VBA 循环遍历列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com