473,385 Members | 1,453 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,385 software developers and data experts.

Exporting Access query to Excel

Hi,

I am trying to export an access query to excel. I have made use of transferspreadsheet command to achive this. However i have hardcoded the path and the filename in the VBA code. I am not sure as to how i can make the user enter his ownfile name and select the location where the file is to be written.

I am attaching below my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Click()
  2. On Error GoTo Err_Report_Click
  3.  
  4. Dim db As DAO.Database
  5. Dim qdf As DAO.QueryDef
  6. Dim strSQL As String
  7. Set db = CurrentDb
  8. Set qdf = db.QueryDefs("CWW Billing Query")
  9.  
  10. If Me.All_cbox = True Then
  11.     strSQL = "SELECT [CWW_Data].*,[CWW_Acct].Premises,[CWW_Acct].UC_Location,[CWW_Acct].SAP_Account,[CWW_Acct].Notes " & _
  12.              "FROM [CWW_Data],[CWW_Acct] " & _
  13.              "WHERE [CWW_Data].Billing_Month BETWEEN # " & Me.Bill_Start & " # AND # " & Me.Bill_End & " #" & _
  14.              "AND [CWW_Data].GCWW_Acct = [CWW_Acct].GCWW_Acct " & _
  15.              "ORDER BY [CWW_Acct].GCWW_Acct;"
  16. ElseIf Me.Loc_cbox = True Then
  17.     strSQL = "SELECT [CWW_Data].*,[CWW_Acct].Premises,[CWW_Acct].UC_Location,[CWW_Acct].SAP_Account,[CWW_Acct].Notes " & _
  18.              "FROM [CWW_Data],[CWW_Acct] " & _
  19.              "WHERE [CWW_Data].Billing_Month BETWEEN # " & Me.Bill_Start & " # AND # " & Me.Bill_End & " #" & _
  20.              "AND [CWW_Acct].UC_Location = '" & Me.Loc_combo.Value & "'" & _
  21.              "AND [CWW_Data].GCWW_Acct = [CWW_Acct].GCWW_Acct " & _
  22.              "ORDER BY [CWW_Acct].GCWW_Acct;"
  23. ElseIf Me.SAP_cbox = True Then
  24.     strSQL = "SELECT [CWW_Data].*,[CWW_Acct].Premises,[CWW_Acct].UC_Location,[CWW_Acct].SAP_Account,[CWW_Acct].Notes " & _
  25.              "FROM [CWW_Data],[CWW_Acct] " & _
  26.              "WHERE [CWW_Data].Billing_Month BETWEEN # " & Me.Bill_Start & " # AND # " & Me.Bill_End & " #" & _
  27.              "AND [CWW_Acct].SAP_Account = '" & Me.SAP_Combo.Value & "'" & _
  28.              "AND [CWW_Data].GCWW_Acct = [CWW_Acct].GCWW_Acct " & _
  29.              "ORDER BY [CWW_Acct].GCWW_Acct;"
  30. ElseIf Me.Acct_cbox = True Then
  31.     strSQL = "SELECT [CWW_Data].*,[CWW_Acct].Premises,[CWW_Acct].UC_Location,[CWW_Acct].SAP_Account,[CWW_Acct].Notes " & _
  32.              "FROM [CWW_Data],[CWW_Acct] " & _
  33.              "WHERE [CWW_Data].Billing_Month BETWEEN # " & Me.Bill_Start & " # AND # " & Me.Bill_End & " #" & _
  34.              "AND [CWW_Acct].GCWW_Acct = '" & Me.Acct_combo.Value & "'" & _
  35.              "AND [CWW_Data].GCWW_Acct = [CWW_Acct].GCWW_Acct " & _
  36.              "ORDER BY [CWW_Acct].GCWW_Acct;"
  37. ElseIf Me.Premises_cbox = True Then
  38.     strSQL = "SELECT [CWW_Data].*,[CWW_Acct].Premises,[CWW_Acct].UC_Location,[CWW_Acct].SAP_Account,[CWW_Acct].Notes " & _
  39.              "FROM [CWW_Data],[CWW_Acct] " & _
  40.              "WHERE [CWW_Data].Billing_Month BETWEEN # " & Me.Bill_Start & " # AND # " & Me.Bill_End & " #" & _
  41.              "AND [CWW_Acct].Premises = '" & Me.Premises_combo.Value & "'" & _
  42.              "AND [CWW_Data].GCWW_Acct = [CWW_Acct].GCWW_Acct " & _
  43.              "ORDER BY [CWW_Acct].GCWW_Acct;"
  44.  
  45. End If
  46.  
  47. Debug.Print strSQL
  48. qdf.SQL = strSQL
  49.  
  50. DoCmd.TransferSpreadsheet acExport, 8, "CWW Billing Query", "c:\test\test.xls", True, "Report"
  51.  
  52. DoCmd.Close acForm, Me.Name
  53.  
  54. Exit Sub
  55.  
  56. Err_Report_Click:
  57.     MsgBox "No records found for the selected query.", vbExclamation, "Information"
  58. End Sub
