Ask Your Question
0

Running parameter query using macro on a form.

asked 2019-07-06 18:37:46 +0100

LO_user1 gravatar image

updated 2019-07-07 01:59:47 +0100

Hi, I have to insert record in a Subform by pushing a button on Mainform using following macro. This macro use a query with a parameter ":sid". This parameter value have to be taken from Mainform control "Stud_ID".

SUB Insert_Fee
    DIM oDatabaseFile AS OBJECT
    DIM oQuery AS OBJECT
    DIM stQuery AS STRING
    Dim oForm  As Object
    Dim Result
    Dim iStudNumber  As Integer
    Dim oStmt As Object
    oDatabaseFile = ThisComponent.Parent.CurrentController.DataSource
    oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
    iStudNumber = oForm.Columns.getByName("Stud_ID").Value
    oQuery = oDatabaseFile.getQueryDefinitions()
    stQuery = oQuery.getByName("Insert_Fee").Command
    stQuery = Replace(stQuery, ":sid","?")
    oConnection = Thisdatabasedocument.CurrentController.ActiveConnection
    oStmt = oConnection.createStatement()
    oStmt.ResultSetType = com.sun.star.sdbc.ResultSetType.SCROLL_INSENSITIVE
    oStmt = oConnection.prepareCall(stQuery)
    oStmt.setInt(1, iStudNumber)
    oResult = oStmt.execute()
    oStmt.close()
    oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
    oForm.Filter = "( Stud_ID = " & iStudNumber & ")"
    oForm.Reload()
END SUB

The query name is "Insert_Fee" which is following. Subform taken from table "fee_trans".

INSERT INTO "fee_trans"("Type","Month","ST_ID","Amount") VALUES ('TF','JUN',(SELECT "ST_ID" FROM "Fee_Define" WHERE "ST_ID" = :sid),(SELECT "Fee" FROM "Fee_Define" WHERE "ST_ID" = :sid))

This macro insert 3 values only but the fourth one remain empty with no error showing. I am using LO version 6.2 on Windows 10. Database is local HSQLDB. Can you please check the macro code and query and point out where is the problem.

edit retag flag offensive close merge delete

Comments

Edited for readability.

SM_Riga gravatar imageSM_Riga ( 2019-07-06 18:57:11 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-07-06 19:23:10 +0100

Ratslinger gravatar image

updated 2019-07-06 22:23:09 +0100

Hello,

It appears your macro is working just as you coded it to do.

stQuery = Replace(stQuery, ":sid","?")

:sid appears twice in your Insert statement. Therefore you need two different substitutions.

oStmt.setInt(1, iStudNumber)

is one but you need to fill the second:

oStmt.setInt(2, iStudNumber)

Now as for the form, you are reloading oForm and filtering the result with iStudNumber. Seems this would display one record. If not what is wanted, why filter?

Also, please include vital information in your questions - specific LO version; OS; database used.

Above may differ depending upon what is being used.

Edit:

Have done some further reviewing. Seems I have previously presented you with how to set multi-parameters in this post -> Using SQL cursor and loop in LO base macro. .

Also, these posts may interest you:

SOLVED: Read values from selected rows in a table control

Base Table Grid Control restore the rows selection after applying a filter

edit flag offensive delete link more

Comments

Hi, Ratslinger! Thank you for your quick answer. I have corrected my code as you guided and it is working fine. The filter portion is Ok so I edited my question. (You can edit your answer) The code is working with one parameter when I use it with mysql database and this query is a procedure there. I call it as ("CALL Insert_fee(?)") as you advised in my previous question.

LO_user1 gravatar imageLO_user1 ( 2019-07-07 02:12:25 +0100 )edit

Simply confused at this latest comment. My answer already answers your question since you dropped the record displayed portion.

The previous question (you mentioned & link in my answer) has two parameters:

"CALL `fetch_students`(?,?)"

This is also what you have in your sample above :

stQuery = Replace(stQuery, ":sid","?")

Since :sid occurs twice, there will be two ? in the statement which need to be filled. My answer already states the second one needing to be added:

oStmt.setInt(2, iStudNumber)

As far as I can tell your question was already answered before you modifications or comment. Am I missing something? Maybe the confusion is you stating - You can edit your answer. Don't know why it needs editing.

Ratslinger gravatar imageRatslinger ( 2019-07-07 03:19:44 +0100 )edit

Actually my previous question and your answer was on a procedure with cursor and that have two parameters to insert students marks. But later I used the same macro you provided with the simple SQL insert query shown in this question with one parameter in the procedure to insert student fee. I understood that it need two time for local query because [:sid] was used two time but one time with mysql procedure because you have one parameter defined there although you are using in two places.

LO_user1 gravatar imageLO_user1 ( 2019-07-07 17:09:15 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-07-06 18:37:46 +0100

Seen: 59 times

Last updated: Jul 07