fDialog use with form to export data

K

Kate

I want to create a form to export data from a query and save it in a .csv
file. I can figure out how to use the msoFileDialogFilePicker code to select
a file that already exists and write the new data into it. But how do I
allow the user to input a new file name and have the data write out to that.

Here is what I have - but I want to be able to add new file names.

Private Sub HaircutExtract_Click()
On Error GoTo Err_HaircutExtract_Click
Dim fDialog As Office.FileDialog
Dim varFile As Variant

Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog

' Limit file selection to a single file in dialog box
.AllowMultiSelect = False

' Set the title of the dialog box.
.Title = "Please select one file for target"

' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Access Projects", "*.ADP"
.Filters.Add "All Files", "*.*"

' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.

If .Show = True Then
For Each varFile In .SelectedItems

DoCmd.TransferText acExportDelim, "HaircutExtractSpec", "Haircut
Query", FileName:=varFile
Next

Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With


Exit_HaircutExtract_Click:
Exit Sub

Err_HaircutExtract_Click:

MsgBox Err.Description
Resume Exit_HaircutExtract_Click

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top