RCheesley
Registered User.
- Local time
- Today, 13:02
- Joined
- Aug 12, 2008
- Messages
- 243
Hi all,
Just wanted to check something, I'm planning to use the mail merge solution posted here but need to run the merge ONLY for the particular record active in the form from which the button is pressed.
The VBA code on the article above incorporates an SQL statements:
The form is running from a table called tbl_sampleinfo with the "create letter" button sitting on a form called frm_sampleinfo.
I want the mail merge to use a query called qry_samples (as there are fields from other tables I need to incorporate and thought this would be the best way of accomplishing it). I have set up the mail merge template using the fields from this query and formatted it accordingly.
I assume I need to use a statement which says (in laymans terms) "Select records from qry_samples for NHSNo stated on frm_sampleinfo" but I am not sure on the syntax? I am taking a big guess here but would the following be anywhere near correct?
I tried this and get errors about onload/onclick saying "user-defined type not defined" :|
Further info on error above - VBA code as follows:
Just wanted to check something, I'm planning to use the mail merge solution posted here but need to run the merge ONLY for the particular record active in the form from which the button is pressed.
The VBA code on the article above incorporates an SQL statements:
Code:
SQLStatement:="SELECT * FROM [tblcompanies]"
I want the mail merge to use a query called qry_samples (as there are fields from other tables I need to incorporate and thought this would be the best way of accomplishing it). I have set up the mail merge template using the fields from this query and formatted it accordingly.
I assume I need to use a statement which says (in laymans terms) "Select records from qry_samples for NHSNo stated on frm_sampleinfo" but I am not sure on the syntax? I am taking a big guess here but would the following be anywhere near correct?
Code:
"SELECT * FROM [qry_samples] WHERE NHSNo=[frm_sampleinfo]![NHSNo]"
Further info on error above - VBA code as follows:
Code:
Option Compare Database
Dim WithEvents oApp As Word.Application
Private Sub But_Conslet_Click()
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("<my UNC Path>\Test Letter")
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "<my UNC Path>\<databasename>.mdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [qry_samples] WHERE NHSNo=[frm_sampleinfo]![NHSNo]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visible = True
oApp.Application.WindowState = 1
oApp.ActiveWindow.WindowState = 1
End Sub
Private Sub Form_Load()
Set oApp = CreateObject("Word.Application")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set oApp = Nothing
End Sub
Last edited: