J
Jody
I have a Word template that is set up to receive Access
data. It's working fine except that I would like to put
multiple records into one field (bmkAttendees) in the Word
document. Each record will be separated by a semicolon.
So far the program only inserts the first record and
stops. How do I have it continue inserting all the
records based on the select statement? Here's my code:
Private Sub cmdPrint_Click()
Dim objWord As Word.Application
Dim doc As Word.Document
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim strSalesPerson As String
Dim strCustomer As String
Dim dtmRept_Date As Date
Dim strItem As String
Dim strItem_Owner As String
Dim strAttendees As String
strSQL = "SELECT * FROM CallVisit " _
& "WHERE [Call_VisitID] = " & Me![Call_VisitID]
rst.Open strSQL, CurrentProject.Connection,
adOpenStatic, adLockReadOnly
With rst
strCustomer = Nz(.Fields("Customer")) & " " _
& Nz(.Fields("CustLoc")) & " " & Nz(.Fields
("Call_Date"))
strSalesPerson = Nz(.Fields("SalesPerson"))
dtmRept_Date = Nz(.Fields("Rept_Date"))
.Close
End With
strSQL = "SELECT Attend_FNm, Attend_LNm, Attend_Title
FROM CallAttendees " _
& "WHERE [Call_VisitID]=" & Me![Call_VisitID]
rst.Open strSQL
With rst
strAttendees = Nz(.Fields("Attend_FNm")) & " " & Nz
(.Fields("Attend_LNm")) & ", " _
& Nz(.Fields("Attend_Title")) & "; "
.Close
End With
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err = 429 Then
Set objWord = New Word.Application
End If
On Error GoTo 0
With objWord
.Visible = True
Set doc = .Documents.Add("\\Data1\public data\DEPT
SHARES\RQP Sales\Travel\Sales Trip Report.dot")
With doc.Bookmarks
.Item("bmkSalesPerson").Range.Text =
strSalesPerson
.Item("bmkCustomer").Range.Text = strCustomer
.Item("bmkRept_Date").Range.Text = dtmRept_Date
.Item("bmkAttendees").Range.Text = strAttendees
End With
End With
strSQL = "SELECT Item, Item_Owner FROM CallDetails " &
_
"WHERE [Call_VisitID]=" & Me![Call_VisitID]
rst.Open strSQL
With rst
doc.Bookmarks("bmkCallDetail").Select
Do Until .EOF
strItem = Nz(.Fields("Item"))
strItem_Owner = Nz(.Fields("Item_Owner"))
With objWord.Selection
.TypeText strItem
.MoveRight wdCell
.TypeText strItem_Owner
.MoveRight wdCell
.MoveRight wdCell
.MoveRight wdCell
End With
.MoveNext
Loop
End With
objWord.Activate
Set rst = Nothing
Set doc = Nothing
Set objWord = Nothing
End Sub
data. It's working fine except that I would like to put
multiple records into one field (bmkAttendees) in the Word
document. Each record will be separated by a semicolon.
So far the program only inserts the first record and
stops. How do I have it continue inserting all the
records based on the select statement? Here's my code:
Private Sub cmdPrint_Click()
Dim objWord As Word.Application
Dim doc As Word.Document
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim strSalesPerson As String
Dim strCustomer As String
Dim dtmRept_Date As Date
Dim strItem As String
Dim strItem_Owner As String
Dim strAttendees As String
strSQL = "SELECT * FROM CallVisit " _
& "WHERE [Call_VisitID] = " & Me![Call_VisitID]
rst.Open strSQL, CurrentProject.Connection,
adOpenStatic, adLockReadOnly
With rst
strCustomer = Nz(.Fields("Customer")) & " " _
& Nz(.Fields("CustLoc")) & " " & Nz(.Fields
("Call_Date"))
strSalesPerson = Nz(.Fields("SalesPerson"))
dtmRept_Date = Nz(.Fields("Rept_Date"))
.Close
End With
strSQL = "SELECT Attend_FNm, Attend_LNm, Attend_Title
FROM CallAttendees " _
& "WHERE [Call_VisitID]=" & Me![Call_VisitID]
rst.Open strSQL
With rst
strAttendees = Nz(.Fields("Attend_FNm")) & " " & Nz
(.Fields("Attend_LNm")) & ", " _
& Nz(.Fields("Attend_Title")) & "; "
.Close
End With
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err = 429 Then
Set objWord = New Word.Application
End If
On Error GoTo 0
With objWord
.Visible = True
Set doc = .Documents.Add("\\Data1\public data\DEPT
SHARES\RQP Sales\Travel\Sales Trip Report.dot")
With doc.Bookmarks
.Item("bmkSalesPerson").Range.Text =
strSalesPerson
.Item("bmkCustomer").Range.Text = strCustomer
.Item("bmkRept_Date").Range.Text = dtmRept_Date
.Item("bmkAttendees").Range.Text = strAttendees
End With
End With
strSQL = "SELECT Item, Item_Owner FROM CallDetails " &
_
"WHERE [Call_VisitID]=" & Me![Call_VisitID]
rst.Open strSQL
With rst
doc.Bookmarks("bmkCallDetail").Select
Do Until .EOF
strItem = Nz(.Fields("Item"))
strItem_Owner = Nz(.Fields("Item_Owner"))
With objWord.Selection
.TypeText strItem
.MoveRight wdCell
.TypeText strItem_Owner
.MoveRight wdCell
.MoveRight wdCell
.MoveRight wdCell
End With
.MoveNext
Loop
End With
objWord.Activate
Set rst = Nothing
Set doc = Nothing
Set objWord = Nothing
End Sub