Thanks
Kaushik
Jan 8 '08 #1
2 2064
jaxjagfan
254 Expert 100+
I did a similar process several years ago (SAP environment as well). We created a billing folder on the network and pushed the files created to the network folder. We also dynamically named the files based on the Billing Month being processed.

Our network path was similar to:

\\MyServer\Acct\AP\Billing\2007\

Each file was named

Billing_01_2007.xls
Billing_02_2007.xls

(They had wanted the months (Jan, Feb) initially but then realized the numerics allowed for better sorting) If not now they will complain about this later.

Be consistent with locations and naming structures. Never know what an employee will name a file or where they will put it.

MyPath ="\\MyServer\Acct\AP\Billing\2007\"
Make sure to use UNC - not all users may have identical drive mappings.
MyFile = "Billing_ & Format([BillDate],"mm_yyyy") & ".xls"

TransferSpreadsheet .... MyPath & MyFile
Jan 8 '08 #2
I did a similar process several years ago (SAP environment as well). We created a billing folder on the network and pushed the files created to the network folder. We also dynamically named the files based on the Billing Month being processed.

Our network path was similar to:

\\MyServer\Acct\AP\Billing\2007\

Each file was named

Billing_01_2007.xls
Billing_02_2007.xls

(They had wanted the months (Jan, Feb) initially but then realized the numerics allowed for better sorting) If not now they will complain about this later.

Be consistent with locations and naming structures. Never know what an employee will name a file or where they will put it.

MyPath ="\\MyServer\Acct\AP\Billing\2007\"
Make sure to use UNC - not all users may have identical drive mappings.
MyFile = "Billing_ & Format([BillDate],"mm_yyyy") & ".xls"

TransferSpreadsheet .... MyPath & MyFile
Thank you for the suggestion. However these reports will not be generated every billing month, rather they will be generated by the various at their need. What i would like to do is to pop up a save as dialog box and allow the user to select the location and enter the file name. It there a way to do this?

Thanks
Kaushik
Jan 9 '08 #3

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

Similar topics

3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
4
by: D | last post by:
I've created a report with many subreports of aggregate data. I want my client to be able to export this data to Excel to make her charts, etc. Only one problem: one of the fields is a "SchoolYear"...
2
by: Kenneth | last post by:
How do I remove the limitation in Access that deny me from exporting 24000 rows and 17 columns (in a query) into Excel? Kenneth
1
by: Suffrinmick | last post by:
Hello Everyone I've built a database using Access 2000 which includes a query which is built using a form containing filters. No problem. When I export the results of the query to excel, (File >...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
16
by: robertmeyer1 | last post by:
Hey, I am working on creating a query which will export the information to excel. I have a specific excel document which has line by line items (corresponds to access query). Here's the...
6
by: jpatchak | last post by:
Hi, I have kind of a strange problem. I have a report that I need to export to excel. The report opens fine in Access and when I export it or click on "Analyze It with Microsoft Office Excel," I...
2
by: eskelies | last post by:
Hello all, I have data, which is separated into account numbers (ie. 10, 20, 30), but it exists in one query. Right now, I have an access macro "transferspreadsheet," which is exporting all the...
4
by: myemail.an | last post by:
Hi all, I use Access 2007 and have the following problems: when exporting banal select queries (either to Excel or to a csv file) I find that exporting often doesn't work and creates a file with...
9
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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 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.