By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,301 Members | 3,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,301 IT Pros & Developers. It's quick & easy.

Send report to Excel

P: n/a
I would like to redirect a form to output it's recordset to Excel
rather than the Access report. I have the code to send SQL or a
recordset to Excel, but how do I extract the datasource from the
report? This would be at runtime, either when the form is called from
another form or on the on_open event of the report.

Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You can try this to have the datasource for your report pushed to a
recordset:

Private Sub Report_Open(Cancel As Integer)

Dim dbs As Database
Dim rst As Recordset

Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(Me.RecordSource)

'Put your code to send the recordset to Excel here

End Sub

Nov 13 '05 #2

P: n/a
I'm running into a problem. These reports are being opened with a
filter via docmd:

DoCmd.OpenReport stDocName, acPreview, , strCriteria

When I do the above I lose the strCriteria filter.

Nov 13 '05 #3

P: n/a
I do a similar thing, and it works ok for me....
objAccess.DoCmd.OpenReport sReport, acView, , sWhere

when you say 'lose', do you mean its there in the variable, but the
report seems to ignore it?
or do you mean that strCriteria var is empty?

some things to check.
* did you cut and paste this line of code into this posting? or did
you rekey it? I ask because the two commas after acPreview are
critical to hold the position of the strCriteria argument.
* you don't show the code leading up to this statement, i assume you've
trapped through and actually see the where codition in the strCriteria
var?
* assuming you have the where text in the var, are u sure it works? if
you have some complicated stuff in there, you may just have an error in
it. run the where from access directly to test it.
* if you can't find anything wrong with the strCriteria expression,
then change it anyway to something really simple like "name = 'smith'",
"cost > 100" and see if the report filters on that simple thing.

Nov 13 '05 #4

P: n/a
My problem is when if call the me.recordsource, I get the base
recordset of the report without the criteria in in strcriteria applied.

for example, me.recordsource will give me the string "qry_ReportQuery".
But when I opened the form I had also passed a where condition in
strCriteria of "[Item] = '1224'".

My short term fix has benn to generate a sql string

strMyVar = "Select * from " & me.reports.recordsource & " where " &
me.reports.filter.

This SQL then gets passed to a function export to Excel. But it just
feels to cumbersome and prone to create problems down the line.

Nov 13 '05 #5

P: n/a
Here is another way, whether it's better or not, I can't say:

Dim dbs As Database
Dim rstTemp, rstFinal As Recordset

Set dbs = CurrentDb()
Set rstTemp = dbs.OpenRecordset(Me.RecordSource)

rstTemp.Filter = Me.Filter

Set rstFinal = rstTemp.OpenRecordset

Then use the rstFinal recordset in the code that pushes to Excel.

HTH,

Jana

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.