I am using the following code to read a series of records from a table and use the names included in each record to print a form.
The record in "Enrollments" begins with a StudentID followed by the StudentName and then a number of other fields. The Enrollments table is sorted alphabetically by StudentName. However the reading part of the loop goes through the names in a very odd sequence: it starts with a name that begins with Q, then follows the StudentID sequence for a while, then switches to some non-sequential StudentID and again goes on for a while in StudentID sequence, etc. I don't know where it is getting the StudentID information and the jumping around seems to make no sense, but in any case I'd really like it to go through the list alphabetically.
Code:
Private Sub Command0_Click()
Dim rst As DAO.Recordset
Dim printout As Integer
printout = 0
Const MemberQuery As String = "SELECT StudentName FROM Enrollments"
' Clear the form
Forms.DistributeLetters.StudentName = ""
' Get a recordset using the query
Set rst = CurrentDb.OpenRecordset(MemberQuery, dbOpenSnapshot)
' Move through the recordset looking at each record
With rst
Do While Not .EOF
Forms.DistributeLetters.StudentName = ![StudentName]
' temporary message to track sequence
MsgBox (Forms.DistributeLetters.StudentName)
'DoCmd.OpenReport ("Enrolled Students Letter")
printout = printout + 1
If printout Mod 25 = 0 Then MsgBox ("Check Printer Queue")
.MoveNext
Loop
End With
rst.Close
Set rst = Nothing
exit_emailerror:
Exit Sub
emailerror:
MsgBox "There was an error in the subroutine"
Resume exit_emailerror
End Sub
The record in "Enrollments" begins with a StudentID followed by the StudentName and then a number of other fields. The Enrollments table is sorted alphabetically by StudentName. However the reading part of the loop goes through the names in a very odd sequence: it starts with a name that begins with Q, then follows the StudentID sequence for a while, then switches to some non-sequential StudentID and again goes on for a while in StudentID sequence, etc. I don't know where it is getting the StudentID information and the jumping around seems to make no sense, but in any case I'd really like it to go through the list alphabetically.