To give some more info.. Here is what I had, and for the most part it worked:
Dim stDocName As String
'I have a check box as to whether we want the employee's dependents to be included, so if 'the box is checked, it set the [Relationship] to "Sub" which means an employee.
If Nz(Me!IncDep.Value, False) = True Then
stDocName = "Census - UPMC"
DoCmd.OpenReport stDocName, acViewPreview, , "[Relationship]='" & "Sub" & "'"
DoCmd.RunSavedImportExport "Export-Census - UMPC"
Else
stDocName = "Census - UPMC"
DoCmd.OpenReport stDocName, acViewPreview
DoCmd.RunSavedImportExport "Export-Census - UPMC"
End If
The problem with this is that it made the saved export the same name every time, and my coworkers want the date appended to the name of the file at export; the file could only be saved as .xls and we need .xlsx; it would open the report every time, which they just then have to close again.
So I came up with this:
Function OutputUPMCWithoutDeps() As Byte
Dim mFilename As String
mFilename = "V:\Employee Census Forms\DatabaseCensus\Census UPMC Without Deps " _
& Format(Now(), "yyyy-mm-dd_h-nnA/P") & ".xlsx"
DoCmd.OutputTo acOutputQuery, "Census - UPMC", acFormatXLSX, mFilename
MsgBox "Done outputting " & mFilename, , "Done"
End Function
If Nz(Me!IncDep.Value, False) = True Then
OutputUPMCWithoutDeps
Else
OutputUPMCWithDeps
End If
Which fixed the file name issue, but now that I'm not using a report but a query, my Row_ID is gone and I need to replace it somehow? I tried DoCmd.OutputTo acOutputReport, but got a run-time error 2282..