After some searching I think I've got all the pieces to my necessary answer. But my limited knowledge of VBA makes it hard to put it all together.
I have a button on a report, and on click it exports the query (which the report is based on) to an Excel file. However, I realized that if someone starts changing the folder names, then the export won't work. So, I want to verify that the folder exists; if not, the code should make the folder and export; if so, it should just go ahead with the export.
Below are the pieces of code I have so far. The top part I copied from some suggestion found while searching the net/forums. Partly I'm struggling with how/where to stick the "TestForDir()" part - I used the code builder for the exporting part, now I'm trying to add the test for directory part. I still don't understand the hierarchy of Subs (or what the heck "sub" and "dim" even mean...)
I also feel that once I define the folder path name as a string, I should be able to use that in naming the file for the Output Query command (instead of the entire drive name and folders/sub-folders I have now).
Can someone help out and put this stuff in the correct hierarchy and order? Thanks as always.
I have a button on a report, and on click it exports the query (which the report is based on) to an Excel file. However, I realized that if someone starts changing the folder names, then the export won't work. So, I want to verify that the folder exists; if not, the code should make the folder and export; if so, it should just go ahead with the export.
Below are the pieces of code I have so far. The top part I copied from some suggestion found while searching the net/forums. Partly I'm struggling with how/where to stick the "TestForDir()" part - I used the code builder for the exporting part, now I'm trying to add the test for directory part. I still don't understand the hierarchy of Subs (or what the heck "sub" and "dim" even mean...)
I also feel that once I define the folder path name as a string, I should be able to use that in naming the file for the Output Query command (instead of the entire drive name and folders/sub-folders I have now).
Code:
Sub TestForDir()
Dim strDir As String
strDir = "P:\Interpretation\Education\Teacher Information\Reports and Data\"
If Dir(strDir, vbDirectory) = "" Then
MkDir strDir
Else
MsgBox "Directory exists."
End If
End Sub
Private Sub btnExport_Click()
DoCmd.OutputTo acOutputQuery, "qryLocalTeacherEmails", acFormatXLSX, "P:\Interpretation\Education\Teacher Information\Reports and Data\Local Teacher Emails " & (Format(Date, "mm-dd-yyyy")) & ".xlsx", True
MsgBox "File exported to P:\Interpretation\Education\Teacher Information\Reports and Data", vbInformation, "Export Successful!"
End Sub
Can someone help out and put this stuff in the correct hierarchy and order? Thanks as always.