简体   繁体   中英

Export a Requeried Subform into Excel

I have a subform named subform_View_Search and I Re-queried it based on some user's preference (eg filter or find latest date of specific item).

Now, I want to export this modified subform into an user preferred location. I have the following code but I am not sure if I am on the right track and this code seems does not work ;~(

Private Sub Command_Excel_Inst_Click()
Dim Path As String

Path = CurrentProject.Path & "\" & "TempQueryName" & ".xls"

subform_View_Search.Form.RecordSource = SQL
subform_View_Search.Form.Requery

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "subform_View_Search", Path

End Sub

Your help will be appreciated !

This question is different than other questions because I am creating a excel file from SQL instead of from a query.

Here is my second try. Here is the code of creating a temp query but there are 2 problems. 1. it gives a run time error 3066 2. My code seems doent work to delete the old temporary query

Dim db As Database
Dim Qdf As QueryDef

Dim strQry As String
Dim strSQL As String
Dim Path As String

Path = CurrentProject.Path & "\" & "TempQueryName" & ".xls"

strSQL = SQL '<-From public
strQry = "TempQueryName"

Set db = CurrentDb
Set Qdf = db.CreateQueryDef(strQry, strSQL)

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQry, Path

DoCmd.DeleteObject acQuery, strQry

You could try something like the following, not as easy as you want, but at first look its what I'd try. I'll continue to look though.

Dim f As Form
Dim x As Excel.Application

Set f = Me.Child0.Form

Set x = New Excel.Application
x.Visible = 1
x.Workbooks.Add
x.Worksheets(1).Range("a1").CopyFromRecordset f.Recordset

x.ActiveWorkbook.SaveAs "filename"
x.ActiveWorkbook.Close False

Set f = Nothing
Set x = Nothing

Or create a temp query with the same SQL, and then export this, then delete it. Not sure on what's allowed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM