Access-VBA For循环仅执行一次

时间:2019-01-27 08:47:41

标签: sql vba for-loop ms-access access-vba

我正在尝试测量两次更新之间的持续时间。已经有一个表来跟踪“ lastvalue”,“ newvalue”和“ updatetime”。我的方法是搜索满足条件的实体,并为变量分配相关的更新时间,然后进行计算。 但是,我的for循环不起作用,它们只能执行一次。我是access-vba的新手,并使用debug.prints来查看事物的变化(它们是永久的)。以下是我的代码,谢谢

Private Sub olcum()

Dim gs As Long
Dim db As Database
Dim rs As Recordset
Dim pt As Date
Dim ct As Date
Dim pc As String
Dim cc As String
Dim id As Integer
Dim i As Integer
Dim l As Integer
Dim k As Integer
'Dim a As Index
Dim strMessage As String


Set db = CurrentDb
Set rs = db.OpenRecordset("GecenSure", dbOpenSnapshot)
pc = "acilmasi bekleniyor"
cc = "onayda"


rs.MoveFirst
For i = 0 To (rs.RecordCount - 1)
    strMessage = "Burada: " & (rs.AbsolutePosition + 1)
    Debug.Print (strMessage)
    'Set a = rs.AbsolutePosition
    'Debug.Print (a)
    id = rs.Fields("Kimlik")
    rs.MoveFirst

    For l = 0 To (rs.RecordCount - 1)
        strMessage = "Burada: " & (rs.AbsolutePosition + 1)
        Debug.Print (strMessage)
        If (rs.Fields("Kimlik") = id) And (rs.Fields("PreviousCase") = pc) Then
        pt = rs.Fields("UpdateTime")
        Else
        End If

        For k = 0 To (rs.RecordCount - (1 + l))
            strMessage = "Burada: " & (rs.AbsolutePosition + 1)
            Debug.Print (strMessage)
        'If (rs.Fields("Kimlik") = id) And (rs.Fields("CurrentCase") = cc) Then
            'Debug.Print "rs.AbsolutePosition"
            If (rs.Fields("Kimlik") = id) And (rs.Fields("PreviousCase") = cc) Then
            ct = rs.Fields("UpdateTime")
            Else
            End If
        'Else
        'End If
        rs.MoveNext
        strMessage = "Burada: " & (rs.AbsolutePosition + 1)
        Debug.Print (strMessage)
        Next k
        'bu prosedurle 2 kere ct atıyor.

    rs.MoveFirst

    For z = 0 To i
    rs.MoveNext
    strMessage = "Burada: " & (rs.AbsolutePosition + 1)
    Debug.Print (strMessage)
    Next z

    Next l

    gs = ct - pt
    Debug.Print gs

    'Debug.Print rs.Fields("CurrentCase")
rs.MoveNext
strMessage = "Burada: " & (rs.AbsolutePosition + 1)
Debug.Print (strMessage)
Next i

rs.Close
Set rs = Nothing
db.Close

MsgBox "Simdiki durumlar gosterildi"

End Sub

2 个答案:

答案 0 :(得分:5)

rs.RecordCount不返回记录集中的记录总数;而是返回已被访问的记录数。

有几种选择;选项3通常被认为是最好的,并且是许多Microsoft示例中使用的方法:

  1. rs.MoveLast -循环之前;这不是性能选择
  2. 查询SQL 计数-在循环之前,确定记录数
  3. 使用同时执行循环检查rs.EOF (请参见https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb243789(v=office.12)

Do循环示例:

Set rs = db.OpenRecordSet("GecenSure", dbOpenSnapshot)

Do Until rs.EOF
    ' do something
    rs.MoveNext
Loop

rs.Close
Set rs = Nothing

答案 1 :(得分:0)

我建议使用这样的结构:

If Not (rs.BOF And rs.EOF) Then ' if not completely empty
    rs.MoveFirst                ' start at the beginning
    Do While Not rs.EOF         ' while not at end

        do something
        increment i if necessary

        ' error checking if needed:
        If i > ... Or SomethingUnwanted Then Exit Do

        rs.MoveNext
    Loop
End If
rs.Close