Updating a table from a form before the form is closed

G

Guest

Hi,

I have a data entry form which adds a new record to a table. I also have a
comand button which generates a report of the record just written.

The report is generated by finding the record in the table which matches the
information currently on the open form. However, the current record is not
added to the table until the form is closed.

Therefore, a blank report is being shown.

How can I get the form to update the table before I request the report?

Cheers
 
J

John W. Vinson

How can I get the form to update the table before I request the report?

Add a line before the OpenReport method:

If Me.Dirty Then Me.Dirty = False

John W. Vinson [MVP]
 
F

fredg

Hi,

I have a data entry form which adds a new record to a table. I also have a
comand button which generates a report of the record just written.

The report is generated by finding the record in the table which matches the
information currently on the open form. However, the current record is not
added to the table until the form is closed.

Therefore, a blank report is being shown.

How can I get the form to update the table before I request the report?

Cheers

Access doesn't save the record until you navigate to the next record,
close the form, or otherwise save the record.
You should explicitly save the record before printing.

DoCmd.RunCommand acCmdSaveRecord

Then you Open the report, using the record's unique prime key field as
record filter.
In my example it is named [RecordID].

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]

The above assumes a [RecordID] field that is a Number Datatype.

If, however, [RecordID] is Text Datatype, then use:

DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"

as the Where clause.

For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.
 
G

Guest

Hi John, Thanks for your help.

Unfortunately, I tried this and it didn't work. Maybe I placed the line
incorrectly. Here is the event proc for the Open Report comand

Private Sub cmdPrintDetentionLetter_Click()
On Error GoTo Err_cmdPrintDetentionLetter_Click

Dim stDocName As String

stDocName = "rptDetentionLetter"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdPrintDetentionLetter_Click:
Exit Sub

Err_cmdPrintDetentionLetter_Click:
MsgBox Err.Description
Resume Exit_cmdPrintDetentionLetter_Click

End Sub

Can you be more specific as to where the line should go? Sorry, I am quite
new to all this!

Cheers
 
J

John W. Vinson

Private Sub cmdPrintDetentionLetter_Click()
On Error GoTo Err_cmdPrintDetentionLetter_Click

Dim stDocName As String

stDocName = "rptDetentionLetter"
DoCmd.OpenReport stDocName, acPreview

Exit_cmdPrintDetentionLetter_Click:
Exit Sub

Err_cmdPrintDetentionLetter_Click:
MsgBox Err.Description
Resume Exit_cmdPrintDetentionLetter_Click

End Sub

Can you be more specific as to where the line should go? Sorry, I am quite
new to all this!

Right between the stDocName= line and the DoCmd.OpenReport line.

John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top