Sub ExportCustomerReports()'your function returns nothing, so it could be a sub
'Procedure declarations first; not "wrong", just better. PATH is a reserved word. To avoid this your naming
'convention should include type prefixes. This way, when reading far down in the code, you don't have to
'scroll up to see what type the variable is. I make some exceptions, such as rs or rst.
'I sometimes use CurrentDb and then the required method.
'Setting a db variable here instead since there's more than 1 usage for it, plus it illustrates an option you have
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strRptName As String, StrUsername As String
Dim strOrderNumber As String, strPath As String
On Error GoTo errHandler
StrUsername = Environ("UserName")
Set db = CurrentDb
db.Execute "MT_TempCustomerReports", dbFailOnError
Set rst = db.OpenRecordset "SELECT * FROM TempCustomerReports" 'so short that variable isn't really needed
If Not (rst.BOF And rst.EOF) Then 'if true, there are no records. This test should be made 1st
rst.MoveFirst 'no guarantee that you will start at the beginning if rst is based on a table. Good habit.
Do While Not rst.EOF
'Debug.Print rst!Report
strRptName = rst!Report
strOrderNumber = rst!OrderNumber
strPath = sDefaultPath & StrUsername & "/" & strOrderNumber & "/"
If Len(Dir(strPath, vbDirectory)) = 0 Then MyMkDir strPath
DoCmd.OutputTo acOutputReport, strRptName, "PDFFormat(*.pdf)", strPath & Mid(rst!Report, 4, 55) & ".pdf", False, "", 0, acExportQualityPrint
rst.MoveNext
'you didn't aswer what the counter was for so I removed it
Loop
End IF
exitHere:
Set db = Nothing
Set rst = Nothing
Exit Function
errHandler:
'if you want custom message, use strMsg and assign text to it. You can use If or Select Case block to deal with specific
'error numbers. Several err numbers can be individually dealt with this way. This one is just general and simple.
MsgBox "Error " & err.Number & ": " & err.Description
Resume exitHere
End Sub