All seemed to be working well, however, I noticed that all my subtable records in the database are exporting with each Primary table record. In my output, i'm looking to see each primary table record followed by one or more subtable records from a one to many relationship.
Thanks in advance. (Office 2010) Access/Word
Private Sub cmdPrint1_Click()
Dim objWord As Word.Application
Dim docm As Word.Document
Dim db As DAO.Database
Dim rstLandSales As DAO.Recordset
Dim rstLandData As DAO.Recordset
Dim strLandSalesID As String
Dim strPropertyName As String
Dim strClass As String
Dim J As Integer
Set db = CurrentDb
Set rstLandSales = db.OpenRecordset("qryLandSales")
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
Set docm = .Documents.Add("C:\Users\GlennJ\AppData\Roaming\Microsoft\Templates\Report2007Forms.dotm")
.Visible = True
objWord.ActiveWindow = True
End With
Do While Not rstLandSales.EOF
With rstLandSales '(Primary Table records)
strLandSalesID = Nz(.Fields("[LandSalesID]"))
strPropertyName = Nz(.Fields("[PropertyName]"))
strClass = Nz(.Fields("[Class]"))
objWord.Run "GHLandCompTable1"
With objWord.Selection
.MoveRight Unit:=wdCell, Count:=2
.TypeText strLandSalesID
.MoveRight Unit:=wdCell, Count:=2
.TypeText strPropertyName
.MoveRight Unit:=wdCell, Count:=2
.TypeText strPropertyName
.MoveRight Unit:=wdCell, Count:=2
.TypeText strClass
.MoveRight Unit:=wdCell, Count:=2
End With
On Error Resume Next
ActiveDocument.GoTo What:=wdGoToTable, Which:=wdGoToLast
On Error Resume Next
With ActiveDocument.Content
With Selection
.Tables(1).Cell(1, 1).Select
.MoveRight Unit:=wdCell, Count:=1
.EndKey Unit:=wdLine
J = J + 1
.TypeText Text:=J
.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
End With
objWord.Run "bmLandCompdetailP1"
objWord.Run "SpaceDeleteUp1"
End With
End With
'LandData (SubTable Records)
Dim strLandType As String
Dim strAcreage As String
Dim strFrontage As String
Dim strZoning As String
Dim L As Integer
L = 0
Set rstLandData = db.OpenRecordset("qryLandData")
Do While Not rstLandData.EOF
With rstLandData
strLandType = Nz(.Fields("[LandType]"))
strAcreage = Nz(.Fields("[Acreage]"))
strFrontage = Nz(.Fields("[Frontage]"))
strZoning = Nz(.Fields("[Zoning]"))
End With
objWord.Run "GHLandCompTable3"
With objWord.Selection
' .MoveRight Unit:=wdCell, Count:=2
.TypeText strLandType
.MoveRight Unit:=wdCell, Count:=2
.TypeText strAcreage
.MoveRight Unit:=wdCell, Count:=2
.TypeText strFrontage
.MoveRight Unit:=wdCell, Count:=2
.TypeText strZoning
.MoveRight Unit:=wdCell, Count:=2
.MoveUp Unit:=wdLine, Count:=2
End With
On Error Resume Next
ActiveDocument.GoTo What:=wdGoToTable, Which:=wdGoToLast
On Error Resume Next
With ActiveDocument.Content
With Selection
.Tables(1).Cell(1, 1).Select
.MoveRight Unit:=wdCell, Count:=1
.EndKey Unit:=wdLine
L = L + 1
.TypeText Text:=L
.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
End With
End With
objWord.Run "bmLandCompdetailP3"
objWord.Run "SpaceDeleteUp1"
rstLandData.MoveNext
Loop
rstLandData.Close
rstLandSales.MoveNext
Loop
rstLandSales.Close
End Sub
Thanks in advance. (Office 2010) Access/Word
Private Sub cmdPrint1_Click()
Dim objWord As Word.Application
Dim docm As Word.Document
Dim db As DAO.Database
Dim rstLandSales As DAO.Recordset
Dim rstLandData As DAO.Recordset
Dim strLandSalesID As String
Dim strPropertyName As String
Dim strClass As String
Dim J As Integer
Set db = CurrentDb
Set rstLandSales = db.OpenRecordset("qryLandSales")
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
Set docm = .Documents.Add("C:\Users\GlennJ\AppData\Roaming\Microsoft\Templates\Report2007Forms.dotm")
.Visible = True
objWord.ActiveWindow = True
End With
Do While Not rstLandSales.EOF
With rstLandSales '(Primary Table records)
strLandSalesID = Nz(.Fields("[LandSalesID]"))
strPropertyName = Nz(.Fields("[PropertyName]"))
strClass = Nz(.Fields("[Class]"))
objWord.Run "GHLandCompTable1"
With objWord.Selection
.MoveRight Unit:=wdCell, Count:=2
.TypeText strLandSalesID
.MoveRight Unit:=wdCell, Count:=2
.TypeText strPropertyName
.MoveRight Unit:=wdCell, Count:=2
.TypeText strPropertyName
.MoveRight Unit:=wdCell, Count:=2
.TypeText strClass
.MoveRight Unit:=wdCell, Count:=2
End With
On Error Resume Next
ActiveDocument.GoTo What:=wdGoToTable, Which:=wdGoToLast
On Error Resume Next
With ActiveDocument.Content
With Selection
.Tables(1).Cell(1, 1).Select
.MoveRight Unit:=wdCell, Count:=1
.EndKey Unit:=wdLine
J = J + 1
.TypeText Text:=J
.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
End With
objWord.Run "bmLandCompdetailP1"
objWord.Run "SpaceDeleteUp1"
End With
End With
'LandData (SubTable Records)
Dim strLandType As String
Dim strAcreage As String
Dim strFrontage As String
Dim strZoning As String
Dim L As Integer
L = 0
Set rstLandData = db.OpenRecordset("qryLandData")
Do While Not rstLandData.EOF
With rstLandData
strLandType = Nz(.Fields("[LandType]"))
strAcreage = Nz(.Fields("[Acreage]"))
strFrontage = Nz(.Fields("[Frontage]"))
strZoning = Nz(.Fields("[Zoning]"))
End With
objWord.Run "GHLandCompTable3"
With objWord.Selection
' .MoveRight Unit:=wdCell, Count:=2
.TypeText strLandType
.MoveRight Unit:=wdCell, Count:=2
.TypeText strAcreage
.MoveRight Unit:=wdCell, Count:=2
.TypeText strFrontage
.MoveRight Unit:=wdCell, Count:=2
.TypeText strZoning
.MoveRight Unit:=wdCell, Count:=2
.MoveUp Unit:=wdLine, Count:=2
End With
On Error Resume Next
ActiveDocument.GoTo What:=wdGoToTable, Which:=wdGoToLast
On Error Resume Next
With ActiveDocument.Content
With Selection
.Tables(1).Cell(1, 1).Select
.MoveRight Unit:=wdCell, Count:=1
.EndKey Unit:=wdLine
L = L + 1
.TypeText Text:=L
.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
End With
End With
objWord.Run "bmLandCompdetailP3"
objWord.Run "SpaceDeleteUp1"
rstLandData.MoveNext
Loop
rstLandData.Close
rstLandSales.MoveNext
Loop
rstLandSales.Close
End Sub