J
jbassett
I have been playing around with using the FileDialog SaveAs object in
Access 2003 VBA. After searching I see that the standard is to use the
code at:
http://www.mvps.org/access/api/api0001.htm
However, I modified some code in a thread that Amy Blankenship tossed
out as untested and came up with the following.
*** Code Start ***
Private Sub ExportCB_Click()
Dim dlgSaveAs As FileDialog
Dim strRawFileInfo As String
Dim strMailerExportPath As String
Dim strMailerExportFile As String
' Create Save AS Dialog Box
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
'Use a With...End With block to reference the FileDialog object.
With dlgSaveAs
.Title = "Save Export File"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then
strRawFileInfo = .SelectedItems(1)
'The user pressed Cancel.
Else
strRawFileInfo = ""
MsgBox "Action Canceled"
End If
End With
' Extract Seperate Path and File Names from SaveAs Dialog Box
If strRawFileInfo <> "" Then
strMailerExportFile = Right(strRawFileInfo, Len(strRawFileInfo)
- InStrRev(strRawFileInfo, "\"))
strMailerExportPath = Left(strRawFileInfo, Len(strRawFileInfo)
- Len(strMailerExportFile))
End If
' Check file name for correct extension and if not present add .txt
If Right(strMailerExportFile, 4) <> ".txt" Then
' Check for wrong extension and strip it
If Mid((Right(strMailerExportFile, 4)), 1, 1) = "." Then
strMailerExportFile = Left(strMailerExportFile,
Len(strMailerExportFile) - 4)
Else
End If
' Add Correct Extension
strMailerExportFile = strMailerExportFile & ".txt"
Else
End If
' Export tab delimited txt file to folder with file name that use
selected
DoCmd.TransferText acExportDelim, , "QBrokerMailer",
strMailerExportPath & strMailerExportFile
' Clear strings used
strRawFileInfo = ""
strMailerExportPath = ""
strMailerExportFile = ""
End Sub
*** End Code ***
Being relatively new to Access VBA I wanted to get some feedback on the
above code. Can / should it be streamlined (Not sure if I need to
break the path and the file name out into two separate strings)? Would
it work if I moved it to a module rather then repeating the code on
every form? It seems to work just fine for my use (Exporting a query
and allowing the individual to select where and name the file via the
file dialog box).
Comments appreciated.
Thanks,
Jeff Bassett
Commercial Properties, Inc.
j bassett (at) cpi az (dot) com
Access 2003 VBA. After searching I see that the standard is to use the
code at:
http://www.mvps.org/access/api/api0001.htm
However, I modified some code in a thread that Amy Blankenship tossed
out as untested and came up with the following.
*** Code Start ***
Private Sub ExportCB_Click()
Dim dlgSaveAs As FileDialog
Dim strRawFileInfo As String
Dim strMailerExportPath As String
Dim strMailerExportFile As String
' Create Save AS Dialog Box
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
'Use a With...End With block to reference the FileDialog object.
With dlgSaveAs
.Title = "Save Export File"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then
strRawFileInfo = .SelectedItems(1)
'The user pressed Cancel.
Else
strRawFileInfo = ""
MsgBox "Action Canceled"
End If
End With
' Extract Seperate Path and File Names from SaveAs Dialog Box
If strRawFileInfo <> "" Then
strMailerExportFile = Right(strRawFileInfo, Len(strRawFileInfo)
- InStrRev(strRawFileInfo, "\"))
strMailerExportPath = Left(strRawFileInfo, Len(strRawFileInfo)
- Len(strMailerExportFile))
End If
' Check file name for correct extension and if not present add .txt
If Right(strMailerExportFile, 4) <> ".txt" Then
' Check for wrong extension and strip it
If Mid((Right(strMailerExportFile, 4)), 1, 1) = "." Then
strMailerExportFile = Left(strMailerExportFile,
Len(strMailerExportFile) - 4)
Else
End If
' Add Correct Extension
strMailerExportFile = strMailerExportFile & ".txt"
Else
End If
' Export tab delimited txt file to folder with file name that use
selected
DoCmd.TransferText acExportDelim, , "QBrokerMailer",
strMailerExportPath & strMailerExportFile
' Clear strings used
strRawFileInfo = ""
strMailerExportPath = ""
strMailerExportFile = ""
End Sub
*** End Code ***
Being relatively new to Access VBA I wanted to get some feedback on the
above code. Can / should it be streamlined (Not sure if I need to
break the path and the file name out into two separate strings)? Would
it work if I moved it to a module rather then repeating the code on
every form? It seems to work just fine for my use (Exporting a query
and allowing the individual to select where and name the file via the
file dialog box).
Comments appreciated.
Thanks,
Jeff Bassett
Commercial Properties, Inc.
j bassett (at) cpi az (dot) com