MS Access 2010 VBA整数变量不在循环中更改

时间:2017-08-17 20:49:08

标签: vba

我的代码循环遍历一组成员编号并检索每个成员编号的记录。每次,我都需要使用最多返回12的记录的计数。但是,一旦设置了保持计数的变量,它将不会在下次调用时重置。它也从第一个记录“跳转”到最后一个记录而不是循环遍历每个记录。换句话说,如果记录集返回了4条记录,它将执行第一个和最后一个,然后给出错误“No Current Record”这是我的代码:

 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确实发生变化,也不会发生变化。

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您有两个不同的问题。首先,使用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