473,785 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export SQL stored procedure to Excel file

dbushcmohle
6 New Member
Hello,
I am having problems exporting a stored procedure's results to an Excel
file... I've done this many times successfully, but never had to introduce a
variable. Now that I've introduced the variable, I'm having problems. It is
presenting the error listed below on the last line in my code section below.

ERROR: Method ‘Open’ of object ‘Workbooks’ failed

Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As New Excel.Application 
  2. Dim wbk As Excel.Workbook 
  3. Dim wksht As Excel.Worksheet 
  4. Dim strDate1 As String 
  5. Dim strDate2 As String 
  6. strDate1 = Forms!frmDailyCounts!cntrl_Calendar.Value 
  7. strDate2 = Format(strDate1, "yyyy-mm-dd") 
  8.  
  9. DoCmd.OutputTo acOutputStoredProcedure, _
  10.                "EXEC pDailyCounts '" & strDate2 & "'", _
  11.                "Microsoft Excel (*.xls)", _
  12.                "c:\dbase\DailyCounts.xls" 
  13.  
  14. xlApp.Workbooks.Open "c:\dbase\DailyCounts.xls" 
I could be totally wrong, but here is what I think is happening... When the
acOutputStoredP rocedure function runs, it automatically names the sheet on
the Excel file the name of the stored procedure... Now that I've introduced
the variable, it is trying to name the sheet "EXEC pDailyCounts '" & strDate2
& "'"... After I get my error, I can go to the file location, open it, and
it says Excel is attempting repair an invalid sheet name. Once I let it fix
the file, and rename the sheet name to "RECOVERED" the file opens and I can
see all of my results...

Anyone got any ideas on this one...?

Thanks,
-Dave

P.S. - I am working in Office 2007...
Sep 22 '07 #1
3 7637
NeoPa
32,578 Recognized Expert Moderator MVP
The only thing I can think of is to have a QueryDef that executes the code (you can update its SQL before the export) and use the QueryDef in the OutputTo command. A kludge, I know :(
Sep 22 '07 #2
dbushcmohle
6 New Member
Hello,

I wanted to let everyone know that I FINALLY found out how to do it... Below is my final code... Hopefully it can help someone else in the future...!

Thanks,
-Dave

Expand|Select|Wrap|Line Numbers
  1. Dim strDate1 As String
  2. Dim strDate2 As String
  3. Dim sqlSP
  4.  
  5. strDate1 = Forms!frmDailyCounts!cntrl_Calendar.Value
  6. strDate2 = Format(strDate1, "yyyy-mm-dd")
  7.  
  8. sqlSP = "EXEC pDailyCounts @CalDate='" & strDate2 & "'"
  9.  
  10. DoCmd.OutputTo acOutputStoredProcedure, sqlSP, "Microsoft Excel (*.xls)", "c:\dbase\DailyCounts.xls"
  11.  
  12. xlApp.Workbooks.Open "c:\dbase\DailyCounts.xls"
Sep 23 '07 #3
NeoPa
32,578 Recognized Expert Moderator MVP
Dave,

What is the name of the worksheet created when this code executes? I'm surprised this works, but good for you for finding this solution :) It's less of a kludge than mine.

-NeoPa.
Sep 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5036
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table. Before I iterate through the recordset I instruct the browser that the content type is Excel using the following line: (Response.ContentType = "application/vnd.ms-excel") This works fine with Excel 2003 but with older versions (I tested Excel...
0
1290
by: Vadim Vulfov | last post by:
My task was to create Crystal Report on ASP.NET page to display different date depending on to textboxes From Date and To Date. I created stored procedure in SQL with two parameters from_date and to_date. I created dataset object in ASP.NET (drag and drop on it the stored procedure I just created) and save as XSD file. In the Crystal Report selected all the fields and included into the
3
11689
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a DataSet that consists of one DataTable. That DataTable has a specific name (for arguments purposes, let's say it's called "DataGrid"), that I need to use as the tab name in the exported Excel
3
2604
by: ABC | last post by:
What methods can export excel file from stored procedure with parameters which input from web form?
4
15013
by: Jiro Hidaka | last post by:
Hello, I would like to know of a fast way to export data source data into an Excel sheet. I found a way from C# Corner(Query Tool to Excel using C# and .NET) which is a neat little way of exporting dataset data to an excel using the Excel COM object. This works fine but the problem is its pretty darn slow when exporting large
0
2160
by: Mariana | last post by:
Hello, I am exporting reports in pdf format from .net 2005 . The export is working fine; however the user values of stored procedure parameters does not show in the report. What do I need to do in order to see the parameters value in the report? The report has a report parameter "reportTitle" and i see the value in the report. It has also another 2 stored procedure parameters @BeginDate and @EndDate. The values are entered be the user;...
2
7298
by: Guvnor | last post by:
Hello there, I have about 30 sql servers runnign multiple databases amd i need to audit them so what i want to do is to log onto each server and then maybe export the database properties into an excel sheet or text file. Anyone know how to do that i am quite new to sql any help will be awesome. Regards and thanx in advance
5
2539
by: Reggie | last post by:
Hi and TIA! I have an export procedure that exports my data from my datagrids to excel. The problem is that I lose leading zero's cause excel removes them and doesn't treat them as text. Is it possible to design an excel template file, place it in my shared network folder and have the procedure write data to the template file? I've thought of reading the recordset line by line, field by field and concatenating an apostrophe at the...
2
6421
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the method.... it creates 6 sheets # region Namespaces using System;
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.