Not a coder, needing assistance - ms-access

I am not a coder nor do it work in IT but I maintain an access database for our marketing reporting. This database has a VBA script in it that cycles through our report and creates separate excel files for each entity. I have been able to copy the exact code and change it out using new table and filter references without any issues.
I tried to do it again today and I am getting an error "Run-time error 3061 Too few parameters expected". I have been looking online and everyone seems to say that the error refers to incorrect field names or a missing quote but i can't for the life of me figure it out. I double checked the table and field names and I have tried numerous combinations of quotes with no luck. Sorry if this is a really stupid question but I am stumped.
Private Sub Command4_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Partners"
SQLstring = "SELECT DISTINCT [2-5 User Retest Partners].[Owner] FROM [2-5 User Retest Partners]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Owner")
FilterCriteria = "[2-5 User Retest Partners].[Owner] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Manager Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Partners", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub
This is the version of the code that runs:
Private Sub Command1_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Stores"
SQLstring = "SELECT DISTINCT [2-8 User Retest Stores].[Store Num] FROM [2-8 User Retest Stores]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Store Num")
FilterCriteria = "[2-8 User Retest Stores].[Clinic (Default)] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Store Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Stores", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub

Related

Creating Event Procedure in MS Access

I have tried to create the event procedure, but It returns zero irrespective of my selection.
I have two tables, which are correctly joined, and below is the code that has an issue.
First, "MsgBox Me.Technology" is returning my selection value eg. Python, Java, but "MsgBox rs!ProjEmployeeID" is returning 0 all times. Help me troubleshoot the code. Thank you. I want it to return Project Employee ID like 1, 2, 3
Option Compare Database
Private Sub Technology_AfterUpdate()
MsgBox Me.Technology
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT ProjEmployeeID FROM Project WHERE Technologies = Trim('" & Forms!Form_employee_by_technologies!Technology & "')")
rs.AddNew
MsgBox rs!ProjEmployeeID
Dim strDocName As String
Dim strWhere As String
strDocName = "Technology"
strWhere = "[EmployeeID] =" & rs!ProjEmployeeID
DoCmd.OpenReport strDocName, acViewReport, , strWhere, acWindowNormal
End Sub

How to Change Record Source with VBA in MS Access Report Macro

