Hi Peter,

On Tue, Nov 05, 2013 at 03:12:45PM +0100, Peter Eberlein wrote:
> Hi,
> 
> in the gui there's a command "Data into fields" for a selected row in
> Database sources.  Any ideas how to get the selected row (bookmark of
> the resultset) into these fields with api?  I remember that Fernand
> asked Frank S. a similar question, but didn't found it anymore.

AFAIK there is no API for that, but you can dispatch the UNO command
with the proper arguments sequence; see the attached example, it uses
record numbers instead of bookmarks, but it should work with bookmarks
too (set "BookmarkSelection" to True).


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
REM  *****  BASIC  *****

Option Explicit

Sub Main
        Dim oDoc as Object
        Dim oFrame as Object
        Dim oURLTransformer as Object
        Dim oURL as new com.sun.star.util.URL
        Dim oDispatch as Object
        
        oDoc = 
StarDesktop.loadComponentFromURL("private:factory/swriter","_default",com.sun.star.frame.FrameSearchFlag.ALL,Array())
        oFrame = oDoc.getCurrentController().getFrame()
        oURLTransformer = CreateUnoService("com.sun.star.util.URLTransformer")
        oURL.Complete = ".uno:DataSourceBrowser/InsertContent"
        oURLTransformer.parseStrict(oURL)
        oDispatch = 
oFrame.queryDispatch(oURL,"_self",com.sun.star.frame.FrameSearchFlag.SELF)
        
        If IsNULL(oDispatch) Then
                Exit Sub
        End If
        
        Dim oMasters as Object
        Dim oMasterID as Object
        Dim oMasterAuthor as Object
        Dim sMasterID$
        Dim sMasterAuthor$
        
        sMasterID = 
"com.sun.star.text.fieldmaster.DataBase.Bibliography.biblio.Identifier"
        sMasterAuthor = 
"com.sun.star.text.fieldmaster.DataBase.Bibliography.biblio.Author"
        oMasters = oDoc.getTextFieldMasters()
        
        If oMasters.hasByName(sMasterID) Then
                oMasterID = oMasters.getByName(sMasterID)
        Else
                oMasterID = 
oDoc.createInstance("com.sun.star.text.fieldmaster.Database")
                With oMasterID
                        .DataBaseName = "Bibliography"
                        .DataCommandType = com.sun.star.sdb.CommandType.TABLE
                        .DataTableName = "biblio"
                        .DataColumnName = "Identifier"
                End With
        End If
        
        If oMasters.hasByName(sMasterAuthor) Then
                oMasterAuthor = oMasters.getByName(sMasterAuthor)
        Else
                oMasterAuthor = 
oDoc.createInstance("com.sun.star.text.fieldmaster.Database")
                With oMasterAuthor
                        .DataBaseName = "Bibliography"
                        .DataCommandType = com.sun.star.sdb.CommandType.TABLE
                        .DataTableName = "biblio"
                        .DataColumnName = "Author"
                End With
        End If
        
        Dim oText as Object
        Dim oCursor as Object
        oText = oDoc.getText()
        oCursor = oText.createTextCursor()
        
        oText.insertString(oCursor, "Test:", False )
        
oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False
 )
        
        Dim nFields%
        Dim oField as Object
        For nFields = 0 To 3
                oText.insertString(oCursor, CStr(nFields + 1) + ": [", False )
                
                oField = 
oDoc.createInstance("com.sun.star.text.textfield.Database")
                oField.attachTextFieldMaster(oMasterID)
                oText.insertTextContent(oCursor, oField, False )
                
                oText.insertString(oCursor, "] ", False )
                
                oField = 
oDoc.createInstance("com.sun.star.text.textfield.Database")
                oField.attachTextFieldMaster(oMasterAuthor)
                oText.insertTextContent(oCursor, oField, False )

                oField = 
oDoc.createInstance("com.sun.star.text.textfield.DatabaseNextSet")
                oField.DataBaseName = "Bibliography"
                oField.DataCommandType = com.sun.star.sdb.CommandType.TABLE
                oField.DataTableName = "biblio"
                oField.Condition = "TRUE"
                oText.insertTextContent(oCursor, oField, False )
                
                oText.insertString(oCursor, ".", False )
                
oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False
 )
        Next
        
        
        Dim oDS as Object
        Dim oConnection as Object
        Dim oDatabaseContext as Object
        Dim oStatement as Object
        Dim oResultSet as Object
        
        oDatabaseContext = CreateUnoService("com.sun.star.sdb.DatabaseContext") 
        oDS = oDatabaseContext.getByName("Bibliography")
        oConnection = oDS.getConnection("","")
        oStatement = oConnection.createStatement()
        oResultSet = oStatement.executeQuery("SELECT * FROM ""biblio""")
        
        'Selection - a sequence of Any
        ' record numbers or bookmars
        Dim aSelection(3) as Variant
        'index in the rowset starts at 1
        aSelection(0) = 8
        aSelection(1) = 5
        aSelection(2) = 1
        aSelection(3) = 3
        
        ' see documentation in ::com::sun::star::sdb::DataAccessDescriptor
        Dim aDispatchArgs(6) as new com.sun.star.beans.PropertyValue
        
        aDispatchArgs(0).Name = "DataSourceName"
        aDispatchArgs(0).Value = "Bibliography"
        
        aDispatchArgs(1).Name = "ActiveConnection"
        aDispatchArgs(1).Value = oConnection
        
        aDispatchArgs(2).Name = "Command"
        aDispatchArgs(2).Value = "biblio"
        
        aDispatchArgs(3).Name = "CommandType"
        aDispatchArgs(3).Value = com.sun.star.sdb.CommandType.TABLE
        
        aDispatchArgs(4).Name = "Cursor"
        aDispatchArgs(4).Value = oResultSet
        
        aDispatchArgs(5).Name = "Selection"
        aDispatchArgs(5).Value = CreateUnoValue("[]any", aSelection)
        
        aDispatchArgs(6).Name = "BookmarkSelection"
        aDispatchArgs(6).Value = False ' = we use record numbers in "Selection"
        
        oDispatch.dispatch(oURL, aDispatchArgs)
End Sub

Attachment: pgpz15Qo7TbOb.pgp
Description: PGP signature

Reply via email to