嵌套访问VBA

时间:2017-12-04 00:51:39

标签: access-vba nested-loops

我试图在Access VBA中运行嵌套循环。每天的每小时(0到23)循环记录集并打印每个实例,小时落在BeginHour和EndHour字段之间。看起来内循环只运行一次,但在程序结束时i = 24。

样本记录集:

BeginHour   EndHour     Desired Result
    9        10              9
   10        11             10
   10        11             10
   10        12             10
                            10

代码:

Sub hourCount()
Dim myDatabase As DAO.Database
Dim rst As DAO.Recordset
Dim i As Integer

Set myDatabase = CurrentDb
Set rst = myDatabase.OpenRecordset("DailyData_BTJob")

For i = 0 To 23
Do While Not rst.EOF
  If i >= rst!BeginHour And i <= rst!EndHour Then
    Debug.Print i
  End If
rst.MoveNext
Loop
i = i + 1
Next

Set myDatabase = Nothing
Set rst = Nothing
End Sub

0 个答案:

没有答案