2017-08-17 5 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 not 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 
関連する問題