Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 hours ago.
Improve this question
When working with some 30 test records all is fine. I filter a recordset and create PDF files. When using the production data, the filter returns 100 records and creates SOME PDFs - varying from 1 to 7 to 20, but not all.
It just stops working, the task manager shows no activity any more.
I only think there is an issue with objects not closed but I have no idea.
Dim rst As DAO.Recordset
Dim rstFiltered As DAO.Recordset
Dim rpt As Access.Report
dim strRptName As String
Dim sFolder As String 'the output folder with PDFs
Dim sFilePDF As String 'complete path of the *.PDF file
Const gcAppQry53312 as string = "Qry53312"
Set rst = CurrentDb.OpenRecordset(gcAppQry53312, dbOpenSnapshot)
var_rpt_WHERE = ....
rst.Filter = var_rpt_WHERE
sFolder = "C:\test\"
Set rstFiltered = rst.OpenRecordset(dbOpenSnapshot)
With rstFiltered
.MoveFirst
Do While Not .EOF
DoCmd.OpenReport ReportName:=strRptName _
, View:=acViewPreview _
, Filtername:="" _
, WhereCondition:="" _
, WindowMode:=acHidden
Set rpt = Reports(strRptName).Report
sFilePDF = ![NVNameID] & ".pdf"
sFilePDF = sFolder & sFilePDF
'=*=*=*=*
DoEvents
'=*=*=*=*
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, sFilePDF, , , , acExportQualityPrint
'-- 13. finis
Set rpt = Nothing
DoCmd.Close acReport, strRptName
.MoveNext
Loop
End With
If Not rpt Is Nothing Then Set rpt = Nothing
If Not rstFiltered Is Nothing Then
rstFiltered.Close
Set rstFiltered = Nothing
End If
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
instead of using .EOF, if you have a way to dynamically identify the document count you need at any given time you could create this value as a dimention then loop on an incranemtal counter within the loop (also a dimention) vs the dynamically identify document count, this should work.
Try to reduce this to the essential part:
Do While Not .EOF
sFilePDF = ![NVNameID] & ".pdf"
sFilePDF = sFolder & sFilePDF
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, sFilePDF, False, , , acExportQualityPrint
.MoveNext
Loop
The error was: in ![NVNameID] exists a record with a "/".
Solution:
Dim strNVNameID As String
strNVNameID = Nz(![NVNameID], "")
strNVNameID = Replace(strNVNameID, "/", "_")
Related
I realise this is probably the simplest question ever but I'm about to tear my hair out.
I am trying to export an access report for a single record in my data table, as specific by the field Unique ID.
I keep getting thrown a run time error occurring in the DoCmd.OpenReport line. For the life of me I cannot figure out where or why this is happening.
In a perfect world, I would want the user to be able to enter as many Unique ID's as they want, and it would export a single report for each line (not a large report containing all rows).
Sub test()
Dim MyFileName As String
Dim rst As DAO.Recordset
Dim strReportName As String ' report name to work with
Dim strOutPutFile As String ' pdf output file on disk
strReportName = "TEST"
Set rst = CurrentDb.OpenRecordset("table")
Do While rst.EOF = False
strOutPutFile = "C:\Users\me\Documents" & "-test1-" & rst![Unique ID] & ".pdf" ' output file = ID.pdf
DoCmd.OpenReport "TEST", acViewPreview , , "Unique ID = " & rst![Unique ID]
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strOutPutFile
DoCmd.Close acReport, strReportName
rst.MoveNext
Loop
rst.Close
MsgBox "done"
End Sub
I have a recordset that I'm looping through and would like to create a report that displays information for each user ID in the recordset. I've found many posts that have helped me to write the code, but I cannot figure out why my code keeps opening the report with all user IDs instead of each user ID individually. Here's the code I'm using:
Public Function report()
Dim rs As Recordset
Dim strReportName As String
Dim fileName, pathName As String
pathName = "C:\Users\Joe\Documents"
Set rs = CurrentDb.OpenRecordset("SELECT Add_user, keyer FROM qryProductionReport;")
strReportName = "ProductionReport"
Do While Not rs.EOF
DoCmd.OpenReport strReportName, acViewPreview, , "Add_user = " & rs!Add_user, acHidden
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, pathName & strReportName & rs!Keyer & ".PDF"
DoCmd.Close acReport, strReportName
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Function
I made sure that my field name Add_user on the report matches the filter that I have in the DoCmd.OpenReport line. I'm using Access 2016. Thanks for the help.
One way is to modify your report to use a TempVars criteria
(e.g. [TempVars]![userId]) as explained here.
You can then use it like this:
Public Function report()
Dim rs As Recordset, strReportName As String
Dim fileName as String, pathName As String
pathName = "C:\Users\Joe\Documents\"
Set rs = CurrentDb.OpenRecordset("SELECT Add_user, keyer FROM qryProductionReport;")
strReportName = "ProductionReport"
Do While Not rs.EOF
TempVars("userId") = rs!Add_user
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, pathName & strReportName & rs!Keyer & ".PDF"
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Function
I can't seem to understand what I've done wrong here. I'm getting an error 3265 (Item not found in this collection) at the three lines starting with "qdf.Parameters..." My understanding is that I define the where clause of my sql statement here, but maybe I'm wrong? Pretty new to vba with access so a little confused.
Sub Save_Invoices_Meet_Criteria()
Dim FileName As String
Dim FilePath As String
Dim myStmt As String
Dim Db As DAO.Database
Dim myrs As DAO.Recordset
Set Db = CurrentDb()
Dim qdf As DAO.QueryDef
Set qdf = Db.QueryDefs("qryCreateInvoicesApproved")
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![Invoice_approved]") = [Forms]![frmAccountingDatabaseInput]![Invoice_approved]
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![invoice_date]") = [Forms]![frmAccountingDatabaseInput]![Combo272]
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![Invoice_Type}") = [Forms]![frmAccountingDatabaseInput]![Combo274]
Set myrs = CurrentDb.OpenRecordset("SELECT distinct [reference] from qryCreateInvoicesApproved", 2)
Do Until myrs.EOF
FileName = Me.reference
foldername = Format(Now(), "YYYY-MM-DD")
FilePath = "C:\Users\company\Desktop\Invoicing Database\Save_Test\" & foldername & "\" & FileName & ".pdf"
DoCmd.OpenReport "RPTInvoice", acFormatPDF, FilePath
'DoCmd.OutputTo acOutputReport, , acFormatPDF, FilePath
DoCmd.Close
myrs.MoveNext
Loop
myrs.Close
Set myrs = Nothing
End Sub
My sql statement:
SELECT tblAccountingDatabase.*
FROM tblAccountingDatabase
WHERE (((tblAccountingDatabase.Invoice_approved)=Yes) And ((tblAccountingDatabase.invoice_date)=Forms!frmAccountingDatabaseInput!Combo272) And ((tblAccountingDatabase.Invoice_Type)=Forms!frmAccountingDatabaseInput!Combo274));
Simply add a PARAMETERS line at the beginning of your stored query which you then reference in the VBA querydef object. Then use the Querydef.OpenRecordset() method to pass parameterized query into a recordset object. Right now you are passing named parameters that do not exist:
SQL
PARAMETERS [Approveparam] YesNo, [Dateparam] Datetime, [Typeparam] String;
SELECT DISTINCT [reference]
FROM tblAccountingDatabase
WHERE (((tblAccountingDatabase.Invoice_approved) = [Approveparam])
AND ((tblAccountingDatabase.invoice_date) = [Dateparam])
AND ((tblAccountingDatabase.Invoice_Type) = [Typeparam]));
VBA
...
Dim qdf As DAO.QueryDef
Set qdf = Db.QueryDefs("qryCreateInvoicesApproved")
qdf!Approveparam = [Forms]![frmAccountingDatabaseInput]![Invoice_approved]
qdf!Dateparam = [Forms]![frmAccountingDatabaseInput]![Combo272]
qdf!Typeparam = [Forms]![frmAccountingDatabaseInput]![Combo274]
Set myrs = qdf.OpenRecordset()
...
To pass parameters to a form/report/macro that uses the same paramterized query use DoCmd.SetParameter method. And yes, you need to wrap every value with quotes hence the quote escaping. Also use DoCmd.OutputTo to convert report to PDF:
DoCmd.SetParameter "Approveparam", _
"""" & [Forms]![frmAccountingDatabaseInput]![Invoice_approved] & """"
DoCmd.SetParameter "Dateparam", _
"""" & [Forms]![frmAccountingDatabaseInput]![Combo272] & """"
DoCmd.SetParameter "Typeparam", _
"""" & [Forms]![frmAccountingDatabaseInput]![Combo274] & """"
DoCmd.OpenReport "RPTInvoice", acViewPreview
DoCmd.OutputTo acOutputReport, "RPTInvoice", acFormatPDF, FilePath
It kind of looks like you're trying to force yourself to use a parameter query but not really committed to it. If you don't want to truly use one you can change your SQL structure to use generic parameter names - and then use the qdf.Parameters method to fill the values from your form.
But I think this is the easiest for what you have now.
Replace these lines:
Dim qdf As DAO.QueryDef
Set qdf = Db.QueryDefs("qryCreateInvoicesApproved")
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![Invoice_approved]") = [Forms]![frmAccountingDatabaseInput]![Invoice_approved]
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![invoice_date]") = [Forms]![frmAccountingDatabaseInput]![Combo272]
qdf.Parameters("[Forms]![frmAccountingDatabaseInput]![Invoice_Type}") = [Forms]![frmAccountingDatabaseInput]![Combo274]
Set myrs = CurrentDb.OpenRecordset("SELECT distinct [reference] from qryCreateInvoicesApproved", 2)
With this line to open your recordset
Set myrs = qdf.OpenRecordset("SELECT * from qryCreateInvoicesApproved", 2)
Change your query to:
SELECT DISTINCT [reference]
FROM tblAccountingDatabase
WHERE (tblAccountingDatabase.Invoice_approved=[Forms]![frmAccountingDatabaseInput]![Invoice_approved])
AND (tblAccountingDatabase.invoice_date=Forms!frmAccountingDatabaseInput!Combo272)
AND (tblAccountingDatabase.Invoice_Type=Forms!frmAccountingDatabaseInput!Combo274);
I have a database with 5 tables, 3 queries, 3 reports (the queries are the recordsets) and three reports each showing the several fields on the recordsets. The problem is, even though they have the same code, one of the sub routines has inconsistent results. It is like it is cycling through each supervisor and creating a report and then doing it again, it's caught in a loop and I can't see where the issue is. Hoping someone can help.
Private Sub cmdFedInvest_Click()
Dim x As String
Dim y As String
Dim StrSQL As String
Dim stWhereStr As String 'Where Condition'
Dim stSection As String 'Selection from drop down list
Dim stfile As String
Dim stDocName As String
Dim StrEmail As String
StrSQL = "SELECT DISTINCTROW [qryActT8_Sup].[Sup], [qryActT8_Sup].Sup_email " & _
"FROM [qryActT8_Sup];"
y = Year(Date)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim qdTemp As DAO.QueryDef
Set qdTemp = db.CreateQueryDef("", StrSQL)
Set rst = qdTemp.OpenRecordset()
If rst.EOF And rst.BOF Then
MsgBox "No data available for the Ledger Process routine."
Else
Debug.Print rst.Fields.Count
rst.MoveFirst
Do While Not rst.EOF
x = rst![Sup]
StrEmail = rst![Sup_email]
stDocName = "FedInvest - ISSR - T8 Recertification Report"
stWhereStr = "[qryActT8_Sup].[SUP]= '" & x & "'"
stfile = Me.txtLocationSaveFI & "\" & x & " - " & y & " FedInvest Recertification.pdf"
DoCmd.OpenReport stDocName, acPreview, , stWhereStr
'DoCmd.SendObject acSendReport, stDocName, acFormatPDF, StrEmail, , , "2016 FedInvest Recertification", ""
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stfile
DoCmd.Close acReport, stDocName
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End Sub
You both open the report for preview and for output to PDF.
If only PDF is needed, skip the preview.
I am looking to optimize and rewrite some code to loop a bunch of mail merges that run on command click. Right now, there exists 8 individual merges that run after each other. My problem is that I run 8 queries, which all require a start/end date to pull information from a table and feed it over to the mail merge document. I would like to specify the parameter date, through say, inputbox and then pass it to all the merges in the loop.
For i = 1 To 1 'will change to 8 :)
Select Case i
Case 1
wordDoc = pathToDocToMerge
sqlStr = "SELECT * FROM [QUERY - FIRST LETTERS]"
sqlConex = "QUERY - FIRST LETTERS"
strExport = "Normal"
fileout = desktopPath & "\ " & strmonth & " - FIRSTLETTERS.pdf"
Case 2
Case 3
Case 4
Case 5
Case 6
Case 7
Case 8
End Select
Set objword = GetObject(wordDoc)
objword.Application.Visible = False
objword.MailMerge.OpenDataSource _
Name:=pathToMDB, _
LinkToSource:=True, _
Connection:=sqlConex, _
sqlstatement:=sqlStr
objword.MailMerge.Destination = wdSendToNewDocument
objword.MailMerge.Execute
If printtopdf = vbYes Then
objword.Application.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
fileout _
, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument
objword.Application.ActiveDocument.close SaveChanges:=wdDoNotSaveChanges
Else
objword.Application.Options.PrintBackground = False
'Print one copy of the letter
objword.Application.ActiveDocument.PrintOut
'Close Word and do not save the changes
End If
objword.Application.Quit SaveChanges:=wdDoNotSaveChanges
Set objword = Nothing
Next i
I know parameters can be passed like so:
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_AMIS")
For Each prm In qdf.Parameters
prm = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
But I am unsure how to tie this into a mail merge, seeing as the query becomes RST. Can anyone shed some light?
After a quick look at the documentation for MailMerge.OpenDataSource here, I don't see why you couldn't just change your existing code...
sqlStr = "SELECT * FROM [QUERY - FIRST LETTERS]"
...to something like...
sqlStr = "SELECT * FROM [QUERY - FIRST LETTERS] WHERE SomeDate=#" & Format(CDate(Me.txtSpecifyDate.Value), "yyyy-mm-dd") & "#"
...where Me.txtSpecifyDate is a text box on the form you use to prompt the user to supply the date.