Solved

Export the recordset to excel from VB

Posted on 2008-06-16
6
1,208 Views
Last Modified: 2013-12-26
Hi
    I am using a stored procedure and the setting it equal to a record set. How do i see and export the data to excel via excel object in VB6.


Set cmd = New Command
        cmd.ActiveConnection = con
        cmd.CommandType = adCmdStoredProc
        cmd.CommandText = "PlanActalData"
        cmd.Parameters.Append cmd.CreateParameter("ReportReleaseID", adVarChar, adParamInput, 255, cboReportRelease.Text)
        cmd.Parameters.Append cmd.CreateParameter("ReportCycleID", adVarChar, adParamInput, 255, cboReportTestCycle.Text)
        cmd.Parameters.Append cmd.CreateParameter("ReportBuildID", adVarChar, adParamInput, 255, cboReportBuild.Text)
        'cmd.Parameters.Append cmd.CreateParameter("result", adInteger, adParamOutput)
       ' rsGetActualData.Open cmd, con, adOpenForwardOnly, adLockReadOnly
        'cmd.Execute
        'rsGetActualData.Open cmd.Execute, con, adOpenDynamic, adLockReadOnly
        Dim rsCount As Integer
    'MsgBox rsCount
         Set rsGetActualData = cmd.Execute(ra)
0
Comment
Question by:kshireesh
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
nds_rahulmistry earned 250 total points
ID: 21800015

YOU CAN USE FOLLOWING FUNCTION. YOU HAVE TO PASS THE RECORDSET AS AN ARGUMENT.

THIS WILL CREATE A NEW EXCEL FILE AND EXPORT THE ENTIRE RECORDSET TO THE EXCEL FILE.

CHECK FOR TWO COMMENTED LINES IN THE CODE AND REMOVE THE COMMENT AS PER YOUR NEED.

REGARDS,

RAHUL
Public Sub ExportToExcel(ByVal rs As ADODB.RecordSet)
    Dim oApp As New Excel.Application
    Dim oBook As Excel.Workbook
    Dim oWorkSheet As Excel.Worksheet
    Dim oField As ADODB.Field
    Dim c As Long
    Dim i As Long
 
    On Error GoTo ErrorHandler
 
    Set oBook = oApp.Workbooks.Add
    Set oWorkSheet = oBook.Worksheets.Item(1)
    
    oApp.Visible = False
    
    With oWorkSheet
        c = Asc("A")
        For Each oField In rs.Fields
            .Range(Chr(c) & "1").Value = oField.Name
            .Range(Chr(c) & "1").Font.Bold = True
            c = c + 1
        Next
        
        i = 2
        'IF YOUR RECORDSET CURSOR LOCATION IS LOCAL, THEN REMOVE COMMENT FROM FOLLOWING LINE
        'If rs.RecordCount > 0 Then rs.MoveFirst
        rs.MoveFirst
        While Not rs.EOF
            c = Asc("A")
            For Each oField In rs.Fields
                .Range(Chr(c) & i).Value = rs(oField.Name)
                c = c + 1
            Next
            i = i + 1
            rs.MoveNext
        Wend
    End With
    
    oApp.Visible = True
 
    Exit Sub
 
    GoTo CleanExit
 
ErrorHandler:
    MsgBox Err.Number & ": " & Err.Description
CleanExit:
    If Not oApp Is Nothing Then Set oApp = Nothing
    If Not oBook Is Nothing Then Set oBook = Nothing
    If Not oWorkSheet Is Nothing Then Set oWorkSheet = Nothing
End Sub

Open in new window

0
 

Author Comment

by:kshireesh
ID: 21804253
the fuction does not seems to be working .

thats how i making a function call

 Call ExportToExcel(rsGetActualData).

the error is
3704: Object is not allowed when the object is closed.

i still beleive the anomaly is at Set rsGetActualData = cmd.Execute(ra)
ra = 2 but rsGetActualData.recordcount throws an error.





0
 
LVL 3

Expert Comment

by:nds_rahulmistry
ID: 21811052

The recordset which is passed to the function should be open at the time of function call. I have tried to execute the function and it 100000 % working dear...!   I am using the same function for all my projects from last 8 years...!

Just make sure the recordset object is not closed. Check your cursor type also.... ( client side or server side )  Make necessary changes , and you will definitely get the data in Excel.

RAHUL
0
 
LVL 2

Expert Comment

by:DennisPost
ID: 24545196
Confirmed. This code works perfectly.
Don't forget to add the reference to Excel.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 69
message box in access 4 46
VB6 - Convert HH:MM into Decimal 8 54
Help me. 3 54
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question