Hi
I am getting confused when I try and reference the parent and sub forms that I am wishing to extract text from. The code is part of a mail merge procedure that almost works (full code detailed at end of post)
The original code works okay for the parent and 1st subform but then stops. I have tried to help Access/me identify exactly which controls on what forms using the following code:
I've tried to do this as follows:
Exit Sub
End Sub
I am getting confused when I try and reference the parent and sub forms that I am wishing to extract text from. The code is part of a mail merge procedure that almost works (full code detailed at end of post)
The original code works okay for the parent and 1st subform but then stops. I have tried to help Access/me identify exactly which controls on what forms using the following code:
I've tried to do this as follows:
Code:
Dim frm As Form, sfrm1 As Form, sfrm2 As Form, sfrm3 As Form
Set frm = frm!forCustomerDetails2 ' Main Form
Set sfrm1 = sfrm1!forSiteName.Form ' subform level 1
Set sfrm2 = sfrm2!forJobTracking.Form ' subform level 2
Set sfrm3 = sfrm3!forProject2.Form ' subform level 3
resulting in:
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("CompanyName").Select
.Selection.Text = (CStr(Forms!frm.CompanyName))
.ActiveDocument.Bookmarks("SiteName").Select
.Selection.Text = (CStr(Forms!sfrm1!SiteName))
.ActiveDocument.Bookmarks("cboDesignation").Select
.Selection.Text = (CStr(Forms!sfrm1.Form!cboDesignation))
.ActiveDocument.Bookmarks("SiteAddress1").Select
.Selection.Text = (CStr(Forms!sfrm1.Form!SiteAddress1))
.ActiveDocument.Bookmarks("DateofComment").Select
.Selection.Text = (CStr(Forms!sfrm2!DateofComment))
I can't get it work and can't see why.
Any suggestions would be very welcome.
Regards
Steven811 - Novice
The code that works but stops after the 1st subform is as follows:
Private Sub MergeButton_Click()
On Error GoTo MergeButton_Err
Dim objWord As Word.Application
'Start Microsoft Word.
Set objWord = CreateObject("Word.Application")
With objWord
'Make the application visible.
.Visible = True
'Open the document.
.Documents.Open ("C:\MyMerge.doc")
Dim frm As Form, sfrm1 As Form, sfrm2 As Form, sfrm3 As Form
Set frm = frm!forCustomerDetails2 ' Main Form
Set sfrm1 = sfrm1!forSiteName.Form ' subform level 1
Set sfrm2 = sfrm2!forJobTracking.Form ' subform level 2
Set sfrm3 = sfrm3!forProject2.Form ' subform level 3
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("CompanyName").Select
.Selection.Text = (CStr(Forms!frm.CompanyName))
'Move to each bookmark and insert text from the form.
.ActiveDocument.Bookmarks("SiteName").Select
.Selection.Text = (CStr(Forms!sfrm1!SiteName))
.ActiveDocument.Bookmarks("cboDesignation").Select
.Selection.Text = (CStr(Forms!sfrm1.Form!cboDesignation))
.ActiveDocument.Bookmarks("SiteAddress1").Select
.Selection.Text = (CStr(Forms!sfrm1.Form!SiteAddress1))
.ActiveDocument.Bookmarks("DateofComment").Select
.Selection.Text = (CStr(Forms!sfrm2!DateofComment))
.ActiveDocument.Bookmarks("Comments").Select
.Selection.Text = (CStr(Forms!sfrm2!Comments))
.ActiveDocument.Bookmarks("Action").Select
.Selection.Text = (CStr(Forms!sfrm2!Action))
.ActiveDocument.Bookmarks("ActionByDate").Select
.Selection.Text = (CStr(Forms!sfrm2!ActionByDate))
.ActiveDocument.Bookmarks("ByWhom").Select
.Selection.Text = (CStr(Forms!sfrm2!ByWhom))
' 'Move to each bookmark and insert text from the form.
' .ActiveDocument.Bookmarks("DateofComment").Select
' .Selection.Text = (CStr(Me.forProject2.Form!DateofComment))
' .ActiveDocument.Bookmarks("JobNo").Select
' .Selection.Text = (CStr(Me.Form!forJobTracking2!JobNo))
' .ActiveDocument.Bookmarks("cboManager").Select
' .Selection.Text = (CStr(Me!forContactsJobTracking.Form!cboManager))
'
' .ActiveDocument.Bookmarks("Contact11stName").Select
' .Selection.Text = (CStr(Me!forContactsJobTracking.Form!Contact11stName))
'
' .ActiveDocument.Bookmarks("Contact12ndName").Select
' .Selection.Text = (CStr(Me!forContactsJobTracking.Form!Contact12ndName))
' Me.forProject2.Form.SetFocus
'Me.forProject2.SetFocus
'Me.forProject2!DateofComments.SetFocus
'
'
'Move to each bookmark and insert text from the form.
' .ActiveDocument.Bookmarks("DateofComment").Select
' .Selection.Text = (CStr(Forms!forProject2!DateofComment))
'
' .ActiveDocument.Bookmarks("Comments").Select
' .Selection.Text = (CStr(Forms!forProject2!Comments))
'
' .ActiveDocument.Bookmarks("Action").Select
' .Selection.Text = (CStr(Forms!forProject2!Action))
'
' .ActiveDocument.Bookmarks("ActionByDate").Select
' .Selection.Text = (CStr(Forms!forProject2!ActionByDate))
'
' .ActiveDocument.Bookmarks("ByWhom").Select
' .Selection.Text = (CStr(Forms!forProject2!ByWhom))
End With
'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
' objWord.ActiveDocument.PrintOut Background:=False
'Close the document without saving changes.
' objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'
' 'Quit Microsoft Word and release the object variable.
' objWord.Quit
' Set objWord = Nothing
' Exit Sub
MergeButton_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
Exit Sub
End Sub
Last edited by a moderator: