n = 0
conDB
Set oRs = New Recordset
strSql = "Select * from Transactions where Status='Unpaid'"
oRs.Open strSql, goCnn, adOpenKeyset, adLockOptimistic, adCmdText
x = oRs.RecordCount
axn
Do While Not oRs.EOF
strItem = oRs("Group Name")
Set oRst = New Recordset
strsql1 = "Select * from Transactions where [Group Name]='" & strItem & "'" & " And status='Unpaid'"
oRst.Open strsql1, axnCnn, adOpenKeyset, adLockOptimistic
Set oRsy = New Recordset
strsql2 = "Select * from Groups where [Group Name]='" & strItem & "'"
oRsy.Open strsql2, axnCnn, adOpenKeyset, adLockOptimistic
y = oRst.RecordCount
For i = 1 To y
If n = 0 Then
Set WordObject = CreateObject("Word.Application")
With WordObject
.Application.Documents.Add App.Path & "\Invoice.dot"
.Visible = True
.Activate
.ActiveDocument.Bookmarks("Date").Range.InsertAfter Format(Date, "DD/MM/YYYY")
.ActiveDocument.Bookmarks("GROUPNAME").Range.InsertAfter oRst("Group Name")
.ActiveDocument.Bookmarks("GROUPID").Range.InsertAfter oRst("GroupID")
.ActiveDocument.Bookmarks("Address").Range.InsertAfter oRsy("Address")
.ActiveDocument.Bookmarks("TELEPHONE").Range.InsertAfter oRsy("Phone")
.Selection.MoveDown wdLine, 17
.Selection.MoveLeft wdCharacter, 2
Do While oRst.EOF = False
.Selection.TypeText oRst!Invoice_ID
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!VisitDate
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!TreatMent
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!AmountCharged
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!AmountPaid
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!AmountToPay
.Selection.MoveRight wdCharacter, 1
.Selection.TypeText oRst!Status
oRst.MoveNext
If oRst.EOF = False Then
.Application.ActiveDocument.Tables(2).Rows.Add
.Selection.MoveDown wdLine, 1
.Selection.MoveLeft wdCharacter, 6
End If
Loop
Next
End With
End If