Renaming exported query (1 Viewer)

hllary

Registered User.
Local time
Yesterday, 21:19
Joined
Sep 23, 2019
Messages
80
I have a button that exports a query to an excel. The issue is the name of the query is in the file name. Is there a way the user has to enter a file name?

1586366639225.png


My code is:

Code:
Private Sub BtnExport_Click()

On Error GoTo Err_Handler
    
    DoCmd.Requery (ListboxExport_qry)
    DoCmd.OutputTo acOutputQuery, "ListboxExport_qry", acFormatXLSX, sFilename, True
    
Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "The exported excel was not saved."
    Resume Exit_Handler
        
End Sub
 

bastanu

AWF VIP
Local time
Yesterday, 21:19
Joined
Apr 13, 2010
Messages
1,402
Use FileDialog to allow them to save the file with their choice of name
Code:
Dim intChoice As Integer
Dim sFilename As String 'you might already have this declared somewhere else

'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
sFilename = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
DoCmd.OutputTo acOutputQuery, "ListboxExport_qry", acFormatXLSX, sFilename, True

Cheers,
Vlad
 

Users who are viewing this thread

Top Bottom