473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"Save As export:" function to have the option of naming the file before exporting.

I have this code up and running for exporting queries. Right now, when I click "EXPORT" button, it prompts me as to which folder I would like to have the file exported to. But it does not ask me to type in the "FILE NAME" or in other words, name the file that I will be exporting. Therefore, what should I be adding to my existing codes to add that option as well?



The codes I have so far is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdExport_Click()
  3.  
  4. On Error GoTo Err_cmdTest_Click
  5. 'Must 1st set a Reference to the Microsoft Office XX.X Object Library
  6. Dim dlgOpen As FileDialog
  7. Dim strExportPath As String
  8. Const conOBJECT_TO_EXPORT As String = "qryExportMetrics"
  9.  
  10. Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
  11.  
  12. With dlgOpen
  13.   .ButtonName = "Export To"
  14.   .InitialView = msoFileDialogViewLargeIcons
  15.   .InitialFileName = CurrentProject.Path
  16.      If .Show = -1 Then
  17.        'Allow for Root Directory selection: C:\, D:\, etc.
  18.        strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
  19.  
  20.        Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  21.                                TableName:=conOBJECT_TO_EXPORT, _
  22.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  23.  
  24.  Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  25.                                TableName:="qry3", _
  26.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  27. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  28.                                TableName:="qry4", _
  29.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  30. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  31.                                TableName:="qryCapacity", _
  32.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  33.  
  34.  
  35.  
  36.  
  37.        MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
  38.                conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
  39.      End If
  40. End With
  41.  
  42. 'Set the Object Variable to Nothing.
  43. Set dlgOpen = Nothing
  44.  
  45. Exit_cmdTest_Click:
  46.   Exit Sub
  47.  
  48. Err_cmdTest_Click:
  49.   MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
  50.     Resume Exit_cmdTest_Click
  51.  
  52.  
  53. End Sub
  54.  
  55.  
Sep 2 '11 #1
12 4208
NeoPa
32,556 Expert Mod 16PB
Michelle, for many people choosing ADezii's code to work with would be a great idea. At your level of understanding though, I suggest you'd be better advised taking the code from the linked article instead. Let me explain.

Most people would see this code and understand why and how it works. When they needed minor modifications such as what you ask for here, they would work with the code and make the changes, which are relatively straightforward. At your level of understanding though, where you need to ask for help for such things, it would make more sense to have a routine that is written to be flexible and to be used within various different scenarios (as is so for the code in the article referred to in the earlier thread - Select a File or Folder using the FileDialog Object). That way, all you need to understand is how to call this one procedure.

Let me see if I can give you some help setting this up :
  1. Grab the code for FSBrowse() from the linked article and put it into a standard module in your project.
    1. From your Access Window press Alt-F11 to switch to the VBA IDE.
    2. Show the Project Explorer pane (Ctrl-R).
    3. Underneath your main project are up to three folder icons. The top one will contain modules for your main objects (Forms; Reports; etc). Underneath that may be one called Modules if any exist.
    4. If this exists there may be a number of modules connected to it. You can either choose one of these to add the code into or ignore these and follow the steps below to add one instead. If you add the code to an existing module then ensure that the first two lines go at the very top of it. If either is already present then it won't need to be duplicated.
    5. To add a new module, should you need or choose to, put the mouse inside the Project Explorer pane, right-click and select Insert... followed by Module.
    6. Paste the code you grabbed previously into here replacing anything already in there.
    You now have a project which includes the FSBrowse() function.
  2. Now we just need to manipulate your code a little to use this function instead of coding it directly as ADezii's code did (which was good news for helping more advanced coders understand the subject better, but less so for you).
  3. Replace your code then, with :
    Expand|Select|Wrap|Line Numbers
    1. 'Must 1st set a Reference to the Microsoft Office XX.X Object Library
    2. Private Sub cmdExport_Click()
    3. On Error GoTo Err_cmdTest_Click
    4.     Dim strExportPath As String
    5.  
    6.     strExportPath = CurrentProject.Path & "\"
    7.     strExportPath = FSBrowse(strStart:=strExportPath, _
    8.                              lngType:=msoFileDialogFilePicker, _
    9.                              strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
    10.                                          "All Files,*.*")
    11.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    12.                                    TableName:="qryExportMetrics", _
    13.                                    FileName:=strExportPath)
    14.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    15.                                    TableName:="qry3", _
    16.                                    FileName:=strExportPath)
    17.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    18.                                    TableName:="qry4", _
    19.                                    FileName:=strExportPath)
    20.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
    21.                                    TableName:="qryCapacity", _
    22.                                    FileName:=strExportPath)
    23.     Call MsgBox(Prompt:="Your queries have been exported to " & strExportPath, _
    24.                 Buttons:=vbInformation, _
    25.                 Title:="Export Complete")
    26.     Exit Sub
    27.  
    28. Err_cmdTest_Click:
    29.     Call MsgBox(Err.Description, vbExclamation, "Error in cmdTest_Click()")
    30. End Sub

