Prompt SaveAs dialog when exporting data (1 Viewer)

Snowflake68

Registered User.
Local time
Today, 18:17
Joined
May 28, 2014
Messages
452
I have found this code on the web that will prompt the SaveAs dialog box when exporting a file but how can I modify it so that it populates the filename with a specific filename such as "Results" plus the current date?

Code:
Public Function saveFileAs() As String

Dim fd As FileDialog, fileName As String

    On Error GoTo ErrorHandler

    Set fd = Application.FileDialog(msoFileDialogSaveAs)
 
    If fd.Show = True Then
        If fd.SelectedItems(1) <> vbNullString Then
            fileName = fd.SelectedItems(1)
        End If
    Else
        'Stop Code Execution for Null File String
        End
    End If
    saveFileAs = fileName

    'Cleanup
    Set fd = Nothing
    Exit Function
 
ErrorHandler:
    Set fd = Nothing
    MsgBox "Error " & Err & ": " & Error(Err)

End Function

I am using the above code in the transfer spreadsheet code below.

Code:
Private Sub cmdExcelExport_Click()
    
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryData", saveFileAs, True

End Sub
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:17
Joined
May 7, 2009
Messages
19,246
add that to FileDialog InitialFileName, eg:

Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Date
 

Minty

AWF VIP
Local time
Today, 18:17
Joined
Jul 26, 2013
Messages
10,378
Adding the date as it is will cause an error due to the / present in the date format. You'll need to format it something like

Code:
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Format(Date,"yyyymmdd")
 

Snowflake68

Registered User.
Local time
Today, 18:17
Joined
May 28, 2014
Messages
452
add that to FileDialog InitialFileName, eg:

Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Date

Works perfectly thank you so much.:D
 

Snowflake68

Registered User.
Local time
Today, 18:17
Joined
May 28, 2014
Messages
452
had to format the date though as wouldnt allow slashes in file names but all good now.
Code:
fd.InitialFileName = "SupplyChainMonitor_" & Format(Date, "yyyymmdd")

thanks again.
 

Snowflake68

Registered User.
Local time
Today, 18:17
Joined
May 28, 2014
Messages
452
Adding the date as it is will cause an error due to the / present in the date format. You'll need to format it something like

Code:
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.InitialFileName = "Results_" & Format(Date,"yyyymmdd")

Thanks I discovered that too and managed to sort it :p
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:17
Joined
May 7, 2009
Messages
19,246
The excel file, that is what we are putting the cf
 

Users who are viewing this thread

Top Bottom