Ask Your Question
0

How to make a ResultSet Read/Write

asked 2020-06-29 06:15:19 +0100

Bear31 gravatar image

I have a DRAW document and BASE document. The BASE document has tables that contain data which relates to different shapes in the DRAW document.

I have written a macro that will display data related to a selected shape and display it in a Dialog.

Clicking the "Cancel button on the Dialog causes it to close, so all good up to that point.

I want to be able to edit the TextBoxes on the Dialog and then click the OK button to save the updated data back to the BASE document but when I try to do this I get an error message that the "Result Set is Read Only".

I've used MRI to examine the Connection to determine that it is Read/Write, but do not see a Read Only property for the Result Set.

I have manually tested the SQL statement in the BASE GUI and I can update the fields that way, so I think my SQL is OK.

The OOo BASIC Guide tells me there is a ResultSetConcurrency Variant that controls whether the Result Set can be modified (but doesn't go so far as to explain how to modify it). Using MRI I can see that ResultSetConcurrency is a number which is ReadOnly. I'm guessing that I need to set this value to something else, somehow, as part of the ExecuteQuery method. Half a day of looking through various documents and forums has not enlightened me :(

I've attached my Macro(s) that relate to the problem I'm stuck at, so perhaps someone can point me in the right direction (please!)
BTW - these macros are still in very early development, so maybe there is some fundamental flaw here somewhere.

Option Explicit

Global FibreDoc As Object
Global Connection As Object
Global DataSource As Object
Global DatabaseContext As Object
Global bPitSaveFlag as Boolean
Global oPitDialog as Object

Sub SetDefaults

    Dim InteractionHandler as Object

FibreDoc = ThisComponent
DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
DataSource = DatabaseContext.getByName("Fibre-data")

If Not DataSource.IsPasswordRequired Then
  Connection = DataSource.GetConnection("","")
Else
  InteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  Connection = DataSource.ConnectWithCompletion(InteractionHandler)
End If
'Connection.SetReadOnly(False)
'mri(Connection)
End Sub


Sub ShowPitDialog
      Dim oCurrentSelection As Variant
      Dim oPit As Variant
      Dim sPitName as String
      Dim iNameLength as Integer
      Dim iSepPos as Integer
      Dim iPitID as Integer
      Dim sPitSQL as String
      Dim sTypeSQL as String
  Dim oPitStatement as Object
  Dim oTypeStatement as Object
  Dim oResultSet as Object
  Dim oTypeResult as Object
  Dim oTypeList as Object
  Dim sTitle as String
  Dim iCount as Integer


   Globalscope.BasicLibraries.LoadLibrary( "MRILib" )

  If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then 
      GlobalScope.BasicLibraries.LoadLibrary("Tools")
  End If      

  bPitSaveFlag = False 'Indicator to save updated data

 'mri(Connection)

  oCurrentSelection = FibreDoc.getCurrentSelection()
  oPit = oCurrentSelection.getByIndex(0)
  sPitName = oPit.Name  'The name of the object that is selected
  iNameLength = len(sPitName)
  iSepPos = InStr(sPitName,":")  'Location of the separating ":" in the name
  iPitID = Right(sPitName,(iNameLength-iSepPos)) 'The number to the right of the separating ":"

  sPitSQL = "SELECT ""ID"", ""Lat"", ""Long"", ""Type"", ""Comments"" FROM ...
(plus)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2020-06-29 19:37:19 +0100

Ratslinger gravatar image

updated 2020-06-29 19:40:24 +0100

Hello,

When using executeQuery you are reading. With 'executeUpdate` you are writing.

So you have at least two choices. In the first, have two connections for statements. See this post -> How to scroll through a table in base

That same post has a link to -> firebird equivalent for resultset.last which uses a RowSet to do what you want. Tested with this code:

Dim oRS as Object
oRS = createUnoService("com.sun.star.sdb.RowSet")
oRS.DataSourceName = ThisComponent.Location
oRS.CommandType = com.sun.star.sdb.CommandType.COMMAND
oRS.Command = sPitSQL
oRS.Execute
oRS.First
oRS.updateString(2, "hello")
oRS.updateRow()

There is a lot to cover with this situation. The first link above contains many other links which are most helpful in covering this topic in more detail.

Here is more information on RowSet -> https://www.openoffice.org/api/docs/c...

and also for CommandType in RowSet -> https://www.openoffice.org/api/docs/c...

NOTE: This was tested with HSQLDB v1.8 embedded database. You have not specified what DB you are using.

edit flag offensive delete link more

Comments

Thanks again @Ratslinger for your great advice. I've modified my code to use RowSet and now it works as I want. You've saved me much frustration of banging my head on the brick wall at the end of the dead-end alley I'd navigated myself into. Also, thanks for the link to Andrew Pitonyak's BASE programming guide - I'm sure it will be a useful resource. FYI - I'm just using the built-in BASE database system, not connecting to anything external (for the moment).

Bear31 gravatar imageBear31 ( 2020-06-29 23:58:32 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-06-29 06:15:19 +0100

Seen: 97 times

Last updated: Jun 29 '20