Solved

MS Acccess - Update Field in Table when report is printed

Posted on 2016-09-29
6
53 Views
Last Modified: 2016-09-30
Hi,

Seems very simple but I can't seem to make it work properly.
I have a macro created for me which exports reports as pdf files. WHich works great.

However I'd like field ("Printed") in my "Contracts" table to be updated with "Yes" when the report is exported.

Here's the existing code:

Private Sub Command31_Click()
DoCmd.RunCommand acCmdSaveRecord
Forms!Form2.SetFocus
Dim oApp As Object
Dim oEmail As Outlook.MailItem
Dim fileName As String
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT [Account], [InvoiceNum], [EmailAddress], [Company] FROM [ContactTotalsNoEmail] WHERE (((Contracts.SelectedPrint)=True)) ORDER BY [Account];", dbOpenSnapshot)

Do While Not rst.EOF
    strRptFilter = "[InvoiceNum] = " & Chr(34) & rst![InvoiceNum] & Chr(34)
    fileName = "C:\Scripts" & "\" & rst![Account] & " - " & rst![InvoiceNum] & ".pdf"
    DoCmd.OutputTo acOutputReport, "InvTotalNoEmail", acFormatPDF, fileName
    DoEvents
     rst.MoveNext
   Loop
rst.Close
Set rst = Nothing
Forms!Form2.Requery
End Sub

Open in new window


I couldn't fingure it with rst, so tried to create rst2 but it would not update the correct records.
rst2.Edit
rst2!Print = Yes
rst2.Update
rst2.MoveNext

Open in new window


Can anyone assist?

Many thanks.
0
Comment
Question by:Jack Marley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:PatHartman
ID: 41821928
Private Sub Command31_Click()
DoCmd.RunCommand acCmdSaveRecord
Forms!Form2.SetFocus
Dim oApp As Object
Dim oEmail As Outlook.MailItem
Dim fileName As String
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT [Account], [InvoiceNum], [EmailAddress], [Company] FROM [ContactTotalsNoEmail] WHERE (((Contracts.SelectedPrint)=True)) ORDER BY [Account];", dbOpenSnapshot)

Do While Not rst.EOF
    strRptFilter = "[InvoiceNum] = " & Chr(34) & rst![InvoiceNum] & Chr(34)
    fileName = "C:\Scripts" & "\" & rst![Account] & " - " & rst![InvoiceNum] & ".pdf"
    DoCmd.OutputTo acOutputReport, "InvTotalNoEmail", acFormatPDF, fileName
    rst.Edit
    rst!Print = True
    rst.Update
    DoEvents
     rst.MoveNext
   Loop
rst.Close
Set rst = Nothing
Forms!Form2.Requery
End Sub

Open in new window

I changed the Print set statement to use True rather than Yes.  If the Print field is actually text, then it should be  -- rst!Print = "Yes"
0
 

Author Comment

by:Jack Marley
ID: 41821944
Thanks PatHartman,

However that gives me a "Operation is not supported for this type of Object".

Do you know what might cause this?
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 41821952
The query is not updateable.  Remove the DISTINCT predicate.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Jack Marley
ID: 41822009
Still errors I'm afraid, on line
    rst.Edit

Open in new window

0
 
LVL 38

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41822204
You have it opened as Snapshot.  Shapshot is read only.  Use Dynaset.
1
 

Author Comment

by:Jack Marley
ID: 41823181
Thanks Phil!

I also noticed I hadn't even specified the 'Print' field in the SELECT! Then I put the field in square brackets to prevent any disambiguation.

rst![Printed] = "Yes"

Open in new window


Thanks again!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question