This should leave you with a fully integrated system that you can use relatively straightforwardly wherever required in your project.
Sep 3 '11 #2
Thanks NeoPa! I know I am such a pain to you and to the rest of the experts.. I am trying my best to learn as much as possible as I am very new to Access.


Anyways, I tried what you just said. I created FSBrowse module by copy and paste.

Then I put the code to the export button and it is giving me error.
the codes are as follow:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExport_Click()
  2.  
  3. 'Must 1st set a Reference to the Microsoft Office XX.X Object Library
  4.  
  5. On Error GoTo Err_cmdTest_Click
  6.     Dim strExportPath As String
  7.  
  8.     strExportPath = CurrentProject.Path & "\"
  9.     strExportPath = FSBrowse(strStart:=strExportPath, _
  10.                              lngType:=msoFileDialogFilePicker, _
  11.                              strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
  12.                                          "All Files,*.*")
  13.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  14.                                    TableName:="qryExportMetrics", _
  15.                                    FileName:=strExportPath)
  16.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  17.                                    TableName:="qryCapacityBuilding", _
  18.                                    FileName:=strExportPath)
  19.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  20.                                    TableName:="qryPresentingProblem", _
  21.                                    FileName:=strExportPath)
  22.  
  23.     Call MsgBox(Prompt:="Your queries have been exported to " & strExportPath, _
  24.                 Buttons:=vbInformation, _
  25.                 Title:="Export Complete")
  26.     Exit Sub
  27.  
  28. Err_cmdTest_Click:
  29.     Call MsgBox(Err.Description, vbExclamation, "Error in cmdTest_Click()")
  30.  
  31.  
  32. End Sub
  33.  
  34.  

The error message says "COMPILE ERROR: EXPECTED VARIABLE OR PROCEDURE, NOT MODULE" for line 9
Sep 3 '11 #3
NeoPa
32,556 Expert Mod 16PB
That makes no sense to me. Just to be clear, you're saying the code that's highlighted when you compile it is :
Expand|Select|Wrap|Line Numbers
  1.     strExportPath = FSBrowse(strStart:=strExportPath, _
  2.                              lngType:=msoFileDialogFilePicker, _
  3.                              strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
  4.                                          "All Files,*.*")
If that is true then I'm very confused.

In that case (that you confirm this is exactly what you are getting) you may want to attach a copy of the database. There are some important instructions to follow for that which can be found in Attach Database (or other work). It looks complicated but if you follow each instruction one at a time it's pretty straightforward really. If you do that I'll look into it for you and see if I can work out what's going wrong.
Sep 3 '11 #4
ADezii
8,834 Expert 8TB
@Michele: I hope I am not confusing matters, but it appears as though everything that you are saying indicates the need for a 'True' Save As Dialog Box. Simply download the Attachment and see if it is what you need, it needs no References and is completely self contained.
Attached Files
File Type: zip SaveAs.zip (34.0 KB, 177 views)
Sep 3 '11 #5
NeoPa
32,556 Expert Mod 16PB
I'm not sure that's the problem here ADezii, but if so lngType:=msoFileDialogSaveAs would give that facility. It probably is a better way to use the function actually, as I expect it gives the opportunity to enter a non-existent filename rather than simply select an existing one.

I just tested it and that's certainly a better way of doing it. Thanks to ADezii for noticing that :-)

PS. I like the new avatar ADezii. Different.
Sep 3 '11 #6
I just tried what ADezii had posted and that is what I was looking for and wanted.
Its working perfectly when I add those codes to my database but if you see my codes, I am exporting multiple queries as well. I tried adding this codes but it is giving me error

Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  2.                                    TableName:="qryCapacityBuilding", _
  3.                                    FileName:=strExportPath)
  4.  
  5.  

Line 3 is the error
and says "Compile error: Variable not defined"

I tried changing it to "conOBJECT_TO_EXPORT" for FileName but it did not work either
Sep 3 '11 #7
NeoPa
32,556 Expert Mod 16PB
I'm not sure why it would make sense to try on a completely different tack at this stage of the question Michelle. Do so if you think it would help of course, but it's not clear that's the case here. Why not post an answer to my post #4 anyway and that way there are more solutions to choose from.

