Sometimes, in Ms. Ascces, I was need to export the current form recordsource to Ms. Excel, with vba code.

the best esay way to to this is:

Private Sub cmdtoexcel_Click()
     TempSQL = Me.RecordSource
     If IsNull(DLookup("name", "msysobjects", "name='query1'")) Then
         CurrentDb.CreateQueryDef "Query1", TempSQL
     Else
         CurrentDb.QueryDefs("Query1").SQL = TempSQL
     End If
     Dim dlgOpen As FileDialog
     Set dlgOpen = Application.FileDialog(msoFileDialogSaveAs)
     With dlgOpen
             .InitialFileName = "f:\initial directory"
     End With
 
     If dlgOpen.Show = 0 Then
         Exit Sub
     Else
         ' Code if "action" button was clicked...
         DoCmd.OutputTo acOutputQuery, "Query1", acFormatXLS, dlgOpen.SelectedItems(1), 0
     End If
end sub
Facebooktwitterredditpinterestlinkedinmail