Access和Word 2010合并一条记录,具体取决于单击的子窗体按钮 [英] Access and Word 2010 merging one record depending on subform button clicked
问题描述
我正在开发一个小程序,在其中单击Access子窗体中的一个按钮时,需要在Word中创建一个字母. 该表单代表一个客户端,子表单中包含该客户端执行的命令列表.在每个命令行旁边(包含日期和描述),有一个按钮可触发maccro并创建字母.到目前为止,单击一个按钮后,我成功创建了单词letter,但是子窗体中的每个命令都在word文档中创建了一个页面.
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...
感谢您的帮助.
-编辑-
这里是代码,[Fusion]是我的SQL请求,它获取所有客户端以及与它们相关的订单.
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
推荐答案
您的问题尚不清楚,但是如果按钮在子窗体上,则可以使用
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")
修改
或Me.Parent.Controls("id_client")
在引用子表单时,必须首先引用父表单.
When referencing a subform you must reference the parent form first.
如果您只想从列表中打印1条命令,则您的SQL似乎需要更改以引用该命令.即
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.
这篇关于Access和Word 2010合并一条记录,具体取决于单击的子窗体按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!