PDA

View Full Version : Solved: In VBA, how to make a table from a recordset.



AirCooledNut
09-26-2012, 11:39 AM
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 works). The user sets just a few parameters in the form, thus the Me. syntax you will see.

Here's the code so far:

Option Explicit
Public Const gTEMP_TBL As String = "_temp_table"
...
Dim sExecuteQuery As String, rst As dao.Recordset, qdf As dao.QueryDef, bHasProgramCode As Boolean, sFullPath As String, sFileName As String
...code not shown here just gets the query name...
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's where I need to put the recordset into the temporary table gTEMP_TABLE
'
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, gTEMP_TABLE, 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:beerchug:
Access 2010 in Windows 7

AirCooledNut
09-27-2012, 08:00 AM
Got a solution from another forum. They suggested that I create a query that references an open form, like so: SELECT Test.ID, Test.Data
FROM Test
WHERE Test.AField=[forms]![test]![pickone]

Here's what I did:

Here's the query I added that will use the references of the opened form per the 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 semi-colon
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