Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:54:34+00:00 2026-06-11T23:54:34+00:00

I have a recordset that I want to export into Excel 2000 format (acSpreadsheetTypeExcel9).

  • 0

I have a recordset that I want to export into Excel 2000 format (acSpreadsheetTypeExcel9). I believe I need to drop it into a table first then execute a DoCmd.TransferSpreadsheet (keeps it easy and it works). The user sets just a few parameters in the form, thus the Me. syntax you will see.

Here’s the working code so far:

Select Case Me.Controls("frame_ChooseReport").Value
    Case 1
        sExecuteQuery = "qry_PDSR w/ Destruct Dates"
        bHasProgramCode = True
        sFileName = "Project_Doc_Submittal_Request_better"
    Case 2
        sExecuteQuery = "qry_PDSR w/Destruct Dates BE"
        bHasProgramCode = False  'This is the only query here that doesn't have a Program Code parameter
        sFileName = "Project_Doc_Submittal_Request_better_BE"
    Case 3
        sExecuteQuery = "qry_Project Documentation Submittal Request w/ Destruct Dates"
        bHasProgramCode = True
        sFileName = "Project_Doc_Submittal_Request_ENH"
    Case 4
        sExecuteQuery = "qry_Project_Doc_Submittal_Request_w_Destruct_Dates_HES_Installer"
        bHasProgramCode = True
        sFileName = "Project_Doc_Submittal_Request_Installer"
    Case Else
        Stop  'Error!  This should never be reached!
End Select
'Execute query & save output to Excel
Set qdf = CurrentDb.QueryDefs(sExecuteQuery)  'Open the query

'Assign values to the query using the parameters option
If bHasProgramCode = True Then
    qdf.Parameters(0) = Me.lbl_ProgramCodes.Section
    qdf.Parameters(1) = Me.txt_StartDate
    qdf.Parameters(2) = Me.txt_EndDate
Else
    qdf.Parameters(0) = Me.txt_StartDate
    qdf.Parameters(1) = Me.txt_EndDate
End If

sFullPath = Me.lbl_SaveTo.Caption & "\" & sFileName
Set rst = qdf.OpenRecordset  'Convert the querydef to a recordset and run it
If rst.BOF = True And rst.EOF = True Then
    MsgBox "No records were found.", vbExclamation, "Empty recordset"
    Exit Sub
End If
'Dump recordset into a table, export it to Excel, then delete it.

'Here is where the recordset needs to become a table.

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_PDSR w/ Destruct Dates", sFullPath, True  'Export table to an Excel format

'Clean up!
DoCmd.DeleteObject acTable, gTEMP_TBL  'Done with the temporary table so delete it
rst.Close
qdf.Close
Set rst = Nothing
Set qdf = Nothing

Help/suggestions? Thank you.

Access 2010 in Windows 7

———- FOLLOW UP ———-

Here’s the query I added that will use the references of the opened form per Remou’s suggestion:

SELECT dbo_PROJECT.PROJECTID, dbo_PROJECT.TITLE, dbo_PROJECT.PROGRAMCODE, dbo_PROJECT.PROJECTTYPE, dbo_PROJECT.REFERENCE, dbo_PROJECT.STATUS, dbo_PROJECT.PMC, dbo_TRANSACTION_SUM.STATUS, dbo_TRANSACTION_SUM.IMPORTEDDT, dbo_TRANSACTION_SUM.CHECKDT, dbo_PROJECT.NOTES, dbo_TRANSACTION_SUM.TRANSACTIONID, dbo_TRANSACTION_SUM.GL_ACCT, dbo_PROJECT_SUM.PAID_INCENT_TOTAL, dbo_TRANSACTION_SUM.AMOUNT
FROM ((dbo_INCENTIVE RIGHT JOIN dbo_PROJECT ON dbo_INCENTIVE.PROJECTID = dbo_PROJECT.PROJECTID) LEFT JOIN dbo_TRANSACTION_SUM ON dbo_INCENTIVE.INCENTIVEID = dbo_TRANSACTION_SUM.INCENTIVEID) LEFT JOIN dbo_PROJECT_SUM ON dbo_PROJECT.PROJECTID = dbo_PROJECT_SUM.PROJECTID
WHERE (((dbo_PROJECT.PROGRAMCODE) In ([Forms]![Submittal_Request_Report]![txt_ListProgramCodeSelections])) AND ((dbo_TRANSACTION_SUM.CHECKDT) Between [Forms]![Submittal_Request_Report]![txt_StartDate] And [Forms]![Submittal_Request_Report]![txt_EndDate]));

