Send report to Excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wylie72@gmail.com

    #1

    Send report to Excel

    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.

  • Jana

    #2
    Re: Send report to Excel

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

    Private Sub Report_Open(Can cel As Integer)

    Dim dbs As Database
    Dim rst As Recordset

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecords et(Me.RecordSou rce)

    'Put your code to send the recordset to Excel here

    End Sub

    Comment

    • wylie72@gmail.com

      #3
      Re: Send report to Excel

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

      DoCmd.OpenRepor t stDocName, acPreview, , strCriteria

      When I do the above I lose the strCriteria filter.

      Comment

      • John@ViridianTech.com

        #4
        Re: Send report to Excel

        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.

        Comment

        • wylie72@gmail.com

          #5
          Re: Send report to Excel

          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_ReportQuer y".
          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.reco rdsource & " where " &
          me.reports.filt er.

          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.

          Comment

          • Jana

            #6
            Re: Send report to Excel

            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.OpenRecords et(Me.RecordSou rce)

            rstTemp.Filter = Me.Filter

            Set rstFinal = rstTemp.OpenRec ordset

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

            HTH,

            Jana

            Comment

            Working...