Hi,
I have tree querys I need to execute on the push of a putton in a form in OObase. So, I need a Macro for that.. Is there simple tutorial on how to do this? Just get the query to execute. Nothing will be returned from the query.
/Fredrik
Macro to execute a simple SQL query in OObase
Macro to execute a simple SQL query in OObase
--
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
Re: Macro to execute a simple SQL query in OObase
Frederik,
see this example. It retrieves a query "NameOfQuery" from the OOo registered data source "NameRegDB" and executes it.
Does this help?
see this example. It retrieves a query "NameOfQuery" from the OOo registered data source "NameRegDB" and executes it.
Code: Select all
Sub xQuery
' Kontext holen
oDatenbankKontext = CreateUnoService ( "com.sun.star.sdb.DataBaseContext" )
' Datenquelle auswählen
oDatenquelle = oDatenbankKontext.getByName( "NameRegDB" )
sQuery = "NameOfQuery"
' Abfragen
oAbfragen = oDatenquelle.QueryDefinitions
' gewünschte Abfrage vorhanden?
if ( oAbfragen.hasByName( sQuery) ) Then
' Abfrage holen
oAbfrage = oAbfragen.getByName( sQuery )
' SQL-Ausdruck von Abfrage entnehmen
sQuery = oAbfrage.Command
' Verbindung zur DB herstellen
oVerbindung = oDatenquelle.getConnection( ,)
' Statement erzeugen
oStatement = oVerbindung.createStatement
' Abfrage absetzen
oResultSet = oStatement.executeQuery( oAbfrage.Command )
' durch alle Resultate iterieren
while oResultSet.next()
' erste Spalte ausgeben
emplName = oResultSet.getString( 1 )
msgbox emplName
Wend
' DB-Ressourcen wieder freigeben
oResultSet.close()
oStatement.close()
Else
MsgBox "Keine Abfrage mit dem Name " & sQuery & " vorhanden"
Exit Sub
End If
' Verbindung schliessen
oVerbindung.close()
Does this help?
Re: Macro to execute a simple SQL query in OObase
Hi,
Yes, it does. Thank you! Although.. getting the database by name is not something I can do. This will be used within the context of a oobase form, and so the database would not have to me registered (I have nooo idea what the name will be even if it was).
So, how do I get a connection to the database to which the form belongs?
/Fredrik
Yes, it does. Thank you! Although.. getting the database by name is not something I can do. This will be used within the context of a oobase form, and so the database would not have to me registered (I have nooo idea what the name will be even if it was).
So, how do I get a connection to the database to which the form belongs?
/Fredrik
--
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
-
- Volunteer
- Posts: 283
- Joined: Tue Dec 04, 2007 6:38 pm
- Location: Houston, TX
Re: Macro to execute a simple SQL query in OObase
the form has a property called ActiveConneciton which refers to its connection
Code: Select all
Dim Conn As Object
Conn=Form.ActiveConnection