Vba Not Exporting Files Correctly (1 Viewer)

ponger

New member
Local time
Today, 07:49
Joined
Jul 18, 2017
Messages
3
I am trying to split my report into individual files using a specific naming format. I am able to run it correctly for the most part, however, whenever I add current date to the code it won't run anymore. I am getting a Run-Time error '2467'.

Can someone help me find out why my date code is not working??

PHP:
Option Compare Database

Private Sub Report_Click()


Dim rs As DAO.Recordset
Dim db As DAO.Database

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT DISTINCT PIN FROM ExcelImport ORDER BY PIN;", dbOpenSnapshot)

Dim rst As DAO.Recordset
Dim MyPath As String
Dim MyFilename As String
Dim PIN As String



If rs.RecordCount = 0 Then Exit Sub

Do While Not rs.EOF
PIN = rs![PIN]



strRptFilter = "PIN = " & Chr(34) & rs![PIN] & Chr(34)


MyFilename = "RESALL" & "_" & PIN & "_" & Format(Date,"yyyy-mm-dd") & "__" & Format(Date, "yyyy") & "_" & "WAIVER" & ".pdf"
MyPath = "C:\Users\aduong\Desktop\SWTEST\"

DoCmd.OpenReport "Report1", acViewPreview, , "PIN='" & rs!PIN & "'", acHidden
DoCmd.OutputTo acOutputReport, "Report1", acFormatPDF, MyPath & MyFilename

DoCmd.Close acReport, "Report1"

rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing


End Sub

Private Sub Report_Close()
strRptFilter = vbNullString
End Sub


Private Sub Report_Open(Cancel As Integer)
If Len(strRptFilter) <> 0 Then
     Me.Filter = strRptFilter
     Me.FilterOn = True
End If
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:49
Joined
Aug 30, 2003
Messages
36,124
What's the text of the error? What line does debug take you to?
 

isladogs

MVP / VIP
Local time
Today, 15:49
Joined
Jan 14, 2017
Messages
18,209
Error 2467
The expression you entered refers to an object that is closed or doesn't exist.@For example, you may have assigned a form to a Form object variable, closed the form, and then referred to the object variable.@@1@@@1

I should probably just wait for your response to Paul's questions.
However, I started replying before I saw that

A possible cause is Chr(34) which is the double quote

Also, it may not be relevant but try changing this line

Code:
MyFilename = "RESALL" & "_" & PIN & "_" & Format(Date,"yyyy-mm-dd") & "__" & Format(Date, "yyyy") & "_" & "WAIVER" & ".pdf"

to this

Code:
MyFilename = "RESALL" & "_" & PIN & "_" & Format(Date,"yyyymmdd") & "_" & Format(Date, "yyyy") & "_" & "WAIVER" & ".pdf"

which gets rid of hyphens and double underscores

Or to

Code:
MyFilename = "RESALL" & "_" & PIN & "_" & Format(Date,"yyyymmdd") & "_" & "WAIVER" & ".pdf"

which also removes the repeated year section

If none of that helps, it may be this line is wrongly placed though it looks OK to me
Code:
DoCmd.Close acReport, "Report1"
 

ponger

New member
Local time
Today, 07:49
Joined
Jul 18, 2017
Messages
3
The error message is:
"The expression you entered refers to an object that is closed or doesn't exist"

I updated my code with your recommendation Ridders but no luck. Thank you though! The double underscore and repeat of current year is needed because it correlates with the file formatting our importing program needs.

I forgot to mention that it would correctly export 1 file before the error message pops up.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:49
Joined
Aug 30, 2003
Messages
36,124
I'll get out of the way.
 

isladogs

MVP / VIP
Local time
Today, 15:49
Joined
Jan 14, 2017
Messages
18,209
Hi Paul
No please don't move aside ....
My answer was completely wrong which is what happens when I try to guess the cause!

@ponger (great name by the way)
Which line of code triggers the error?
 

Minty

AWF VIP
Local time
Today, 15:49
Joined
Jul 26, 2013
Messages
10,368
You aren't using strRptFilter in your report opening statement. So it keeps trying to save the same file name.
 

ponger

New member
Local time
Today, 07:49
Joined
Jul 18, 2017
Messages
3
Oh no Paul, don't move aside! Ridders, the line is:

MyFilename = "RESALL" & "_" & PIN & "_" & Format(Date, "yyyy-mm-dd") & "__" & Format(Date, "yyyy") & "_" & "WAIVER" & ".pdf"

Minty, how should I add it in?? Sorry, I am complete novice when it comes to VBA.
 

Minty

AWF VIP
Local time
Today, 15:49
Joined
Jul 26, 2013
Messages
10,368
Actually - you are setting it as a variable (strRptFilter), not using it , but creating the correct string again in the command - apologies I mis-read the original code.

I'll think I'll let Paul solve this - The Pub is open, and it's very hot and humid here. :p
 

Users who are viewing this thread

Top Bottom