2017-08-17 83 views
0

我有循環遍歷數組成員數組並檢索每個記錄的代碼。每次我都需要使用返回記錄數爲12的計數。但是,一旦保存計數的變量被設置,它將不會在下一次調用時重置。它也從第一個到最後一個記錄「跳」,而不是循環遍歷每個記錄。換句話說,如果存在由記錄返回4條記錄,將執行第一個和最後一個,然後給出「沒有當前記錄」錯誤這裏是我的代碼:MS Access 2010 VBA整數變量不會在循環中更改

Dim x As Integer 
For i = 1 To intMembers 
strGetMemberInfo = "SELECT PatientRecords.[Medication Name], PatientRecords.[First Name], PatientRecords.[Last Name],PatientRecords.[doc phone]" _ 
& " FROM PatientRecords WHERE member_no ='" & arrMembers(i) & "'" 
Set rstMedicine = dbs.OpenRecordset(strGetMemberInfo, dbOpenSnapshot) 
Dim intMedicine As Integer 
    intMedicine = rstMedicine.RecordCount 
    If intMedicine > 12 Then 
    intMedicine = 12 
    End If 

Do Until rstMedicine.EOF 
    For x = 1 To intMedicine 
    strMedicationField = strMedication & x 
    strDoctorFNameField = strDoctorFName & x 
    strDoctorLNameField = strDocotrLName & x 
    strDoctorPhoneField = strDoctorPhone & x 
    strSQL = "UPDATE TransformationTable SET " & strMedicationField & " = '" & rstMedicine.Fields("[Medication Name]").Value & "'," & strDoctorFNameField & " = '" & rstMedicine.Fields("[First Name]").Value & "', " & strDoctorLNameField & " = '" & Replace(rstMedicine.Fields("[Last Name]"), "'", "''") & "', " & strDoctorPhoneField & " = '" & rstMedicine.Fields("[doc phone]").Value & "' WHERE member_no ='" & arrMembers(i) & "'" 
    dbs.Execute strSQL 


rstMedicine.MoveNext 
Next x 
Loop 
rstMedicine.Close 
Set rstMedicine = Nothing 
Next i 

在上面的代碼,intMedicine由第一個記錄集設置,即使rstMedicine.RecordCount確實發生變化也不會改變。

任何幫助表示讚賞

+0

rstMedicine中有多少條記錄?如果它大於12,那麼intMedicine將始終爲12.您說'它不會在下一次調用時重置,但是您希望它重置爲什麼值? – 0liveradam8

+1

你有調試嗎? – June7

+0

它停留在1.它沒有事件設置爲12 –

回答

0

你有2個不同的問題。首先,使用rstMedicine.MoveLast移動到記錄集的底部並獲得完整的計數。第二。您將「週期數」限制爲12,但intMedicine爲12後仍不退出循環,因此它仍在嘗試到達記錄集的末尾,因爲您的代碼顯示「Do Until rstMedicine.EOF」。將您的代碼更改爲:

 Dim x As Integer 
    For i = 1 To intMembers 
    strGetMemberInfo = "SELECT PatientRecords.[Medication Name], PatientRecords.[First Name], PatientRecords.[Last Name],PatientRecords.[doc phone]" _ 
    & " FROM PatientRecords WHERE member_no ='" & arrMembers(i) & "'" 
    Set rstMedicine = dbs.OpenRecordset(strGetMemberInfo, dbOpenSnapshot) 
rstMedicine.MoveLast 
    Dim intMedicine As Integer 
     intMedicine = rstMedicine.RecordCount 
     If intMedicine > 12 Then 
     intMedicine = 12 
     End If 
    rstMedicine.MoveFirst 
    Do Until rstMedicine.EOF 
     For x = 1 To intMedicine 
     strMedicationField = strMedication & x 
     strDoctorFNameField = strDoctorFName & x 
     strDoctorLNameField = strDocotrLName & x 
     strDoctorPhoneField = strDoctorPhone & x 
     strSQL = "UPDATE TransformationTable SET " & strMedicationField & " = '" & rstMedicine.Fields("[Medication Name]").Value & "'," & strDoctorFNameField & " = '" & rstMedicine.Fields("[First Name]").Value & "', " & strDoctorLNameField & " = '" & Replace(rstMedicine.Fields("[Last Name]"), "'", "''") & "', " & strDoctorPhoneField & " = '" & rstMedicine.Fields("[doc phone]").Value & "' WHERE member_no ='" & arrMembers(i) & "'" 
     dbs.Execute strSQL 


    rstMedicine.MoveNext 
If x = 12 Then 
Exit Do 
End If 
    Next x 
    Loop 
    rstMedicine.Close 
    Set rstMedicine = Nothing 
    Next i