Hi Andrew and Allan,
Below is the latest code I use in my databases for calling an SOL
Statement from a form. I have a Windows 7 64 bit PC and the database I use
is H2. The code should also work with Base's native database HSQL. I don't
know how it would work with other databases.
These macros normally have other lines in them, mainly creating variables
for use in reading from and writing to form controls, but for clarity, I
have stripped them out in this example. I've added a few comments which
hopefully will explain what is happening.
The example is taken from my TimeLog database which I use for recording
start and end times for each of my daily tasks. The form also has fields
for Job No, Job Class, Time taken and brief details of what was done.
For this example there are two forms –
1. the main form (which includes the 'Display Today's Tasks' button) and
2. a form which displays the results of the SQL commands in a table frame
linked to the table, TodaysTasks
REM ***** BASIC *****
' TimeLog.odb version: Example – Calling an SQL Statement from a form
OPTION EXPLICIT
'Global Variables
' Database Object
Global goConn AS Object 'database connection object
' ExecuteQuery variable
Global goStmt7 AS Object 'statement service object for any Table
' Misc variables
Global gsSQL AS String 'SQL Query string
Global gbInitDBDone AS Boolean 'Flag indicating connection state of Database
' fTimeLog 'Name of TimeLog Form
Global goFormTimeLog AS Object 'Form object
Global gbInitfTimeLog AS Boolean 'Flag indicating initialization state of
Timelog Form
'–------------- Initialization Macros
–-------------------------------------------------------------------------------------------------------------
Sub OpenfTimeLog() ' Called from TimeLog LO Main Menu.
Tools|Customise|Event: Open Document
Dim oCurrentForm AS Object
gbInitDBDone = False
OpenForm("fTimeLog")
oCurrentForm = ThisDatabaseDocument.FormDocuments.getByName("fTimeLog")
goFormTimeLog = oCurrentForm.Component.Drawpage.Forms.getByName("fTimeLog")
End Sub
Sub OpenForm(FormName AS String) 'Called from OpenfTimeLog()
If gbInitDBDone = False Then
ConnectToTimeLogDatabase()
End if
With ThisDatabaseDocument.currentcontroller
If Not .isConnected Then
.Connect
End if
End With
ThisDatabaseDocument.FormDocuments.getByName(FormName).open()
End Sub
Sub ConnectToTimeLogDatabase() 'Called from openForm()
Dim oContext AS Object 'database context object
Dim oDB AS Object
oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDB = oContext.getByName("TimeLog") 'get Database
goConn = oDB.getConnection("","") 'establish connection to database.
goStmt7 = goConn.createStatement() 'create a statement service object
gbInitDBDone = True
End Sub
' ------------------------------ End of Initialization Macros
------------------------------------------------------------------------------------
Sub btnDisplayTodaysTasks() 'Called from a button on the main TimeLog Form
Dim sNow AS String
Dim sTodaysDate AS String
' Drop TodaysTasks Table
gsSQL = "DROP TABLE IF EXISTS TodaysTasks "
goStmt7.executeUpdate(gsSQL)
Wait(250)
' Select today's records
sNow = Now()
sTodaysDate = mid(sNow,7,4) + "-" + mid(sNow,4,2) + "-" + Left(sNow,2)
'Format as YYYY-MM-DD
gsSQL = "CREATE TABLE TIMELOG.PUBLIC.TodaysTasks " _
+ "AS SELECT * FROM TIMELOG.PUBLIC.TimeLog " _
+ "WHERE Date = '" + sTodaysDate + "' "
goStmt7.executeUpdate(gsSQL)
Wait(1000)
' Display table TodaysTask on a Form
ThisDatabaseDocument.FormDocuments.getByName("fDisplayTodaysTasks").open()
End Sub
' ------------------------------ End of Macros
--------------------------------------------------------------------------------------------------
Let me know if anything is not clear.
Noel
--
Marion & Noel Lodge
lodgemn@gmail.com
On 1 September 2014 18:48, Allan Newton <allan@opticaldesign.co.uk> wrote:
Noel,
Thanks to the replies from Andrew and Alex I have my code working.
I do however dislike "cut and paste without understanding" coding which is
almost what I am doing
so any additional code would certainly be interesting to me.
Many thanks
Allan
-----Original Message-----
From: Marion & Noel Lodge [mailto:lodgemn@gmail.com]
Sent: 31 August 2014 07:59
To: users@global.libreoffice.org
Subject: Re: [libreoffice-users] Re: Running a Macro from a form
Hi Allan,
With the help of Andrew and Alex, you may have solved your problem by now.
But if not, I run lots of SQL commands in my Base applications and I could
post the Basic code I use, if that was likely to be of any use to you.
Noel
--
Noel Lodge
lodgemn@gmail.com
On 29 August 2014 19:32, Alex Thurgood <alex.thurgood@gmail.com> wrote:
Le 28/08/2014 13:46, Allan Newton a crit :
Hi Allan,
However if I create blank form with just one button and set the
properties
of that button to
run the macro it fails at the line
if IsNull(ThisComponent.CurrentController.ActiveConnection) then
with the error message
"BASIC runtime error.
Property or method not found: ActiveConnection."
Can anyone help me please?
That error message is telling you (or should be telling you) that you
have no Database Context. ActiveConnection is instantiated via a
database connection context :
http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_
1sdbc_1_1XConnection.html
so the macro in your form has to create such a context before
ActiveConnection can return any value.
As you don't say whether your form has been made within your ODB file,
or whether it is independent, it is hard to help out much further.
Generally, if the form is created within the ODB file, the connection
context is initiated upon opening the main ODB interface, whereas an
independent form needs to establish the connection context first. If
your form is within the ODB file, then one of the reasons why the
connection context might not be found is because it needs to walk up
the window hierarchy to find the main or parent. As far as I know,
this has always been a weakness of the CurrentComponent method within
Base containers where child form windows do not automatically inherit
the connection context. Andrew has indicated a way of obtaining the
connection context from by walking up the Model hierarchy of the
Database container.
From a quick read, the answer to your problem seems to have been
published here :
https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=42882#p1984
77
Alex
--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems?
http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more:
http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be
deleted
--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems?
http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be
deleted
--
To unsubscribe e-mail to: users+unsubscribe@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted
Context
Privacy Policy |
Impressum (Legal Info) |
Copyright information: Unless otherwise specified, all text and images
on this website are licensed under the
Creative Commons Attribution-Share Alike 3.0 License.
This does not include the source code of LibreOffice, which is
licensed under the Mozilla Public License (
MPLv2).
"LibreOffice" and "The Document Foundation" are
registered trademarks of their corresponding registered owners or are
in actual use as trademarks in one or more countries. Their respective
logos and icons are also subject to international copyright laws. Use
thereof is explained in our
trademark policy.