-----Original Message-----
Hi Chris,
Below is an example to export data to an excel file using spreadsheet
method with temp table from stored procedure.
Here is the Stored Procedure:
Vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
CREATE PROCEDURE [dbo].[procedure1] AS
select customers.*
into tblDumpit
from customers
GO
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
There is a button in a form with the following code behind it. It includes
a call to an outside sub procedure that refreshes the database window so
the Temp table can be seen by the code:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvv
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
'Runs a stored procedure to create a temp table. Passes
'variables in from ComboBoxes on this form similar to Query By Form
technique.
Dim MySQL As String
MySQL = "Execute Procedure1"
'Runs the Stored Procedure
DoCmd.RunSQL MySQL, True
'This calls a separate subprocedure to refresh the
'Database Window so the TransferSpreadsheet method can find the temp
table.
' See
'Q304256 ACC2000: RefreshDatabaseWindow Method Does Not Work in an
Access
'
http://support.microsoft.com/default.aspx?scid=kb;EN- US;Q304256
'Turn echo off to minimize flashing.
DoCmd.Echo False, "Please Hold..."
Call RefreshTheWindow
'Return focus to the current form.
Me.SetFocus
'DoCmd.SelectObject acForm, "Form1"
DoCmd.Echo True
'Now use TransferSpreadsheet to export the new table.
'First kill the existing file on disk.
'If the file doesn't exist, there is an error handler for it.
Kill "C:\Dumpitxxxxr.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"tblDumpit",
"C:\Dumpitxxxxr.xls"
'Drop the temp table.
DoCmd.RunSQL "Drop Table tblDumpit"
MsgBox "Done!"
Exit_Command0_Click:
Exit Sub
Err_Command0_Click:
DoCmd.Echo True
'If the Kill statement fails above, continue.
If Err.Number = 53 Then
Resume Next
ElseIf Err.Number = 2757 Then
DoCmd.RunSQL "Drop Table tblDumpit"
Resume
Else
MsgBox "Error: " & Err.Number & " - " & Err.Description
Resume Exit_Command0_Click
End If
End Sub
Private Sub RefreshTheWindow()
'Requires reference to Office 9.0 object library.
Dim strSQL As String
Dim cBars As Office.CommandBars
Dim cBarCtl As Office.CommandBarControl
'Select the Database Window so you can refresh it.
DoCmd.SelectObject acTable, , True
Set cBars = Application.CommandBars
Set cBarCtl = cBars.FindControl(msoControlButton, 3812)
If Not cBarCtl Is Nothing Then
cBarCtl.Execute
End If
End Sub
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
In the directory "C:\", you could find the excel file Dumpitxxxxr.xls
Hope it helps
Thank you,
Michael Shao
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
.