没有循环MS Access

时间:2015-05-30 23:01:41

标签: sql access-vba ms-access-2010

我不确定如何接近这个循环。我已经看过各种各样的方式并以各种方式尝试了If Not (rs.EOF And rs.BOF) Then,但它只进行了一次编码。该代码应该遍历满足SELECT查询条件的表Intake。肯定有多个记录符合此查询,因此它应该循环多次。这段代码第一次工作并给出了消息框,但随后就结束了。谢谢:))

Set db = CurrentDb

i = Me.searchlat

strSQL = "SELECT IntakeID, Program, applicationdate From Intake 
WHERE Status Not Like 'Approved' And Status Not Like 'Denied' And Status 
Not Like 'Withdrawn' And Status Not Like 'Incomplete Application' And Status 
Not Like 'No Show' AND workerid = '" & i & "'"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

While Not rs.EOF

If Me.Program = "CF" And Me.applicationdate < Date - "25" Then
MsgBox ("The following cases are nearing 30 days pending:" & " " & Me.caseid)
rs.MoveNext
End If
Wend

2 个答案:

答案 0 :(得分:2)

简单地说,将rs.MoveNext移到If语句之外但仍然在循环中。

目前,代码进入循环并且if条件不适用,退出循环(因为它没有看到移动到下一条记录的命令)。

另外,有时它会有所帮助,在While / Wend循环之前添加以下内容以确保记录集位于开头:

rs.MoveLast
rs.MoveFirst

答案 1 :(得分:1)

在您的选择查询中,您没有选择caseid。将您的选择查询更改为

"SELECT IntakeID, Program, applicationdate,caseid From Intake.......

并检查。

 strSQL = "SELECT IntakeID, Program, 
applicationdate, caseid From Intake WHERE Status Not Like 'Approved' 
And Status Not Like 'Denied' And Status Not Like 'Withdrawn' 
And Status Not Like 'Incomplete Application' And Status 
Not Like 'No Show' AND workerid = '" & i & "'"
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    While Not rs.EOF


If Me.Program = "CF" And Me.applicationdate < Date - "25" Then
    MsgBox ("The following cases are nearing 30 days pending:" & " " & rs!caseid)
End If
rs.MoveNext

Wend