Close MS Access after running previous macros (1 Viewer)

duncan17

Registered User.
Local time
Yesterday, 21:54
Joined
Jan 29, 2019
Messages
10
I have made a macro in Access that import the data from a CSV into a table that is connected to a report. It then deletes the records in the table (to clear it out), then it brings up the print dialog screen so the user can select a printer to print the report onto. After the user selects the printer and the report prints, I want to close out of MS Access. I have tried sending the keystrokes %{F4} (Alt + F4) but this does not work as it closes the print dialog screen. :banghead: How would I go about closing/exiting MS Access after the previous macros have run? Would this be a sub macro? Sleep method? Any tips would be greatly appreciated!

Below is the macro vba code...

Function AutoExec()
On Error GoTo AutoExec_Err

DoCmd.TransferText acImportDelim, "DynamicsPartsLabels Import Specification1", "DynamicsPartsLabels", "\\~\DynamicsPartsLabels.csv", True, "" //Imports data from csv into table
DoCmd.OpenReport "Labels LabelTemplate", acViewReport, "", "", acNormal //opens the report that I want to print
DoCmd.RunSQL "DELETE FROM DynamicsPartsLabels", -1 //deletes records from table
SendKeys "~", True //enter key that exits warning message
SendKeys "^p", True //opens print dialog screen
DoCmd.Quit acExit //Quit MS Access


AutoExec_Exit:
Exit Function

AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit

End Function
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:54
Joined
Oct 29, 2018
Messages
21,358
Hi. Have you tried adding a step in your macro to exit the application?
 

duncan17

Registered User.
Local time
Yesterday, 21:54
Joined
Jan 29, 2019
Messages
10
I have tried adding a step to "Quit Access" after the other steps, but the "Quit Access" step causes MS Access to exit without showing the user the print dialog screen.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:54
Joined
Oct 29, 2018
Messages
21,358
Interesting. Unfortunately, I am not in front of a computer now to perform any tests to see how it can be done. How about showing us your macro design for starters? Thanks.
 

duncan17

Registered User.
Local time
Yesterday, 21:54
Joined
Jan 29, 2019
Messages
10
Function AutoExec()
On Error GoTo AutoExec_Err

DoCmd.TransferText acImportDelim, "DynamicsPartsLabels Import Specification1", "DynamicsPartsLabels", "\\~\DynamicsPartsLabels.csv", True, "" //Imports data from csv into table
DoCmd.OpenReport "Labels LabelTemplate", acViewReport, "", "", acNormal //opens the report that I want to print
DoCmd.RunSQL "DELETE FROM DynamicsPartsLabels", -1 //deletes records from table
SendKeys "~", True //enter key that exits warning message
SendKeys "^p", True //opens print dialog screen
DoCmd.Quit acExit //Quit MS Access


AutoExec_Exit:
Exit Function

AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit

End Function
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:54
Joined
Oct 29, 2018
Messages
21,358
Hi. Since you’re using VBA and not a macro, you should be able to modify it a bit to get what you want. For example, I dont see anywhere in your code where the print dialog is opened. That’s why I said ”interesting“ earlier.
 

duncan17

Registered User.
Local time
Yesterday, 21:54
Joined
Jan 29, 2019
Messages
10
I edited my above comment to show what each line in my code does
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:54
Joined
Oct 29, 2018
Messages
21,358
Okay, thanks. Still though, I can’t try anything since I’m only using my phone right now. Maybe what you could also try is do the quit command in the Close event of your report.
 

Users who are viewing this thread

Top Bottom