Solved

MS Acccess - Update Field in Table when report is printed

Posted on 2016-09-29
6
38 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
  • 3
  • 3
6 Comments
 
LVL 34

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 34

Expert Comment

by:PatHartman
ID: 41821952
The query is not updateable.  Remove the DISTINCT predicate.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

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

Open in new window

0
 
LVL 34

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now