Run Time Error '2501'

Falcon88

Registered User.
Local time
Today, 06:56
Joined
Nov 4, 2014
Messages
309
hiiiiiii all

i have report need to save it by creating Yearly folders and monthly subfolders.

i try to use this code :
Code:
Private Sub apdfNew1_Click()
Dim strFolderYear As String
Dim strFolderMonth As String
Dim myFileNx As String
Dim lngPK As Long 'change data type if yours is different
'set variable to current record ID
lngPK = Me.OrderID
Me.Requery
'return form to original record
With Me.RecordsetClone
  .FindFirst "OrderID = " & lngPK
'  'if your key field is text use this line instead
'  '.FindFirst "OrderID = " & Chr(34) & lngPK & Chr(34)
'  If .NoMatch Then 'just in case another user deleted it in the interim
'    MsgBox "Record not found!", vbCritical
'  Else 'go to that record
    Me.Bookmark = .Bookmark
'  End If
End With
strFolderName = CurrentProject.Path & "\" & Format(Me.OrderDate, "yyyy") & "\"  ' folder to save
strFolderMonth = strFolderName & Format(Me.OrderDate, "mmmm yyyy") & "\"      ' folder to save
 If Len(Dir(strFolderYear, vbDirectory)) = 0 Then
     MkDir (strFolderYear)
     If Len(Dir(strFolderMonth, vbDirectory)) = 0 Then
     MkDir (strFolderMonth)
     End If
 End If
myFileNx = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderMonth & myFileNx, True
DoCmd.RunMacro "RptClose"
'Call Shell("explorer.exe " & str_folder, vbNormalFocus)
End Sub

but it gives me Run Time Error '2501' in the line:

Code:
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderMonth & myFileNx, True

please help me to get solution
 
Does the folder exist?

If it does can you write to it?

To check, open the folder in explorer and create a text document and save it...
 
I think you may have the same folder called both

strFolderName and strFolderYear.

Check those, and try again.

You ought to have option explicit to catch such typos.
 
ooooooooh thanks i try this , it works good:
Code:
Dim strFolderYear As String
Dim strFolderMonth As String
Dim myFileNx As String



Dim lngPK As Long 'change data type if yours is different

'set variable to current record ID
lngPK = Me.OrderID

Me.Requery
'return form to original record
With Me.RecordsetClone
  .FindFirst "OrderID = " & lngPK
'  'if your key field is text use this line instead
'  '.FindFirst "OrderID = " & Chr(34) & lngPK & Chr(34)
'  If .NoMatch Then 'just in case another user deleted it in the interim
'    MsgBox "Record not found!", vbCritical
'  Else 'go to that record
    Me.Bookmark = .Bookmark
'  End If
End With




strFolderYear = CurrentProject.Path & "\" & Format(Me.OrderDate, "yyyy")   ' folder to save
strFolderMonth = strFolderYear & "\" & Format(Me.OrderDate, "mmmm yyyy") & "\"     ' folder to save
 If Len(Dir(strFolderYear, vbDirectory)) = 0 Then
     MkDir (strFolderYear)
 End If
If Len(Dir(strFolderMonth, vbDirectory)) = 0 Then
     MkDir (strFolderMonth)
     End If
myFileNx = Format(Me.OrderDate, "dddd dd mmmm yyyy") & " - " & Me.PtName & ".pdf"
DoCmd.OpenReport "MainRpt", acViewPreview, , "OrderID = " & OrderID, acHidden
DoCmd.OutputTo acOutputReport, "MainRpt", acFormatPDF, strFolderMonth & myFileNx, True
DoCmd.RunMacro "RptClose"
 

Users who are viewing this thread

Back
Top Bottom