All,
In MS Access 2010, I have a table (Today's Settled Jrnls) that is linked to a report. I run the VBA code below to export the report to a pdf on a shared drive.
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Dan\" & Format(Date, "mm-dd-yyyy") & "\"
If Dir(mypath, vbDirectory) = "" Then MkDir mypath
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT distinct [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
This works but I'd like to change the [Today's Settled Jrnls] table to a different table [New Jrnls]. The new table is has the exact same columns and setup. However, when I change the table in the select statement above, the code runs but the report is blank. I assume this is because the report (Settlement Report) is still linked to the old table. Do you know how I can link the report to the new table with VBA?
Dan
Simply add a RecordSource call to point to new table after first opening report:
' OPEN REPORT
DoCmd.OpenReport "Settlement Report", acViewReport
' ADJUST SOURCE
Reports![Settlement Report].Report.RecordSource = "SELECT * FROM [New Jrnls]"
' FILTER AND OUTPUT
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
You can easily set the Reports recordsource property to a SQL String or bind it to the table
You can even use may of the events like open to set
Me.RecordSource ="SELECT * FROM TBL"
Here check this link: https://learn.microsoft.com/en-us/office/vba/api/access.report.recordsource
For Parfat:

DoCmd.OutputTo not working in MS Access 2010 VBA

I'm having a bit of trouble with VBA in MS Access 2010.
My code is attempting to create a new report for each unique Settlement No. I'm then exporting that report to a folder that's created with today's date and the PDF file is assigned a name with the Settlement No.
In the code below, I keep getting an error in the line with the DoCmd.OutputTo method.
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Settlement Reports\" & Format(Date, "mm-dd-yyyy") & "\"
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT dbs_eff_date, batch_id_r1, jrnl_name, ledger, entity_id_s1, account_s2, intercompany_s6, trans_amt, dbs_description, icb_name, [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
If I replace mypath & MyFileName with a hard-coded file path and file name, the macro works so I'm thinking that's where my error is but I can't get it to work correctly.
Any ideas?
Thanks!
Edit:
Here's the specific error:
Run-time error '2501': The OutputTo action was canceled.
You will want to check to make sure the folder exists first, and create it if it doesn't exist. Like this:
If Dir(mypath, vbDirectory) = "" Then MkDir mypath

How to split a report into multiple PDF files

Let's say I have an hundred page report in Access 2010 that includes lists of names (with some other details), grouped by a variable called NOM_RITIRO.
I would like to output the report into different PDF files, one for each value of the variable used for grouping.
I was trying to figure out how to make this code work:
Sub SplitPdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim Source As String
Dim SQL As String
Dim MyPath As String
Dim MyFilename As String
MyPath = "D:\Folder\"
Set db = CurrentDb
SQL = "Select NOM_RITIRO From QueryNominativi Group By NOM_RITIRO"
Set rs = db.OpenRecordset(SQL)
While Not rs.EOF
MyFilename = "TK_" & rs!NOM_RITIRO & ".pdf"
' Apply quotes as NOM_RITIRO is a string.
DoCmd.OpenReport "ElenchiNominativi", acViewPreview, , "NOM_RITIRO = '" & rs!NOM_RITIRO.Value & "'"
DoCmd.OutputTo acOutputReport, , acFormatPDF, MyPath & MyFilename, False
DoCmd.Close acReport, "ElenchiNominativi"
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
I got stuck when I try to run the DoCmd.OpenReport.
I get the message box "Enter parameter Value" like if the recordset is not passing any data
Any idea of what I did wrong?
If NOM_RITIRO is not a field in the recordsource of your report called "ElenchiNominativi" (or mis-spelled) then it will prompt you to enter a parameter value.
Similarly, if the recordsource of your report is a query that contains a fieldname not referenced in any of the tables, you will also get this prompt.

Print Reports to PDF in Access 2013 but use a looping filter

I need to print the same report over and over again but with a different name for each report.My reports are printing blank. Not certain how to get code to loop through names in a table and to use those names as a filter for the report. See Code below. Seems to be and issue with rec2.
Option Compare Database
Option Explicit
Sub PrintSingleRepPerPgDailyReportToPDF()
On Error GoTo PrintToPDF_Err
Dim dadb As DAO.Database
Dim rec1 As DAO.Recordset
Dim rec2 As DAO.Recordset
Dim MyFilter As String
Dim MyPath As String
Dim MyFilename As String
Set dadb = CurrentDb
Set rec1 = dadb.OpenRecordset("tblSalesPPL", dbOpenTable)
Do While rec1.EOF = False
Set rec2 = dadb.OpenRecordset("Select [Rep Name] from tblSalesPPL")
MyFilter = "(((tblSales2.Rep)='rec2'))"
MyPath = "C:\Users\Tallahassee Client\Documents\Reports\Reports Daily\" & "AB_"
MyFilename = Month(Now) & "." & Day(Now) & "." & Year(Now) & ".pdf"
DoCmd.OpenReport "rptSales3_SingleRepPerPg_DailyReport_2", acViewPreview, "qrySales3", MyFilter
DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, True
DoCmd.Close acReport, "rptSales3_SingleRepPerPg_DailyReport_2"
PrintToPDF_Exit:
Exit Sub
PrintToPDF_Err:
MsgBox Error$
Resume PrintToPDF_Exit
Set rec2 = Nothing
rec1.MoveNext
Loop
End Sub
If you add Debug.Print MyFilter after MyFilter is defined, you will see that you are filtering the report to match the literal text rec2, not to match the value of the variable named rec2. Change your filter line to:
and see if you have better results.
Set dadb = CurrentDb
Set rec1 = dadb.OpenRecordset("tblSalesPPL", dbOpenTable)
Do While rec1.EOF = False
MyFilter = "Rep='" & Replace(rec1![Rep Name], "'", "''") & "'"
MyPath = "C:\Users\Tallahassee Client\Documents\Reports\Reports Daily\" & "AB_"
MyFilename = Month(Now) & "-" & Day(Now) & "-" & Year(Now) & ".pdf"
DoCmd.OpenReport "rptSales3_SingleRepPerPg_DailyReport_2", acViewPreview, "qrySales3", MyFilter
DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, True
DoCmd.Close acReport, "rptSales3_SingleRepPerPg_DailyReport_2"
rec1.MoveNext
Loop
Set rec1 = Nothing
so that you have a single loop that calls OpenReport once for every record in tblSalesPPL, using the current value of [Rep Name] as the filter for the report's Rep field.