| View previous topic :: View next topic |
| Author |
Message |
DennisW Newbie

Joined: 20 Nov 2010 Posts: 3 Location: Canada
|
Posted: Sat Nov 20, 2010 6:08 am Post subject: Run queriess iteratively from within a macro or [Solved] |
|
|
I want to run a set of queries iteratively from within a macro or Basic program.
I have a set of queries that where originally created in MS Access. I moved my Access database to Base in OO. The queries all function well in Base (they provide valid results so there is no problem there).
I run an initiating query that performs calculations based on data from 3 source data tables and enters (appends) the result into results table1. After several iterations of the initiating query, a second query appends the data from results table1 into results table2. The data in results table1 is then deleted. and the process is re-initiated only this time a 3rd query is used. The 3rd query uses the data from the 3 source data tables plus the results table2 to perform calculations, and the results are then entered (appended) into results table1. As previously specified, after a given number of iterations of this process, the data from results table1 are appended to results table2. The data in results table1 is deleted, and the process is started again using the 3rd query.
The process is repeated (iterated) a specified number of times.
I want to automate the process, so I would like to simply run the existing queries from within a macro or program that would automatically perform the specified number of iterations.
I need to know the syntax for launching (running) queries from within a macro or Basic program that would control the iterative process.
I have read the beginners guide for Base, and I have read the Developer Guide.
In visual basic there is the command "DoCmd.OpenQuery" like this
DoCmd.OpenQuery "queryname", acViewNormal, acEdit
DoCmd.Close
This command allows me to launch, from within visual basic, a query that was created and saved in the Access database.
I have not been able to find or identify a corresponding command in Basic, and I don't know how create macros without a form.
Dennis W _________________ Dennis W
Last edited by DennisW on Sun Nov 21, 2010 4:51 am; edited 1 time in total |
|
| Back to top |
|
 |
RPG Super User

Joined: 24 Apr 2008 Posts: 2696 Location: Apeldoorn, Netherland
|
|
| Back to top |
|
 |
Sliderule Super User


Joined: 29 May 2004 Posts: 2474 Location: 3rd Rock From The Sun
|
Posted: Sat Nov 20, 2010 11:44 am Post subject: |
|
|
Dennis:
Welcome to OpenOffice Base.
This may be more information compared to what you asked, but, thought I would post it anyway . . . for you and others reading this.
The code below, gets a Query defined in your "Registered Data Name" . . . and . . . runs it.
For documentation of the command:
- API commands by name
http://api.openoffice.org/docs/common/ref/index-files/index-1.html
- Get Query By Name ( Get the SQL defined for the Query )
http://api.openoffice.org/docs/common/ref/com/sun/star/container/XNameAccess.html#getByName
- Execute Query ( Run It )
http://api.openoffice.org/docs/common/ref/com/sun/star/sdbc/XPreparedStatement.html#executeQuery
SAMPLE MACRO CODE to run a Query stored in the "registered database". Includes some 'error messages'.
| Code: | Sub GetRunQuery
Dim oStatement, oDBSource, oConnection, oQueries, oResultSet As Object
sQuery = "MyQueryName" '<< name of Query to run . . . Name is CASE sensitive
sDBName = "MyDatabaseName" '<< registered datasource . . . Change for YOUR DB name name is CASE sensitive
oDatabaseContext = createUnoService( "com.sun.star.sdb.DatabaseContext" )
If oDatabaseContext.hasByName(sDBName) Then
oDBSource = oDatabaseContext.GetByName(sDBName)
oConnection = oDBSource.GetConnection("", "")
oStatement = oConnection.createStatement()
Else
MsgBox "No Registered DB Name: " + sDBName,16,"Please Correct DB Name"
Exit Sub
End If
oQueries = oDBSource.QueryDefinitions '<<queries in datasource
If ( oQueries.hasByName( sQuery ) ) Then
oQuery = oQueries.getByName( sQuery ) '<< the query in question
sQuery = oQuery.Command '<< content of query
Else
BEEP
MsgBox "No query found by name: " + Chr$(13) + Chr$(13) + sQuery _
+ Chr$(13) + Chr$(13) + "Query names are CASE ( UPPER / lower ) sensitive.",16,"Please Correct Query Name"
Exit Sub
End If
oResultSet = oStatement.executeQuery( sQuery ) '<< Execute the Query and send data to oResultSet
End Sub 'GetRunQuery |
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your first post Title ( edit button ) if your issue has been fixed / resolved. |
|
| Back to top |
|
 |
DennisW Newbie

Joined: 20 Nov 2010 Posts: 3 Location: Canada
|
Posted: Sun Nov 21, 2010 4:53 am Post subject: |
|
|
Great response.
I appreciate the help.
Thanks _________________ Dennis W |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|