Here’s the routine that is in the On_Exit event of the listbox:

Private Sub list_ProgramCodes_Exit(Cancel As Integer)
'Get selection from Program Code listbox and store it in a hidden textbox for the query.
Dim x As Long, sValue As String, ctlSource As Control

sValue = ""
Set ctlSource = Me!list_ProgramCodes
For x = 0 To ctlSource.ListCount - 1
    If ctlSource.Selected(x) Then
        sValue = sValue & ctlSource.Column(0, x) & ","
    End If
Next
Me.txt_ListProgramCodeSelections.Value = Left(sValue, Len(sValue) - 1)  'Drop the last comma
Set ctlSource = Nothing
End Sub

Works great! The SQL line In ([Forms]![Submittal_Request_Report]![txt_ListProgramCodeSelections]) pulls the list of items in the hidden textbox (using a label didn’t work) that was populated with the selection from the listbox on the form.

This is now the code for exporting the query:

Private Sub btn_RunReport_Click()
Dim sExecuteQuery As String, sFullPath As String, sFileName As String

On Error GoTo Err_btn_RunReport_Click
If Left(Me.lbl_SaveTo.Caption, 4) = "save" Then
    MsgBox "Please select a folder to save the results to.", vbInformation, "No folder selected"
    Exit Sub
End If

Select Case Me.Controls("frame_ChooseReport").Value
    Case 1
        sExecuteQuery = "qry_PDSR_Destruct_Dates_form"
        sFileName = "Project_Doc_Submittal_Request.xls"
    Case 2
        sExecuteQuery = "qry_Project_Doc_Submittal Request w/ Destruct Dates_form"
        sFileName = "Project_Doc_Submittal_Request_ENH.xls"
    Case 3
        sExecuteQuery = "qry_PDSR_w_Destruct_Dates_HES_Installer_form"
        sFileName = "Project_Doc_Submittal_Request_Installer.xls"
    Case Else
        Stop  'Error!  This should never be reached!
End Select
sFullPath = Me.lbl_SaveTo.Caption & "\" & sFileName
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, sExecuteQuery, sFullPath, True  'Export table to an Excel format

Exit_btn_RunReport_Click:
    Exit Sub

Err_btn_RunReport_Click:
    MsgBox Err.Description
    Resume Exit_btn_RunReport_Click

End Sub

Thanks Remou!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:54:35+00:00Added an answer on June 11, 2026 at 11:54 pm

    I suggest you just set the sql of a query to a suitable string and then export the query:

    sSQL="SELECT * FROM Table WHERE Field=" & me.MyText
    If IsNull(DLookup("name", "msysobjects", "name='query1'")) Then
        CurrentDb.CreateQueryDef "Query1", sSQL
    Else
        CurrentDB.QueryDefs("Query1").SQL = sSQL
    End If     
    
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", sFullPath
    

    You can create a query that references an open form:

    SELECT Test.ID, Test.Data
    FROM Test
    WHERE Test.AField=[forms]![test]![pickone]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MYSQL Database: I have a table of data that I need to put into
I have a recordset loop that creates a table, and every 9 items it
I have a function that accepts one parameter and returns a table/resultset. I want
I want to export data from one table into a new one with a
I have a Mysql Recordset that I have put into an associative array so
I want to check my database for records that I already have recorded before
I have app in which i have recorded sound files i want that i
I have two employee lists that I want to get only unique records from
So I have a WIN32 app that records videos using DirectShow. Now I want
I want to delete multiple records at once. I have two tables, one that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.