标题: Print the record in the form [打印本页] 作者: jsy165 时间: 2005-9-28 22:59 标题: Print the record in the form 文章标题:Print the record in the form
文章来源:http://allenbrowne.com/casu-15.html
原文:
How do you print just the one record you are viewing in the form?
Create a report, to get the layout right for printing. Use the primary key value that uniquely identifies the record in the form, and open the report with just that one record.
The stepsOpen your form in design view.
If you do not see the toolbox, open it from the View menu.
Click the command button in the toolbox, and click on your form.
If the wizard starts, cancel it. It will not give you the flexibility you need.
Right-click the new command button, and choose Properties. Access opens the Properties box.
On the Other tab, set the Name to something like: cmdPrint
On the Format tab, set the Caption to the text you wish to see on the button, or the Picture if you would prefer a printer or preview icon.
On the Event tab, set the On Click property t [Event Procedure]
Click the Build button (...) beside this. Access opens the code window.
Paste the code below into the procedure. Replace ID with the name of your primary key field, and MyReport with the name of your report. The codePrivate Sub cmdPrint_Click()
Dim strWhere As String
If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If
If Me.NewRecord Then 'Check there is a record to print
代码:Private Sub cmdPrint_Click() Dim strWhere As String If Me.Dirty Then 'Save any edits. Me.Dirty = False End If If Me.NewRecord Then 'Check there is a record to print MsgBox "Select a record to print" Else strWhere = "[ID] = " & Me.[ID] DoCmd.OpenReport "MyReport", acViewPreview, , strWhere End IfEnd Sub注意:
1. 如果你的猪键是文本类型(不是数字类型),你需要特别的引用:
strWhere = "[ID] = """ & Me.[ID] & """"
2. 报表打开后不适合已经打开的。
3. 如果你需要打印没打印预览,替换acViewPreview with acViewNormal.