Access and Word 2010 merging one record depending

2019-09-02 16:34发布

问题:

I'm working on a small program in which one letter in Word needs to be create when one button in a Access subform is clicked. The form represents one client and in the subform there are the list of commands done by this client. Next each command line (containing date and description), there is one button that trigger the maccro and create the letter. Until now, I succeed to create the word letter when one button is clicked but each command in the subform create a page in the word document.

Is it possible to keep only the command next to the button clicked and not all the command? I was looking for that kind of command :

"SELECT * FROM [Fusion]WHERE [id_client] = " & Forms!subform!id_client

but when I do it for the subform I have one error saying that the form doesn't exist...

Thanks for your help.

--EDIT--

Here is the code, the [Fusion] is my SQL request which get all the clients and the orders related to them.

Function Publipostage()

Dim mDoc As String
Dim strSQL As String
' Path of the letter
mDoc = "C:\...\LT000006.docx"
strSQL = "SELECT * FROM [Fusion]WHERE [id_client] = " & Forms!FormPatient!id_client

Dim oApp As New Word.Application
Dim oMainDoc As Word.Document
Dim sData As String

   oApp.Visible = True
      sData = "C:\...\Database1.accdb"

   Set oMainDoc = oApp.Documents.Open(mDoc)

      With oMainDoc.MailMerge
          .MainDocumentType = wdFormLetters
          .OpenDataSource Name:=sData, SQLStatement:=strSQL
      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

Set oApp = Nothing
Set oMainDoc = Nothing

Exit Function

Err_Handle:
   Set oApp = Nothing
   Set oMainDoc = Nothing
      MsgBox "An error occurred..." & vbCrLf & vbCrLf & Err.Description
End Function

回答1:

your question is a bit unclear but if the button is on the subform you can use

Me.id_client

if it is on the main form try

Forms("MAIN FORM NAME").Controls("SUB FORM NAME").Form.Controls("id_client")

Edit

Or Me.Parent.Controls("id_client")

When referencing a subform you must reference the parent form first.

If you only want to print 1 command from the list then it seems your SQL needs to change to reference that command. i.e.

"SELECT * FROM [Fusion] WHERE [id_command] = " & Me.id_command

This is just an example as I am unaware of your table structure.