0

I have code that is looping through an array of member numbers and retrieving records for each. Each time, I need to use the count of the records returned up to 12. However, once the variable that is to hold the count is set, it will not reset with the next call. It also "jumps" from the first to the last record rather than looping through each. In other words, if there are 4 records returned by the recordset, it will execute for the first and the last and then give an error of "No Current Record" Here is my code:

 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

In the above code, intMedicinegets set by the first recordset and NEVER changes even though rstMedicine.RecordCount does change.

Any help is appreciated

7
  • How many records are in rstMedicine? If it is greater than twelve, then intMedicine will always be 12. You say it will not reset with the next call, however what value would you like it to reset to? Commented Aug 17, 2017 at 20:59
  • 1
    Have you step debugged? Commented Aug 17, 2017 at 21:00
  • it stays at 1. It doesn't event set to 12 Commented Aug 17, 2017 at 21:36
  • Check the return value of rstMedicine.RecordCount - this is not always reliable Commented Aug 17, 2017 at 21:42
  • The RecordCount is coming back correctly. Commented Aug 17, 2017 at 22:15

1 Answer 1

0

You have 2 different issues. First, use rstMedicine.MoveLast to move to the bottom of the recordset and get the full count. Second. Your limiting the number of "cycles" to 12, but you are not exiting the loop after intMedicine is 12, so it is still trying to get to the end of the recordset because your code says "Do Until rstMedicine.EOF". Change your code to this:

      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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.