2015-05-30 101 views
0

我不知道如何處理這個循環。我已經看到了各種方式,並以各種方式嘗試了If Not (rs.EOF And rs.BOF) Then,但它只通過編碼一次。這段代碼應該通過符合SELECT查詢條件的表Intake循環。肯定有多個記錄符合這個查詢,所以它應該循環多次。這段代碼第一次工作,並給出消息框,但只是結束。謝謝:)不循環MS Access

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 
+0

使用,而(不RS.EOF)... WEND – Tim3880

+0

嗨@ Tim3880我曾用WEND但它沒有通過編碼循環一遍,我不知道如果我不把它在正確的地方,但它跳過了strSQL。 – Lilly

+1

對不起,我誤解了你的問題,我會刪除答案。如果要循環訪問數據集(Me)並執行不同的查詢,則應在循環中放置「Set rs = ...」。 – Tim3880

回答

1

在SELECT查詢,你沒有選擇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 
2

簡單,移動rs.MoveNext如果語句之外,但仍處於循環。

當前,代碼進入循環,並且if條件不適用的位置退出循環(因爲它看不到移動到下一條記錄的命令)。

此外,有時有幫助,雖然在/ Wend循環之前添加以下,以確保記錄位於開頭:

rs.MoveLast 
rs.MoveFirst 
+0

我做了兩件事情,它仍然沒有選擇下一個記錄。它循環的次數是正確的,但是在'&'「&Me.caseid)'的'msg'中,它應該是新的記錄caseid。 – Lilly