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