Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error while creating ADO recordset with VFPOLEDB (2)
Message
From
05/06/2009 06:01:20
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
04/06/2009 18:22:02
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01403827
Message ID:
01403882
Views:
53
>>>Hi Cetin, I asked this question to the whole UT, but wonder if you know the answer.
>>>
>>>Alex
>>>
>>>I have a procedure that uses some code from Cetin to fill Excel workbook from VFP cursors.
>>>
>>>The code creates an ADO recordset from the VFP cursor, saves the recordset to an intermediate rst file, and later opens the rst file from excel.
>>>
>>>This code works well in one procedure but not another. In both cases the code is called twice to fill two Excel sheets. In the routine that fails it does during the second call when the recordset is being saved to the rst file. The error is: OLE IDispatch exception code 0 from Microsoft OLEDB persistence Provider: Multiple-step operation generated errors. Check each status value...
>>>
>>>In each case the code checks to make sure the rst file has been erased before attempting the save.
>>>
>>>Any suggestions?
>>>
>>>TIA,
>>>
>>>Alex
>>
>>Alex,
>>Just yesterday I needed it and the source was a cursor. Instead of saving to RS and reopening it, I now do selecting into a temp table in a temp dbc. Then create an RS from that and use CopyFromRecordset method. I will keep this message and send the code tomorrow when I get to my work computer.
>>Cetin
>
>Thank you. Look forward to it.
>
>Alex

Here is my VFPToExcel.prg:
*!* VFPToExcel.prg
*!* Author : Cetin Basoz
* Parameters:
* tcCursorName
* toRange - target to place
* tcHeaders: Optional. Defaults to field headers
* tnPrefferredWidthForMemo: Optional. Default 80

Lparameters tcCursorName, toRange, tcHeaders, tnPrefferredWidthForMemo
tcCursorName = Evl(m.tcCursorName,Alias())
tnPrefferredWidthForMemo = Evl(m.tnPrefferredWidthForMemo,80)
Local loConn As AdoDB.Connection, loRS As AdoDB.Recordset,;
  lcTemp,lcTempDb, oExcel,ix, lcFieldName, lcHeaders

lnSelect = Select()
lcTemp   = Forcepath(Sys(2015)+'.dbf',Sys(2023))
lcTempDb = Forcepath(Sys(2015)+'.dbc',Sys(2023))

Create Database (m.lcTempDb)
Select * From (m.tcCursorName) Into Table (m.lcTemp) Database (m.lcTempDb)

Local Array aMemo[1]
Local nMemoCount
nMemoCount = 0
lcHeaders = ''
For ix = 1 To Fcount()
  lcFieldName = Field(m.ix)
  If Type(Field(m.ix))='M'
    nMemoCount = m.nMemoCount + 1
    Dimension aMemo[m.nMemoCount]
    aMemo[m.nMemoCount] = m.ix
    Replace All &lcFieldName With Chrtran(&lcFieldName,Chr(13)+Chr(10),Chr(10))
  Endif
  lcHeaders = m.lcHeaders + Iif(Empty(m.lcHeaders),'',',')+Proper(m.lcFieldName)
Endfor
tcHeaders = Evl(m.tcHeaders,m.lcHeaders)

Use In (Juststem(m.lcTemp))
Close Databases
Set Database To

loConn = Createobject('ADODB.Connection')
loRS = Createobject("ADODB.Recordset")
loConn.ConnectionString = "Provider=VFPOLEDB;Data Source="+m.lcTempDb
loConn.Open()
loRS = loConn.Execute("select * from "+m.lcTemp)

* Use first row for headers
Local Array aHeader[1]

toRange.Offset(1,0).CopyFromRecordSet( loRS )  && Copy data starting from headerrow + 1

loRS.Close
loConn.Close

Set Safety Off
Delete Database (m.lcTempDb) Deletetables

Select (m.lnSelect)

For ix=1 To Iif( !Empty(m.tcHeaders), ;
    ALINES(aHeader, m.tcHeaders,1,','), ;
    loRS.Fields.Count )
  toRange.Offset(0,m.ix-1).Value = ;
    Iif( !Empty(m.tcHeaders), ;
    aHeader[m.ix], ;
    Proper(loRS.Fields(m.ix-1).Name) )
  toRange.Offset(0,m.ix-1).Font.Bold = .T.
Endfor

#Define xlJustify                                         -4130
#Define xlTop                                             -4160
* This part is cosmetic
toRange.WorkSheet.Activate
With toRange.WorkSheet.UsedRange
  .VerticalAlignment = xlTop && set all to top
  For ix=1 To m.nMemoCount
    With .Columns(aMemo[m.ix])
      .ColumnWidth = m.tnPrefferredWidthForMemo && 80 chars width
      .WrapText = .T.
    Endwith
  Endfor
  .Columns.AutoFit
  .Rows.AutoFit
Endwith
Sample call:
select First_name, last_name,notes from employee into cursor myCursor nofilter
Local oExcel
oExcel = Createobject("Excel.Application")
With oExcel
  .Workbooks.Add
  .Visible = .T.
  VFPToExcel('myCursor', ;
    .ActiveWorkBook.ActiveSheet.Range("A1") ) && To sheet1, start at A1
Endwith
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform