473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Using msoFileDialogSaveAs in MS Access

MMcCarthy
14,534 Expert Mod 8TB
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
Nov 13 '09 #1
4 34419
ADezii
8,834 Expert 8TB
Since AllowMultiSelect = True, you actually don't need the For...Next Loop. I know, a trivial point, just thought that I would mention it anyway.
Expand|Select|Wrap|Line Numbers
  1. Dim fd As FileDialog
  2. Dim Title As String
  3. Dim vrtSelectedItem As Variant
  4.  
  5. Set fd = Application.FileDialog(msoFileDialogSaveAs)
  6.  
  7. With fd
  8.   .AllowMultiSelect = False
  9.   .Title = "Save File"
  10.   .InitialFileName = "Name Of Report " & Format(Now(), "ddmmyyyyhhnn") & ".xls"
  11.  
  12.      If .Show = True Then
  13.        DoCmd.OutputTo acOutputQuery, "query or table name", acFormatXLS, .SelectedItems(1)
  14.      Else
  15.        MsgBox "No file was selected"
  16.      End If
  17. End With
Dec 16 '09 #2
MMcCarthy
14,534 Expert Mod 8TB
;) Trivial points are all good

I adapted this from another procedure and just left the for loop in. Lazy I know :)
Dec 16 '09 #3
TheSmileyCoder
2,322 Expert Mod 2GB
An issue I have had with the SaveAs dialog is that I cannot seem to force it to be a specific file type. For instance it would allow me to type C:\MyFileName.PDF when I want to force the user to save as excel for example.

The issue is that I cannot set the filter type for the SaveAs dialog. So I have had to check the fileextension after a path is selected. This is the code I ended up with, to whom it may concern:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_Browse_Click()
  2.    Dim oFD As FileDialog
  3.    Dim strExtension As String
  4.    Dim strValidExtensions As String
  5.  
  6.    'Define valid extensions
  7.       strValidExtensions = ".xlsx;.xlsm"
  8.  
  9.    'Open filedialog object
  10.       Set oFD = Application.FileDialog(msoFileDialogSaveAs)
  11.  
  12.    oFD.InitialFileName = "CustomExport" & Format(Date, "yyyy\-mm\-dd") & ".xlsx"
  13.  
  14. tryAgain:
  15.    If oFD.Show() Then
  16.       strExtension = GetFileExt(oFD.SelectedItems(1))
  17.       If strExtension = "" Then
  18.          'No extension typed, set manually to excel
  19.          Me.tb_ExcelPath = oFD.SelectedItems(1) & ".xlsx"
  20.       Else
  21.          If InStr(1, strValidExtensions, strExtension) <= 0 Then
  22.             MsgBox "Invalid file type selected"
  23.             GoTo tryAgain
  24.          End If
  25.       End If
  26.  
  27.       Me.tb_ExcelPath = oFD.SelectedItems(1)
  28.  
  29.    End If
  30.    Set oFD = Nothing
  31. End Sub
GetFileExt is a custom function:
Expand|Select|Wrap|Line Numbers
  1. Public Function GetFileExt(strFilePath As String) As String
  2.     Dim strArray() As String
  3.     strArray = Split(strFilePath, ".")
  4.     If UBound(strArray) <= 0 Then
  5.       'Input is empty, or without extension
  6.       GetFileExt = ""
  7.     Else
  8.       GetFileExt = "." & strArray(UBound(strArray))
  9.    End If
  10. End Function
Jan 29 '13 #4
NeoPa
32,556 Expert Mod 16PB
You may find the Select a File or Folder using the FileDialog Object article helps there Smiley. Let me know here if not.
Jan 29 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim | last post by:
I am a FoxPro programmer. What software do I need to program using MS access... I have apps that are non-web standalone and some web based... Do you recommend any learning resources? ...
1
by: Patrick Blackman | last post by:
Hi I need a good example using ms access with C#. I have to develop a multi user test application using access. Need some pointer on how to handle user conflicts etc. All examples I have seen use...
5
by: jhcorey | last post by:
We're using a third-party treeview and found that the functionality we need works only when we set the web site security to allow Anonymous Access. We also need the user's network id, which we...
2
by: David Dawson | last post by:
I have no experience using MS Access to interface with MySQL. I have a tiny MySQL database that might need to have Access connect with it over the internet and grab data/update etc. Is this...
0
by: Abhi | last post by:
Hi, I have to connect to Oracle 10g Database using Data Access Application block code i am using is Connection string is contained in a file called orcle.config and it contains ...
6
by: ttamilvanan81 | last post by:
Hai. I needto Receive Email using Ms-Access and Outlook. On the receiving end, the user receives the email at the Outlook. When they opens the Email, the message box should have a button called...
0
by: PROGRAMMINGMAESTO | last post by:
How to Fetch and Retrieve image in ASP using MS Access. -------------------------------------------------------------------------------- Hello Friends, Can Anybody guide about how should i...
0
Kosal
by: Kosal | last post by:
Hi Please help me to create AdRotator using Database Access in ASP.NET if everyone can because I don't know how to create table AdList in access and how to contect to access and get data from...
6
by: Mark | last post by:
Currently using MS Access 2000 and SQL Server Express. Using the current DAO OpenRecordset code listed below, however I keep getting the error message.... 3254: ODBC --Cannot lock all records ...
1
by: ncsthbell | last post by:
I created a database using full blown access 2007. I have put it out for users to grab and test using Runtime Access 2007. They have entered data and now I need to go back into a table and change a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.