473,511 Members | 14,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Including Combobox Selection into Exported Excel filename - VBA

6 New Member
Hi, I am currently exporting queries based on a combo box selection to Excel 2010 using Access 2013. The combo box specifies the Team that the query retrieves data for. What I would like to do is include the combo box selection as part of the spreadsheet export i.e. name of file & team (as specified by combobox) & date & .xls. Here is what I have so far (this obviously works up until the combo box bit at the end):

Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acExport, _
  2.    acSpreadsheetTypeExcel8, _
  3.    "Query name", _
  4.    "File Path" & _
  5.    Format(Date, "ddmmyyyy") & _
  6.    ".xls" & _
  7.    "[Forms]![Qry001_Main_Form]![Select_Team]"
Any help on this would be appreciated, I cant seem to find anything.

Thanks.
Feb 6 '14 #1
4 4949
jimatqsi
1,271 Recognized Expert Top Contributor
You have to be sure the ".xls" is the last part.

Jim
Feb 6 '14 #2
butler808
6 New Member
Hi,

Thanks for your input.

So using your suggestion I can get this filename:
Weekly_KPI_DB_Report_10022014[Forms]![Qry001_Main_Form]![Select_Team]

However, I want to capture the combo box selection in the export file name i.e.
Weekly_KPI_DB_Report_10022014 Nightshift

Is this possible?

Thanks again.
Feb 10 '14 #3
mcupito
294 Contributor
Here is code that refers to a TextBox control.

Expand|Select|Wrap|Line Numbers
  1.             outputFileName = CurrentProject.Path & "\ReportsReportName" & Year(StartDate) & ".xlsx"
  2.             DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ReportName", outputFileName, True
Note the .xlsx is for Excel 2007. (StartDate) is the name of the text box control.

