1

I’m new to Calc and I use to be able to do something similar to this in Excel / VB. The code below pulls a SP from MySql, and does exactly what I want, except for the While loop. In VB I was able to loop through the recordset and dump it into a Range like so, “ActiveWorkbook.Sheets(1).Range("A5").CopyFromRecordset rs”. Without having to go column by column. Is there anything in Basic-Calc like this? I’ve looked all over and haven't come across anything.

...

Sub  RetriveData()
Dim RowSetObj As Object, ConnectToDatabase As Object

DATABASE_NAME = "file:///home/jod/Documents/test.odb"

DBContext=createUnoService("com.sun.star.sdb.DatabaseContext")
DataSource=DBContext.getByName(DATABASE_NAME)

If Not DataSource.IsPasswordRequired Then
ConnectToDatabase = DataSource.GetConnection("","")
Else
  InteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
ConnectToDatabase = DataSource.ConnectWithCompletion(InteractionHandler)
End If


oURL="file:///home/jod/Documents/CodExm3.ods"
oDoc=StarDesktop.loadComponentFromURL(oURL, "_blank", 0, Array())
oSheet=oDoc.Sheets(0)
oSheet.Name="Tasks"

SQLQuery= "call GVWC_DB.TestSP();"
SQLStatement=ConnectToDatabase.createStatement
RowSetObj=SQLStatement.executeQuery (SQLQuery)

While RowSetObj.Next
i=i+1
oCell=oSheet.getCellByPosition(0,i)
oCell.String=RowSetObj.getString(1)
oCell=oSheet.getCellByPosition(1,i)
oCell.String=RowSetObj.getString(2)
oCell=oSheet.getCellByPosition(2,i)
oCell.String=RowSetObj.getString(3)
oCell=oSheet.getCellByPosition(3,i)
oCell.String=RowSetObj.getString(4)
oCell=oSheet.getCellByPosition(4,i)
oCell.String=RowSetObj.getString(5)
oCell=oSheet.getCellByPosition(5,i)
oCell.String=RowSetObj.getString(6)
oCell=oSheet.getCellByPosition(6,i)
oCell.String=RowSetObj.getString(7)
oCell=oSheet.getCellByPosition(7,i)
oCell.String=RowSetObj.getString(8)
oCell=oSheet.getCellByPosition(8,i)
oCell.String=RowSetObj.getString(9)
oCell=oSheet.getCellByPosition(9,i)
oCell.String=RowSetObj.getString(10)
oCell=oSheet.getCellByPosition(10,i)
oCell.String=RowSetObj.getString(11)
oCell=oSheet.getCellByPosition(11,i)
oCell.String=RowSetObj.getString(12)
oCell=oSheet.getCellByPosition(12,i)
oCell.String=RowSetObj.getString(13)
oCell=oSheet.getCellByPosition(13,i)
oCell.String=RowSetObj.getString(14)
oCell=oSheet.getCellByPosition(14,i)
oCell.String=RowSetObj.getString(15)
oCell=oSheet.getCellByPosition(15,i)




Wend

oSheet.getRows.insertByIndex( 0, 6 )

'Sheet.Columns.removeByIndex(5, 1)

End Sub
...

1 Answer 1

0

The LibreOffice API doesn't like gigantic do-everything-all-in-one-shot commands as VBA does. Instead, all it takes is a simple For loop.

resultSet = SQLStatement.executeQuery(SQLQuery)
While resultSet.next()
    For iCol = 1 To resultSet.getMetaData().getColumnCount()
        oCell = oSheet.getCellByPosition(iCol - 1, resultSet.getRow())
        oCell.String = resultSet.getString(iCol)
    Next
Wend

This works if all you need is a string representation of the data. When using methods such as getInt() with some columns, a For loop may not be the best choice.

Documentation: ResultSet

3
  • Jim, Thank you so much, exactly what I was looking for. Now for a Dumb question, but it doesn't seem to be obvious to me . How would I have the record set load starting in Cell A5? I apologize but i'm still sifting through the Star Basic documentation coming from Excel VB.
    – Jeff O
    Feb 16, 2020 at 20:26
  • resultSet.getRow() + 4 should do it. But it would be better to declare a constant instead of the magic literal 4, something like STARTROW = 5 and then resultSet.getRow() + STARTROW - 1.
    – Jim K
    Feb 17, 2020 at 18:33
  • Thanks Jim worked perfectly, I was trying something similar but was getting errors. I wasn't using the syntax correctly. Well I have a lot of learning to do. Thanks again.
    – Jeff O
    Feb 17, 2020 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.