Hello,
You can Call a procedure from a macro. Here is a link to a working call within Calc → Calc: call a stored procedure.
The adaptation of this to your Base file should be minimal. There are many posts here on the ASK site by me which can give you information on getting data from controls on a form or other necessary code. Specific code needs to be done by you since you have all the information. Internal form names, control names, table & field names are all part of creating this working procedure.
Have given the direction asked for.
Edit:
Have taken some time, created mock tables and entered the corrected Stored Procedure (posted one still not correct even though two changes made already) and used the following Macro called from a push button:
Sub RunStoredProc
Rem Used vs Registered connection since this is a Base file
oConnection = Thisdatabasedocument.CurrentController.ActiveConnection
Rem Prepare statement for Call
oStmt = oConnection.prepareCall("CALL `fetch_students`(?,?)")
Rem set the two variables - These values should come from controls on form
Rem did not create since can be List box, numeric control etc. so just hard coded
oStmt.setInt(1, 1) 'Parameter for Class ID'
oStmt.setInt(2, 16) 'Param for Exam ID'
oStmt.execute()
MsgBox "Done!"
oStmt.close()
End Sub
Other items need to be done. As mentioned the input controls. Also verification this is not a duplicate - verify records don’t already exist. Display of results? Probably other possible concerns such as what if wrong Class or Exam # entered?
Edit 2019-04-29:
MySQL database ------ Test01.odt
After downloading this file, change the extension from .odt
to .sql
. This site does not allow .sql extensions. Use this file to create the MySQL database. This is most easily done through MySQL Workbench. From menu choose Server->Data Import
. Then Import from Self-Contained File
. Name the new schema MyTest
. Tables and modified procedure (added Class_ID) are included within.
Make sure you have MySQL JDBC connection. Connectors can be found here → Connector/J. The .jar file needs to be added to class path. For this see LO documentation → LibreOffice Base Handbook. Some info in Chapter 2 Creating a Database.
Here is the needed Base file ------ MySQLjdbcTestProcedure.odb – added comments to code
Provided the database and connector are installed this should work as is. One form:
Have included some of the error checking and functionality mentioned earlier. The Class ID control is a list box (only unique and valid ID’s). The Exam ID control is a numeric field.
Although much more than asked for and more than should have been asked for, there is still more to be done in my estimation.
Edit #2 for 2019-04-29:
Have decided to add a second Base sample. This uses an HSQLDB embedded database so is all enclosed. However, because of that, functionality is lost. It is being provided simply because, based upon comments, the previous procedure, although fully tested, may prove to be too difficult.
The screen can be examined, the macros are available and the MySQL procedure is saved as a query which, although cannot be executed from there, can still be viewed and copied.
Sample #2 ---- MockHSQLDBProcedure.odb – added comments to code
Edit #3 for 2019-04-29:
Sample #3 ---- HSQLDBnoProcedure.odb — completely operational with HSQLDB and does NOT use Procedure. Code (SQL portion) is not for MySQL.