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: -
-
Private Sub cmdExport_Click()
-
-
On Error GoTo Err_cmdTest_Click
-
'Must 1st set a Reference to the Microsoft Office XX.X Object Library
-
Dim dlgOpen As FileDialog
-
Dim strExportPath As String
-
Const conOBJECT_TO_EXPORT As String = "qryExportMetrics"
-
-
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
-
-
With dlgOpen
-
.ButtonName = "Export To"
-
.InitialView = msoFileDialogViewLargeIcons
-
.InitialFileName = CurrentProject.Path
-
If .Show = -1 Then
-
'Allow for Root Directory selection: C:\, D:\, etc.
-
strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
-
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:=conOBJECT_TO_EXPORT, _
-
FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
-
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qry3", _
-
FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qry4", _
-
FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryCapacity", _
-
FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
-
-
-
-
-
MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
-
conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
-
End If
-
End With
-
-
'Set the Object Variable to Nothing.
-
Set dlgOpen = Nothing
-
-
Exit_cmdTest_Click:
-
Exit Sub
-
-
Err_cmdTest_Click:
-
MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
-
Resume Exit_cmdTest_Click
-
-
-
End Sub
-
-
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 : - Grab the code for FSBrowse() from the linked article and put it into a standard module in your project.
- From your Access Window press Alt-F11 to switch to the VBA IDE.
- Show the Project Explorer pane (Ctrl-R).
- 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.
- 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.
- 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.
- Paste the code you grabbed previously into here replacing anything already in there.
You now have a project which includes the FSBrowse() function. - 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).
- Replace your code then, with :
- 'Must 1st set a Reference to the Microsoft Office XX.X Object Library
-
Private Sub cmdExport_Click()
-
On Error GoTo Err_cmdTest_Click
-
Dim strExportPath As String
-
-
strExportPath = CurrentProject.Path & "\"
-
strExportPath = FSBrowse(strStart:=strExportPath, _
-
lngType:=msoFileDialogFilePicker, _
-
strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
-
"All Files,*.*")
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryExportMetrics", _
-
FileName:=strExportPath)
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qry3", _
-
FileName:=strExportPath)
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qry4", _
-
FileName:=strExportPath)
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryCapacity", _
-
FileName:=strExportPath)
-
Call MsgBox(Prompt:="Your queries have been exported to " & strExportPath, _
-
Buttons:=vbInformation, _
-
Title:="Export Complete")
-
Exit Sub
-
-
Err_cmdTest_Click:
-
Call MsgBox(Err.Description, vbExclamation, "Error in cmdTest_Click()")
-
End Sub
This should leave you with a fully integrated system that you can use relatively straightforwardly wherever required in your project.
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: -
Private Sub cmdExport_Click()
-
-
'Must 1st set a Reference to the Microsoft Office XX.X Object Library
-
-
On Error GoTo Err_cmdTest_Click
-
Dim strExportPath As String
-
-
strExportPath = CurrentProject.Path & "\"
-
strExportPath = FSBrowse(strStart:=strExportPath, _
-
lngType:=msoFileDialogFilePicker, _
-
strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
-
"All Files,*.*")
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryExportMetrics", _
-
FileName:=strExportPath)
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryCapacityBuilding", _
-
FileName:=strExportPath)
-
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryPresentingProblem", _
-
FileName:=strExportPath)
-
-
Call MsgBox(Prompt:="Your queries have been exported to " & strExportPath, _
-
Buttons:=vbInformation, _
-
Title:="Export Complete")
-
Exit Sub
-
-
Err_cmdTest_Click:
-
Call MsgBox(Err.Description, vbExclamation, "Error in cmdTest_Click()")
-
-
-
End Sub
-
-
The error message says "COMPILE ERROR: EXPECTED VARIABLE OR PROCEDURE, NOT MODULE" for line 9
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 : - strExportPath = FSBrowse(strStart:=strExportPath, _
-
lngType:=msoFileDialogFilePicker, _
-
strPattern:="MS Excel Workbooks,*.XLS; *.XLSX~" & _
-
"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.
@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.
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.
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 -
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
-
TableName:="qryCapacityBuilding", _
-
FileName:=strExportPath)
-
-
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
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.
@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: -
Query1 ==> C:\MyExports\DemoExport.xls
-
Query2 ==> C:\MyExports\DemoExport_2.xls
-
Query3 ==> C:\MyExports\DemoExport_3.xls
-
Query4 ==> C:\MyExports\DemoExport_4.xls
-
Query5 ==> C:\MyExports\DemoExport_5.xls
-
All you have to do is to substitute your Query Names in Code Lines# 22, 23, 25, 27, and 29: -
'Module Code intentionally not shown
-
Dim cDlg As New CommonDialogAPI 'Instantiate CommonDialog
-
Dim lngFormHwnd As Long
-
Dim lngAppInstance As Long
-
Dim strInitDir As String
-
Dim strFileFilter As String
-
Dim lngResult As Long
-
Dim strBaseName As String
-
-
lngFormHwnd = Me.Hwnd 'Form Handle
-
lngAppInstance = Application.hWndAccessApp 'Application Handle
-
strInitDir = CurrentProject.Path 'Initial Directory - [UD]
-
-
'Create any Filters here - [UD]
-
strFileFilter = "Excel Files (*.xls)" & Chr(0) & "*.xls"
-
-
lngResult = cDlg.SaveFileDialog(lngFormHwnd, _
-
lngAppInstance, strInitDir, strFileFilter)
-
-
If cDlg.GetStatus = True Then
-
strBaseName = Split(cDlg.GetName, ".")(0)
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", cDlg.GetName
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query2", _
-
strBaseName & "_2.xls"
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query3", _
-
strBaseName & "_3.xls"
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query4", _
-
strBaseName & "_4.xls"
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query5", _
-
strBaseName & "_5.xls"
-
Else
-
Exit Sub
-
End If
-
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.
Thanks to you two! I will try them soon and see which one works.
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 - DoCmd.OutputTo acOutputReport, "ReportName", acFormatXLS, cDlg.GetName
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.
@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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: Benny Ng |
last post by:
Hi,All,
Export Method:
-------------------------------------------------------------------------
strFileNameExport = "Results"
Response.Clear()
Response.Buffer = True...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |