Using MS Access/command buttons

Advertisement


Question
Hi Scott,

First of all, thank you so much for your help.  My database is going good so far.  I am stuck on one thing.  I have a form where the user enters a record, then at the bottom of the form there is a command button to print a report that contains the current record that was entered into the form.  This works perfectly but only if I save the record first.  If I don't save first, then the report is blank.  I would like the user to be able to print the report using only one button.  It just seems to be odd to have to click a save button and then a print button.  Can the save action and print current record action be easily achieved using only one command button?  Here is what I have so far..

Private Sub Command110_Click()
Dim strReportName As String
Dim strCriteria As String

If NewRecord Then
MsgBox "This Purchase Order Request contains no data."

Exit Sub

Else

strReportName = "REQUISITION"
strCriteria = "[PO_ID]='" & Me![PO_ID] & "'"
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
End If
End Sub

Thanks for any help you can provide me on this.
Carla

Answer
Sure. Access doesn't commit a record until the record loses focus or you consciously save it. That's why the report is blank, because, until its committed, the PO_ID for the currently displayed record isn't in the table yet.

Use the following. The Dirty property indicates whether there are any uncommitted changes in the current record. By setting it to False you force the changes to be committed. So you want to use that instead of testing for a NewRecord. This way when you choose to print, you get the most up to date record.

Private Sub Command110_Click()
Dim strReportName As String
Dim strCriteria As String

Me.Dirty = False

strReportName = "REQUISITION"
strCriteria = "[PO_ID]='" & Me![PO_ID] & "'"
DoCmd.OpenReport strReportName, acViewPreview, , strCriteria

End Sub

Hope this helps,
Scott<>
Blog: http://scottgem.wordpress.com
Website: http://www.diamondassoc.com
Microsoft Access MVP 2007
Author: Microsoft Office Access 2007 VBA

Using MS Access

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scottgem

Expertise

I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience

I've been designing databases for over 25 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

Education/Credentials
Brooklyn College BA

©2016 About.com. All rights reserved.