我正在尝试使用嵌套的do ... while循环遍历访问表中的所有记录。
每个循环都有不同的标准,所以我在运行循环之前打开了多个记录集。
当我运行代码时,它正常工作,但问题是当其中一个记录集没有记录时整个过程失败。
以下是嵌套循环的代码。
Dim rst1, rst2, rst3, rst4, rst5, rst6 As Recordset
Dim sqlSt1, sqlSt2, sqlSt3, sqlSt4, sqlSt5, sqlSt6 As String
Dim rcdCount, a, b, c, d, e, f As Double
Set dbs = CurrentDb
sqlSt1 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 1"
sqlSt2 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 2"
sqlSt3 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 3"
sqlSt4 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 4"
sqlSt5 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 5"
sqlSt6 = "SELECT * FROM [Rune List - Optimizer List] WHERE [Rune Slot]= 6"
Set rst1 = dbs.OpenRecordset(sqlSt1)
Set rst2 = dbs.OpenRecordset(sqlSt2)
Set rst3 = dbs.OpenRecordset(sqlSt3)
Set rst4 = dbs.OpenRecordset(sqlSt4)
Set rst5 = dbs.OpenRecordset(sqlSt5)
Set rst6 = dbs.OpenRecordset(sqlSt6)
startTime = Time
rcdCount = rst1.recordCount
SysCmd acSysCmdInitMeter, "Processing: ", rcdCount
Debug.Print "4: " & rst4.recordCount
Debug.Print "6: " & rst6.recordCount
Do While Not rst1.EOF And Not rst1.BOF
Do While Not rst2.EOF And Not rst2.BOF
Do While Not rst3.EOF And Not rst3.BOF
Do While Not rst4.EOF And Not rst4.BOF
Do While Not rst5.EOF And Not rst5.BOF
Do While Not rst6.EOF And Not rst6.BOF
f = f + 1
rst6.MoveNext
Loop
e = e + 1
rst6.MoveFirst
rst5.MoveNext
Loop
d = d + 1
rst5.MoveFirst
rst4.MoveNext
Loop
c = c + 1
rst4.MoveFirst
rst3.MoveNext
Loop
b = b + 1
rst3.MoveFirst
rst2.MoveNext
Loop
a = a + 1
SysCmd acSysCmdUpdateMeter, a
DoEvents
rst2.MoveFirst
rst1.MoveNext
Loop
rst1.Close
rst2.Close
rst3.Close
rst4.Close
rst5.Close
rst6.Close
SysCmd acSysCmdRemoveMeter
endTime = Time
tmDuration = DateDiff("s", CDate(startTime), CDate(endTime))
tm = Format(Int([tmDuration] / 3600), "00") & ":" & Format(Int(([tmDuration] - (Int([tmDuration] / 3600) * 3600)) / 60), "00") & ":" & Format((([tmDuration] Mod 60)), "00")
Debug.Print a
Debug.Print b
Debug.Print c
Debug.Print d
Debug.Print e
Debug.Print f
Debug.Print "-----------------"
Debug.Print tm
有没有一种简单的方法可以检查空记录并跳过会导致问题的记录?
感谢您的帮助
ķ
答案 0 :(得分:0)
看起来你想要的只是计数。为什么不使用公式而不是循环。
f = rst6.recordcount * rst5.recordcount * ...
e = rst5.recordcount * rst4.recordcount * ...
对所有六个变量执行此操作。要意识到如果rst4.recordcount为0,则d,e,f将得到0。但顺便说一句,你编写的是你想要的。