Connecting Tech Pros Worldwide Forums | Help | Site Map

Using msoFileDialogSaveAs in MS Access

msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,886
#1   2 Weeks Ago
I recently had occasion to want to export the results of a query from Access to Excel using FileDialog. I found researching it that there was a lot of confusion regarding whether SaveAs could even be used in Access and couldn't find a simple routine. I experimented a little and came up with the following which works perfectly.

Although I used this routine to export to Excel it could in theory be used with any format supported by DoCmd.Output or really any statement which output a file.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSendtoExcel_Click()
  2. Dim fd As FileDialog
  3. Dim Title As String
  4. Dim vrtSelectedItem As Variant
  5.  
  6.     Set fd = Application.FileDialog(msoFileDialogSaveAs)
  7.  
  8.     With fd
  9.         .AllowMultiSelect = False
  10.         .Title = "Save File"
  11.         .InitialFileName = "Name Of Report " & Format(Now(), "ddmmyyyyhhnn") & ".xls"
  12.  
  13.         If .Show = True Then
  14.             For Each vrtSelectedItem In .SelectedItems
  15.                 DoCmd.OutputTo acOutputQuery, "query or table name", acFormatXLS, vrtSelectedItem
  16.             Next vrtSelectedItem
  17.         Else
  18.             MsgBox "No file was selected"
  19.         End If
  20.  
  21.     End With
  22.  
  23. End Sub
  24.  
If anyone knows of any improvedments or enhancements to this routine feel free to offer advice.

Mary



Reply

Tags
excel, filedialog, ms access, msofiledialogsaveas, outputto, saveas, vba