I'm using the following code to loop through a recordset which suppose to transfer all selected records to the Word document in a tabular format. It works however it gives me all records except the last one (i.e. I select 4 records and it returns only 3 from the top).
Private Sub cmdWord_Click()
Dim oWord As Object 'Word.Application
Dim doc As Object 'Word.Document
Set oWord = CreateObject("Word.Application")
Set doc = oWord.Documents.Open("D:\Working\Templates\Letter.doc")
oWord.Visible = True
Dim oAccess As Object
Dim dbs As Database
Dim rst As Recordset
Dim strCriteria As String
With oWord.ActiveDocument
If .Bookmarks.Exists("Date") = True Then
.Bookmarks("Date").Select
If Not IsNull([Forms]![frmForReport_Preview]!Date) Then
oWord.Selection.Text = (CStr(Format([Forms]![frmForReport_Preview]!Date, "mmm d, yyyy")))
Else
oWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("Table") = True Then
Set dbs = CurrentDb
strCriteria = "SELECT tblForLetter.ID, tblForLetter.DID, tblForLetter.AT, tblForLetter.Expr1, tblForLetter.L FROM tblForLetter WHERE (((tblForLetter.Print)=-1=True));"
Set rst = dbs.OpenRecordset(strCriteria, dbOpenSnapshot)
oWord.ActiveDocument.Bookmarks("Table").Select
oWord.Selection.Text = ""
i = 1
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
doc.Tables(1).Columns(1).Cells.Add
doc.Tables(1).Columns(1).Cells(i + 1).range.Text = rst.Fields(0)
doc.Tables(1).Columns(2).Cells(i + 1).range.Text = rst.Fields(1)
doc.Tables(1).Columns(3).Cells(i + 1).range.Text = rst.Fields(2)
doc.Tables(1).Columns(4).Cells(i + 1).range.Text = rst.Fields(3)
doc.Tables(1).Columns(5).Cells(i + 1).range.Text = rst.Fields(4)
rst.MoveNext
i = i + 1
Loop
Else
MsgBox ("There are no records in the recordset.")
End If
DoCmd.Close acForm, "frmForReport_Preview", acSaveYes
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * From [tblForLetter]"
DoCmd.SetWarnings True
rst.Close
Set dbs = Nothing
End If
End With
End Sub
How exactly do you select the records in the form?
I'm suspecting this:
tblForLetter
or SELECT * FROM tblForLetter
or something like thisPrint
column as the Control Source.Print
column in the respective row is set to True
/ -1
.If you're doing it like this, you should consider the following:
So when you select multiple checkboxes, what really happens is this:
Now there's an additional gotcha:
When a row is in edit mode, and you click on a control that's in the form's header or footer (in other words, not in the "detail" area, the actual continuous part of the form), that row stays in edit mode and the changes are not saved to the database yet.
cmdWord
button which triggers the code in your question, but in that moment, the fourth row is still in edit mode.... WHERE tblForLetter.Print=True
, you get only the first three selected rows, but not the fourth because in that moment, tblForLetter.Print
is still False
in the database.Generally speaking, you need to make sure that no row is still in edit mode when you run the query that gets the selected rows from the table.
Without changing your code, you can see the difference when you select your four rows, set the focus to another row so that the fourth row is saved as well, and then click the button.
To do this automatically in your code, you can requery the form's data source by just calling Me.Requery
before selecting from the table.
Me.Requery
will reload the data in the form. If there's still a row in edit mode in that moment, edit mode will end first and the changes in that row will be saved to the database.