This is what I have:
Private Sub EthosRpt_Click()
DoCmd.OpenQuery "EthosSessions"
DoCmd.TransferText acExportDelim, , "EthosSessions", "C:\Users\JDoe\Desktop\EthosRpt.csv", True
End Sub
It works with my user account (where my user account = JDoe). How do I get it to work for ANY current user?
try "%USERPROFILE%\Desktop\EthosRpt.csv"
like:
DoCmd.TransferText acExportDelim, , "EthosSessions", "%USERPROFILE%\Desktop\EthosRpt.csv", True
Try with:
Private Sub EthosRpt_Click()
Dim FileName As String
DoCmd.OpenQuery "EthosSessions"
FileName = Environ("UserProfile") & "\Desktop\EthosRpt.csv"
DoCmd.TransferText acExportDelim, , "EthosSessions", FileName, True
End Sub
first get the username as string and replace the username
Dim strDisplayName as string
Set objAD = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objAD.UserName)
strDisplayName = objUser.DisplayName
than
DoCmd.TransferText acExportDelim, ,
"EthosSessions","C:\Users\" & strDisplayName & "\Desktop\EthosRpt.csv", True
Related
Help me, this code I wrote, creates multiples files but every file contains only the values from the first record:
I want a file pdf for each record in the table.
Option Compare Database
Option Explicit
Private Declare PtrSafe Sub
Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Sub Creazione_Click()
Dim rs As DAO.Recordset
Dim rpt As Access.Report
Dim sFolder As String
Dim sFile As String
Const sReportName = "Mandato"
sFolder = "C:\Users\Famiglia\Desktop\Mandati\"
Set rs = Me.RecordsetClone
With rs
If .RecordCount <> 0 Then
'Open the Report
DoCmd.OpenReport sReportName, acViewPreview, , , acHidden
'Define a report object so we can manipulate it below
Set rpt = Reports(sReportName).Report
.MoveFirst
Do While Not .EOF
'Build the PDF filename we are going to use to save the PDF with
sFile = Nz(![id], "") & " " & Nz(![COGNOME], "") & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFile, , , , acExportQualityPrint
Call Sleep(1000)
DoEvents
.MoveNext
Loop
DoCmd.Close acReport, sReportName
End If
End With
End Sub
Open, output, close report filtered to record within loop.
Do While Not .EOF
DoCmd.OpenReport sReportName, acViewPreview, , "ID=" & rs!ID, acHidden
...
DoCmd.Close acReport, sReportName
.MoveNext
Loop
A report object variable is not necessary. Your code doesn't even use the variable.
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
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
I'm trying to get pdf file by tenant_id using docmd.outputTo. Unfortunately this routine produce output pdf file that all are in a same single tenant_id. If I remove docmd.outputTo last parameter pathName & fileName then it's require file name through dialogue and output file nicely filtered by tenant_id. Any help would be appreciated.
Here's the query of invoice: SELECT * FROM tblInvoice WHERE tenant_id = CurTenantID()
Public Sub Output()
Dim MyRs As DAO.Recordset
Dim rpt As Report
Dim fileName As String, pathName As String, todayDate As String
pathName = "C:\Users\abzalali\Dropbox\tenant_db\Invoice\"
todayDate = Format(Date, "MMDDYYYY")
Set MyRs = CurrentDb.OpenRecordset("SELECT tenant_id, name, company, email FROM qryEmailClientList")
DoCmd.OpenReport "Invoice", acPreview, , , acHidden
Set rpt = Reports("Invoice")
With MyRs
.MoveFirst
Do While Not .EOF
fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"
rpt.Filter = "[tenant_id] = " & !tenant_id
rpt.FilterOn = True
DoCmd.OutputTo acOutputReport, "Invoice", acFormatPDF, pathName & fileName
.MoveNext
Loop
End With
End Sub
Simply use the DoCmd.OpenReport method to filter report and then leave the report name blank in DoCmd.OutputTo as per the docs:
ObjectName > Optional > Variant: If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank.
With MyRs
.MoveFirst
Do While Not .EOF
fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"
DoCmd.OpenReport "Invoice", acViewReport, , "[tenant_id] = " & !tenant_id, acHidden
DoCmd.OutputTo acOutputReport, , acFormatPDF, pathName & fileName
.MoveNext
Loop
End With
Since DoCmd.OutputTo lacks parameters for filtering, the best option is to base the report on a public function to get the current ID.
E.g.
' Function to both set and retrieve the current Tenant ID
Public Function CurTenantID(Optional SetTenantID As Long = 0) As Long
Static TenantID As Long
If SetTenantID > 0 Then
TenantID = SetTenantID
End If
CurTenantID = TenantID
End Function
Your report uses this function in its record source, e.g.
SELECT * FROM tblInvoice WHERE tenant_id = CurTenantID()
And when generating the PDF reports in the loop, you use the SetTenantID parameter:
Public Sub Output()
Dim MyRs As DAO.Recordset
Dim fileName As String, pathName As String, todayDate As String
pathName = "C:\Users\abzalali\Dropbox\tenant_db\Invoice\"
todayDate = Format(Date, "MMDDYYYY")
Set MyRs = CurrentDb.OpenRecordset("SELECT tenant_id, name, company, email FROM qryEmailClientList")
With MyRs
' .MoveFirst -- unneeded after OpenRecordset()
Do While Not .EOF
fileName = "Invoice_" & todayDate & !tenant_id & ".pdf"
Call CurTenantID(!tenant_id)
DoCmd.OutputTo acOutputReport, "Invoice", acFormatPDF, pathName & fileName
.MoveNext
Loop
End With
End Sub
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.