2017-08-17 52 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