bombdropVB
Programmer
Hi I'm trying to export to excel via vb ( DTS in SQL Server will not work du to the fact the sp uses a curssor)
the sp returns 16 recordset not all them filled. the code is ment to loop through the recordset export them to a idervidual sheet in a excell spread sheet. it does the first couple then forgets the rest any help anyone!!!!
Thanks guys![[thumbsup] [thumbsup] [thumbsup]](/data/assets/smilies/thumbsup.gif)
the sp returns 16 recordset not all them filled. the code is ment to loop through the recordset export them to a idervidual sheet in a excell spread sheet. it does the first couple then forgets the rest any help anyone!!!!
Code:
Option Explicit
Private Sub Command1_Click()
Dim conExport As ADODB.Connection
Dim recExport As New ADODB.Recordset
Dim oxlApp As Excel.Application
Dim oxlBook As Excel.Workbook
Dim oxlSheet As Excel.Worksheet
Dim blnRecordGotSets As Boolean
Dim intSheetCounter As Integer
Set conExport = New ADODB.Connection
Set recExport = New ADODB.Recordset
' Open the connection.
conExport.Open _
"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=ace;Data Source=LVP-GANDAPI"
'open the recordset will return multiple
recExport.Open "sb_testc", conExport
' Open the destination Excel workbook.
Set oxlApp = New Excel.Application
Set oxlBook = oxlApp.Workbooks.Add
If recExport.EOF Then
Set recExport = recExport.NextRecordset
If recExport.EOF Then
blnRecordGotSets = False
Else
blnRecordGotSets = True
End If 'recExport.EOF
Else
blnRecordGotSets = True
End If 'recExport.EOF
intSheetCounter = 0
Dim strHandler As String
While blnRecordGotSets = True
intSheetCounter = intSheetCounter + 1
If intSheetCounter > 3 Then
oxlBook.Worksheets.Add
End If 'ntSheetCounter > 3
Debug.Assert intSheetCounter <> 4
'While Not recExport.EOF
oxlBook.Worksheets(intSheetCounter).Cells(1, 1) = "Our Ref"
oxlBook.Worksheets(intSheetCounter).Cells(1, 2) = "Clientshort"
oxlBook.Worksheets(intSheetCounter).Cells(1, 3) = " handler"
oxlBook.Worksheets(intSheetCounter).Cells(1, 4) = " Description"
oxlBook.Worksheets(intSheetCounter).Cells(1, 5) = " Reserve"
strHandler = recExport!handler & ""
oxlBook.Worksheets(intSheetCounter).Range("a2").CopyFromRecordset recExport
Set oxlSheet = oxlBook.Worksheets(intSheetCounter)
oxlSheet.Name = strHandler
Set recExport = recExport.NextRecordset
If recExport.EOF Then
Set recExport = recExport.NextRecordset
If recExport.EOF Then
blnRecordGotSets = False
Else
blnRecordGotSets = True
End If 'recExport.EOF
Else
blnRecordGotSets = True
End If 'recExport.EOF
Wend
'
oxlBook.SaveAs "c:\ACE Open by handler.xls"
MsgBox "finished"
oxlBook.Close False
oxlApp.Quit
recExport.Close
conExport.Close
Set oxlBook = Nothing
Set oxlApp = Nothing
Set recExport = Nothing
Set conExport = Nothing
End Sub
Thanks guys
![[thumbsup] [thumbsup] [thumbsup]](/data/assets/smilies/thumbsup.gif)