I'm sure ADezii's solution can work, but you're not going to find it any easier incorporating that into your project than you will the FileDialog approach.
Sep 3 '11 #8
ADezii
8,834 Expert 8TB
@Michele: I am totally at a loss at this point, so I created this Final Demo as a last ditch effort. This Demo will allow you to enter a Filename in a Save As Dialog Box, then Export 5 Querys to Excel Spreadsheets named after the Filename you entered. Let's suppose that you wish to Save the Exports as DemoExport.xls in a Folder named C:\MyExports. Here is how the 5 Querys named Query1...Query5 will be Exported and named:
Expand|Select|Wrap|Line Numbers
  1. Query1 ==> C:\MyExports\DemoExport.xls
  2. Query2 ==> C:\MyExports\DemoExport_2.xls
  3. Query3 ==> C:\MyExports\DemoExport_3.xls
  4. Query4 ==> C:\MyExports\DemoExport_4.xls
  5. Query5 ==> C:\MyExports\DemoExport_5.xls
  6.  
All you have to do is to substitute your Query Names in Code Lines# 22, 23, 25, 27, and 29:
Expand|Select|Wrap|Line Numbers
  1. 'Module Code intentionally not shown
  2. Dim cDlg As New CommonDialogAPI         'Instantiate CommonDialog
  3. Dim lngFormHwnd As Long
  4. Dim lngAppInstance As Long
  5. Dim strInitDir As String
  6. Dim strFileFilter As String
  7. Dim lngResult As Long
  8. Dim strBaseName As String
  9.  
  10. lngFormHwnd = Me.Hwnd                           'Form Handle
  11. lngAppInstance = Application.hWndAccessApp      'Application Handle
  12. strInitDir = CurrentProject.Path                'Initial Directory - [UD]
  13.  
  14. 'Create any Filters here - [UD]
  15. strFileFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls"
  16.  
  17. lngResult = cDlg.SaveFileDialog(lngFormHwnd, _
  18.             lngAppInstance, strInitDir, strFileFilter)
  19.  
  20. If cDlg.GetStatus = True Then
  21.  strBaseName = Split(cDlg.GetName, ".")(0)
  22.   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", cDlg.GetName
  23.   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query2", _
  24.                                       strBaseName & "_2.xls"
  25.   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query3", _
  26.                                       strBaseName & "_3.xls"
  27.   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query4", _
  28.                                       strBaseName & "_4.xls"
  29.   DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query5", _
  30.                                       strBaseName & "_5.xls"
  31. Else
  32.   Exit Sub
  33. End If
  34.  
P.S.-It is imperative that you download the Attachment and change nothing but the Code Lines previously mentioned, or Remove/Add a TransferSpreadsheet Line.
Attached Files
File Type: zip SaveAs_2.zip (34.4 KB, 147 views)
Sep 4 '11 #9
Thanks to you two! I will try them soon and see which one works.
Sep 5 '11 #10
neelsfer
547 512MB
Thx Adezi for that code - i have been looking for it.
I used it in my project to export a race report to Excel - works 200% as usual!!
For anybody else requiring this slight variation to export a report to Excel, and not a Query
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputReport, "ReportName", acFormatXLS, cDlg.GetName
Sep 5 '11 #11
NeoPa
32,556 Expert Mod 16PB
Michelle:
Thanks to you two! I will try them soon and see which one works.
I'm sure both can work Michelle, but let us know how you get on anyway.
Sep 7 '11 #12
ADezii
8,834 Expert 8TB
@neelsfer: Glad you found the Code useful. Be advised that you do not need to type the File Extension for the actual Save As Filename. It will automatically append a .xls Extension if none is provided. This Default can be changed if so desired.
Sep 8 '11 #13

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

Similar topics

5
by: W.Guerlich | last post by:
I've got a Java servlet that delivers large database resultsets transformed to Excel with the HSSF library. In some cases it takes more than 15 minutes before transformation is done and content can...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
3
by: AJ | last post by:
Hi , I am trying to dynamically change file name of an html that is generated through a cgi script, I know that for it is easy to do this for other type of files such as pdf or csv which we...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
4
by: Richard | last post by:
Hi I'm new to ASP/Web programming so any help would be appreciated... Situation: On my web page I would like to present a link {or button} that would allow the user to download a large file. ...
5
by: Paul Sullivan | last post by:
We are a state agency that views protected medical information via our intranet. The screens even have privacy shields. Alarmingly, uses can "Print" and "Save As" which destroys the protection of...
1
by: Siu | last post by:
Hi, is it possibile to change the format of a file by coding (in the codebehind) the function "Save As..." ?? My aim is to change a html file which is visible in Excel, into a .xls file by coding...
0
by: johnb41 | last post by:
The heart of my application is a database (MS Jet, SQL, etc.). The user needs a connection to this database before he/she can do anything w/ the program. The user will also work with supplimental...
5
by: Matt S. | last post by:
I have been using C# 2005 Express to make class libraries for use in another software program. Each version of this software has a different .NET API, so maintaining multiple solutions of the same...
5
by: Jinzuku | last post by:
Hi, I'm trying to map an export function to a command button to my Access form (called Results). However, I would like it so that the "Save As" window appears when I try to export the table so...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.