Export to excel with variable filename

goldenvision

Board Regular
Joined
Jan 13, 2004
Messages
234
--------------------------------------------------------------------------------

Various posts on here seem to have flirted with the answer I am looking for but I can't seem to find anything specific. Any help would be appreciated.

I am looking to export a paramatised query to excel with a variable file name.

i.e. H:\Projects\CrosstabExport - [paramater value].xls

The threads dealing with adding a date to a file name seem to hint that this is possible but I'm not sure how to go about it.

Thanks in advance.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Something like :-
Code:
Dim strFile As String
strFile = "H:\Projects\CrosstabExport - " & [paramater value] & ".xls"
DoCmd.TransferSpreadsheet acExport, , MyQuery, strFile

HTH

Peter
 
Upvote 0
I have applied your suggestion (see below) and receive the error message

Runtime Error 2465 - Cannot find the field "|" referred to in your expression

Private Sub btn_ExportCrosstabInfo_Click()
Dim strFile As String
strFile = "H:\Projects\CrosstabExport - " & [Payout_Crosstab]![Promotion] & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Payout_Crosstab", strFile, 0
If MsgBox("FILE EXPORTED SUCCESSFULLY. Do you wish to open Excel?", vbYesNo) = vbYes Then
DoCmd.RunMacro ("Open Payout In Excel")
Else
End If

End Sub
[/b]

Any ideas?
 
Upvote 0
I am assuming that the error is coming on the Transfer line.
When the error happens select debug then hover the mouse over the strFile variable and check that it is the value you were expecting.

Peter
 
Upvote 0
it looks like it does not like "[Payout_Crosstab]![Promotion]" then. Try:-
strFile = "H:\Projects\CrosstabExport - " & Forms![Payout_Crosstab]![Promotion] & ".xls"

I am assuming that it refers to a field on a form.

Also I noticed that you have set Field names to false, this makes no difference when you export, see below from Help


hasfieldnames
Use True (-1) to use the first row of the spreadsheet as field names when importing or linking. Use False (0) to treat the first row of the spreadsheet as normal data. If you leave this argument blank, the default (False) is assumed.
When you export Microsoft Access table or select query data to a spreadsheet, the field names are inserted into the first row of the spreadsheet no matter what you enter for this argument.


HTH

Peter
 
Upvote 0
depends on what you are trying to do, What are you trying to extract from the query? is it its criteria or a calculated value?

if it is a value you may be able to use recordset function to get the data. if it is criteria that is hard coded you may be able to put the criteria on a form and link the query to that for the criteria.

Peter
 
Upvote 0
It is a crosstab query that has been paramatised [concession]. It is this paramater than I am trying to select.
 
Upvote 0
I have not used crosstab querys, the only suggestion I have is to try and pass your parameters to it from a form so that you can use the same source in your code.
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,132
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top