Form and Subform recordsource

batwings

Registered User.
Local time
Today, 23:17
Joined
Nov 4, 2007
Messages
40
Hi there

I have a form and subform with one to many records derived from 2 tables and I'm using this as a source to populate Formfields on a Word document.

I am using a VBA script I found and altered linked to a button on my form.
Problem is only the records from the Main form window are being output to the document formfields but not the child records from the subform.

How is it possible to have both the Parent and Child records referenced so that all of my formfields are filled.

Code is below.
Code:
Private Sub Command24_Click()
'Print Certificates
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As ADODB.Recordset
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
'Populate recordset object.
Set rst = New ADODB.Recordset
rst.Open Me.RecordSource, CurrentProject.Connection
'Cycle through records to fill Word form fields.
Do While Not rst.EOF
Set doc = appWord.Documents.Open("C:\Template\Cert.doc", , True)
With doc
'Mainform Formfields
.FormFields("fldA").Result = rst!FieldA
.FormFields("fldB").Result = rst!FieldB
.FormFields("fldC").Result = rst!FieldC
.FormFields("fldD").Result = rst!FieldD
.FormFields("fldE").Result = rst!FieldE
'Subform Formfields
.FormFields("fldF").Result = rst!FieldF 
.FormFields("fldG").Result = rst!FieldG
 
'Checkboxes on Mainform
If Me.Full = -1 Then
appWord.ActiveDocument.FormFields("fldchk1").CheckBox.Value = True
End If
If Me.Partial = -1 Then
appWord.ActiveDocument.FormFields("fldchk2").CheckBox.Value = True
End If
'.Visible = True
'.Activate
'.PrintOut
.SaveAs "C:\Certificate\" & rst!CertNo & ""
rst.MoveNext
End With
Loop
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
 
If there are only 2 fields on your sub form that you want to pass to the word doc you could cheat so that you have two hidden controls on your main form that reference the fields on your sub form and refer to them in your code.

David
 

Users who are viewing this thread

Back
Top Bottom