Home > database >  Do until EOF missing the final entry in Access VBA
Do until EOF missing the final entry in Access VBA

Time:01-07

In the following code I am setting a True/False Field in order to print reports using the users input but the last record is not printed,

If I comment out the last loop (the loop to set all to false) I get all records printed but I need to set them all back to False

Sub PrintReports()
Dim rst As Recordset

    Dim startDate As Date
    Dim endDate As Date
 

    ' Prompt user to enter start date
    startDate = InputBox("Enter start date (mm/dd/yyyy):")

    ' Prompt user to enter end date
    endDate = InputBox("Enter end date (mm/dd/yyyy):")

Set rst = CurrentDb.OpenRecordset("SELECT * FROM CustomerT WHERE ExpiryDate >= #" & startDate & "# AND ExpiryDate <= #" & endDate & "#")


rst.MoveFirst

Do Until rst.EOF Or rst.BOF
  rst.Edit
  rst("ToPrint") = True
  rst.Update
  rst.MoveNext
Loop


DoCmd.OpenReport "2DateReports", acViewPreview, , , acNormal

rst.MoveFirst
Do Until rst.EOF Or rst.BOF
  rst.Edit
  rst("ToPrint") = False
  rst.Update
  rst.MoveNext
Loop

End Sub

CodePudding user response:

Most likely a timing issue - perhaps the report hasn't finished printing.

Try this:

DoCmd.OpenReport "2DateReports", acViewPreview, , , acNormal
DoEvents
Stop         ' Press F5 to continue. To be removed after test.
  • Related