Access and Word 2010 merging one record depending on subform button clicked

和自甴很熟 提交于 2019-12-13 04:38:22

问题


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.



来源:https://stackoverflow.com/questions/20424161/access-and-word-2010-merging-one-record-depending-on-subform-button-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!