I did not specifically test out your method, however, you are simply concatenating the text of what you typed in the code, as is. What you need to do is capture the .Value (I think) of the combobox and add it as a string to the outputFileName. Until someone else comes along finds a way of doing this, I suggest giving that a try.
Feb 10 '14 #4
zmbd
5,501 Recognized Expert Moderator Expert
I know that many will simply place the string within the function; however, that makes it very difficult to troubleshoot should you string not resolve properly.
(this is a petpeeve of mine, I really wish that examples in the text and web wouldn't show to build the string within the function)

So I suggest something like this:

Expand|Select|Wrap|Line Numbers
  1. Sub zj_excel_sheet2tablet_1()
  2. Dim ZFilename As String 
  3. Dim zqryout As String
  4. ZFilename = "C:\Documents and Settings\All Users\Workbook1.xlsx"
  5. zqryout = "Query1"
  6. DoCmd.TransferSpreadsheet TransferType:=acExport, _
  7.     Spreadsheettype:=acSpreadsheetTypeExcel12Xml, _
  8.     TableName:=zqryout, _
  9.     Filename:=ZFilename, HasFieldNames:=True_
  10.     ',Range:= "B2:J32" 'uncomment if you want to goto a range.
  11. End Sub
  12.  
This is simple, basic, and has been proven to work.

Let's compare this with your code:


Let's start: Line 2, your acSpreadsheetType is not correct for Excel2010 - see my code block

Line 4, starts that file name stuff... So now we can take that stuff and my zfilename and glop it togeither to get
So many errors there.... let's pretend that you had assigned it to a variable thus:

Expand|Select|Wrap|Line Numbers
  1.  ZFilename = "File Path" & _
  2.    Format(Date, "ddmmyyyy") & _
  3.    ".xls" & _
  4.    "[Forms]![Qry001_Main_Form]![Select_Team]"
If you place a debug.print zfilename right after this line you're going to get this
(using today's date)
Expand|Select|Wrap|Line Numbers
  1. "File Path11022014.xls[Forms]![Qry001_Main_Form]![Select_Team]"
Exactly, as typed above.

and as others have mentioned, line 6, should be at the end and should be xlsx for Excel2010 (however, for the Ac...8 constaint you used the xls is fine... it's just not an excel10 file).

So let's make a few changes:

More than likely, you're not going to get the value shown from [Select_Team] combobox your quotes are wrong, furthermore, you are going to return the bound-column value... ASSUMING, that there are at least two columns being used in the rowsource (usually is if you are using a query/table as source, if not then we'll deal with that)

Let's modify things a tad further using my code block let's add the combo box

further comments within the code:
Expand|Select|Wrap|Line Numbers
  1. Sub zj_excel_sheet2tablet_2()
  2. Dim ZFilename As String 
  3. Dim zqryout As String
  4. Dim zcbotext as string
  5. Dim zdate as string
  6. '
  7. 'Get the second column of the selected item in the combox
  8. 'if you have two columns then 1st = 0 most likely bound
  9. 'and you can simply call the dot-value of the control
  10. '2nd = 1 and you either have to set focus or use this trick
  11. zcbotext = Forms.Qry001_Main_Form.Select_Team.Column(1)
  12. '
  13. 'Let's format the date... using something sort friendly:
  14. zdate = format(date(),"yyyymmdd")
  15. '
  16. 'and now add these to the path I used before:
  17. ZFilename = "C:\Documents and Settings\All Users\Workbook1" & _
  18. zcbotext & zdate & ".xlsx"
  19. '
  20. 'name of a stored query. Xfer Mthd will NOT use dynamic
  21. zqryout = "Query1"
  22. '
  23. ' and finally
  24. DoCmd.TransferSpreadsheet TransferType:=acExport, _
  25.     Spreadsheettype:=acSpreadsheetTypeExcel12Xml, _
  26.     TableName:=zqryout, _
  27.     Filename:=ZFilename, HasFieldNames:=True_
  28.     ',Range:= "B2:J32" 'uncomment if you want to goto a range.
  29. End Sub
  30.  
ʕ•ᴥ•ʔ
Feb 11 '14 #5

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

Similar topics

3
270
by: zweng | last post by:
I checked a few sites on exporting data from asp.net to excel and most of them point to the same source a http://support.microsoft.com/default.aspx?scid=kb;EN-US;317719 with some modifications My...
1
2452
by: Luis Esteban Valencia | last post by:
Hello Everyone, Iam an intermediate ASP.Net programmer and iam facing a challenging task. I have a table in MS-SQL server database called 'Members'. The table has following fields... ...
0
1359
by: Manikandan | last post by:
Hi, I'm working with .NET framework 1.1 I'm manipulating a excel file using Microsoft Excel 9.0 Object Library. Reading and writing to ordinary cells in excel are fine. I have a excel file, it...
4
10486
by: Chris Gilpin | last post by:
Hey everybody. I have Access set up using VBA to run a bunch of queries automatically, and then export the results into Excel. The only problem is, I want each query to be exported to the same...
1
1255
by: coolcoder2007 | last post by:
Hi, I have Exported some data in a Excel file using this code- Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=dependencymatrix.xls"); ...
3
3016
by: minhtran | last post by:
Hi all Anyone has any idea how to get a Excel header column in VB.NET. Please, help me. Thank you so much in advance
1
2539
by: JFKJr | last post by:
Hi! I have exported an access table data into an excel file using the following code. However, the code exported all the data to the excel sheet starting from 'A' cell (first column). But I want to...
17
7477
by: timleonard | last post by:
I seem to be over my head and can use some help... I have pieced together some code from several internet searched that is supposed to export a query from access to an existing excel file. I also...
0
992
by: miss D | last post by:
Hi, I have Access button with this command: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "MYS", "C:\SERVICE AWARD PROGRAM\REPORTS\reports.xls", True, "MYS" It transfer...
1
9097
by: Emily Walshaw | last post by:
I'm sorry, I know this has been asked a million times, but am completely new to access and need idiot proof help. I have a database and I am struggling to get a field to update based on a combobox...
0
7237
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
7137
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
7349
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,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
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,...
1
5063
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...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.