Macro Examples in LibreOffice Help Don't Work

I’m trying to use a macro to manipulate data in a Form Control. I searched LibreOffice 7.3 Help and found an example macro and very quickly discovered that it doesn’t work. This happens MOST of the time with these examples.

Notice in the script below, that the myDoc object hasn’t been declared. Well I got around that by instantiating myForm in another way. Then I got the error message telling me that the “Controls” property or method cannot be found.

I feel like Thomas Edison, learning by trial and error all of the 10,000 ways that don’t work. Is there a reliable source for proper syntax and examples?

Dim myForm As Object, myList As Variant, myControl As Object
Set myForm = myDoc.Forms(“myForm”)
myList = myform.Controls()
Set myControl = myform.Controls(“myTextBox”) ’ SFDocuments.FormControl

I’m running LibreOffice 7.3.2.2 (x64) on Windows 10.

Hello,
Please provide a link to where you found this. It appears as incomplete.
.
Would guess this may be something with ScriptForge - not a fan of it.
.
Please see this post which contains a number of links to documentation and macros > Libreoffice Writer: Macro can’t get the value of a table control
.
Edit:
OK found the post - it is ScriptForge and you have just a piece of it > Service invocation
.
It is supposed to be an easier way to code macros than the uno API but I find little good in it. I have a few related posts on it. Here are two - one Base (in python) and the other for Calc:
.
Calc > Calc Basic ScriptForge CopyToCell doesn't always work - #5 by Ratslinger
.
Base > ScriptForge Database Demo
.
The Base one is where I lost a lot of interest in ScriptForge.
BTW saw your message while I was adding to this.

Thanks for the link. I will check it out. In the meantime here’s the URL to the LibreOffice page where I found that example. It appears most of the way down the page, under the section titled, “Controls”

https://help.libreoffice.org/7.3/en-US/text/sbasic/shared/03/sf_form.html?&DbPAR=SHARED&System=WIN#Controls

If you are going to use ScriptForge, you need to start at its beginning > The ScriptForge Library.
.
The documentation is somewhat difficult to understand and not all items work. Took me some time to get the basics going in Base.

None of these links have the answer I need. I’m going to take a totally different approach.

Actually I would be shocked if there wasn’t something in the Pitonyak library.
.
Regardless, I have posted many samples here and if you explain a bit further what you want I am sure I can help with a sample or two. The question states:

but that can mean a lot from data in various controls to data posted to actual records.
So please provide more specific information.

I have a subform containing a table of numbers that I want to add up and display the total on the parent form. So it’s a text box on the parent that shows the sum of the numbers from the table in the subform.

I used to do this all the time in MS Access and it didn’t even require code.

Instead of adding up something in a sub form, just execute a SQL statement in the macro summing the column and insert the result in the wanted field. See > How to populate text fields from a sql query.

I appreciate your quick responses and your apparent intention to help, but your links to five year old postings that are barely relevant, and don’t actually contain the answer to the question, do not help. Links to reference libraries that don’t explain anything, or provide incorrect and incomplete examples are also merely a wild goose chase.

Your most recent suggestion to use a SQL statement to sum a column is an excellent idea (which I already had) but it doesn’t explain how to put the results into the textbox control on the parent form.

Please note that my original problem was with the example that I found in the LibreOffice 7.3 Help page. The example was incomplete and possibly incorrect since when I ran that code the Controls() property or method of the Form object could not be found.

I need to know how to activate that Control so I can set the Text property.

I will put together a Base file with the code which may be what you want. I am confused because in this post > Text to Control you demonstrated the knowledge to do this.
.
Have already explained the sample you found on the Help page.
.
As far as ‘old’ examples, there is little (especially in Base) which has changed in many years. So most examples from five or ten years ago are relative today.
.

This is mainly why you were provided with many of those links but included are the Pitonyak documents which are the most complete and single source location for LO macros. And as for:

this is Open Source software. If there are items to be fixed or corrected, anyone can volunteer to help to correct this.

@James_Zimmerman
Attached is a sample with one form. It has a main and sub form and button to total the sub form using an SQL query. There is some basic error checking provided and comments in the code.
.
AddSubToMain.odb (14.8 KB)

Hi @Ratslinger
Thank you for the awesome help! Your code works PERFECTLY here. However mine still doesn’t. Specifically, ThisComponent.DrawPage.Forms.getByName() (error message says “Property or method not found: DrawPage”) and when I activate oForm using ThisDatabaseDocument.FormDocuments.getbyname(“myForm”) the oForm object activates but then oForm.IsNew generates the error " Property or method not found: IsNew"

Obviously there is something different between the ODB you sent and the one I created here. I will quit testing Macro code and try to find out what is different between the two ODB containers.

The only way for me to help is for you to post a sample of your Base file - no personal or confidential info please.
.
Also, ThisComponent can be different depending upon where it is executed from. See > ThisDatabaseDocument vs ThisComponent

Well, I tried. I got a few things working but ran into some dead ends. I appreciate you taking a look at the ODB I’m uploading.
HFG_Interface.odb (26.6 KB)

Two problems with your sample. The control you used for the display of the total was a text box. This is better as numeric field as in my sample. Data for the text field would be .Text vs .Valueused & in my sample. Easy to change a control type. Edit the form. Select the control then right mouse click & select replace with and select numeric field.
.
The second issue was the field for the total was on the sub form and you were trying to access it on the main form:

saveTotal = oForm.getByName("txtTotalSale")

You need to access it on the sub form with:

saveTotal = oForm.getByName("SubForm").getByName("txtTotalSale")

and with those corrections you get:


.
Note the total does not seem correct. That is due to you hard coding the SQL to get SaleID = 3.

Thanks again for the help. I’m going to migrate these improvements into my production database and figure out how to move the “Total” field out of the subform and back onto the main form.

When editing the form, open the Form Navigator, locate the control on there, then with the mouse left button drag it from the sub form to the form.

Completely without macro: https://ask.libreoffice.org/uploads/short-url/nW2PUGMTAXtJcBy67iiy8RvNj8m.odb

@Villeroy: Thanks for the wonderful alternative to macro coding. Your example works great! I will try setting up a form and subform in my database using your technique. I didn’t know that we can launch a preset Form Method from a Button control, or set the Value property of a Text control to display the results of a query.

Open the form for editing.
Open the form navigator (menu:Form> Form Navigator).

  1. The grey boxes on top are just indicators for the date range that is stored in the “Data” table. They belong to the main form “Form_MiniMax”.
  2. The second main form “Filtering_MainForm” with yellow boxes is linked to the following query:
SELECT * FROM "Filter" WHERE "FID"=1

This filtering form is only for entering criteria values in one particular record (where FID=1), therefore we disallow everything except “Allow modification”. The user must not tab into any other record and we do not need any navigation bar for a single record.

  1. The filtering form has a “Filtered_SubForm” (green grid) which is linked to the parameter query “qFiltered_Input” which goes like this:
SELECT "PID", "CID", "D", "V", "ID" FROM "Data" 
WHERE ( "PID" = :paramP OR :paramP IS NULL ) 
AND ( "CID" = :paramC OR :paramC IS NULL ) 
AND ( "D" >= :paramD1 OR :paramD1 IS NULL ) 
AND ( "D" <= :paramD2 OR :paramD2 IS NULL )

The parameters paramP, paramC, paramD1 and paramD2 are substituted by their respective parent fields INT1,INT2,D1 and D2 in the parent form. Because this query selects everything from a single table “Data”, including the primary key, the resulting record set is editable. Two listboxes in the grid represent the person names and the category names for the corresponding person IDs and category IDs.

  1. The filtered subform has its own subform “Aggregate_Form” with white boxes. This form is linked to query “qFiltered_Aggregate” which goes like this:
SELECT SUM( "Value" ) "Sum", COUNT( "Value" ) "Count", MIN( "Value" ) "Min", MAX( "Value" ) "Max", AVG( "Value" ) "Average" 
FROM "qFiltered"

and “qFiltered” goes like this:

SELECT "D"."D" AS "Date", "P"."N" AS "Person Name", "C"."N" AS "Category", "D"."V" AS "Value", "D"."V" * 0.15 AS "VAT" 
FROM "Categories" AS "C", "Data" AS "D", "Persons" AS "P", "Filter" AS "F" 
WHERE "C"."ID" = "D"."CID" AND "P"."ID" = "D"."PID" 
AND "F"."FID" = 1
AND ( "D"."PID" = "F"."INT1" OR "F"."INT1" IS NULL ) 
AND ( "D"."CID" = "F"."INT2" OR "F"."INT2" IS NULL ) 
AND ( "D"."D" >= "F"."D1" OR "F"."D1" IS NULL ) 
AND ( "D"."D" <= "F"."D2" OR "F"."D2" IS NULL )

which merges the data table, the filter criteria in the filter table (where FID=1) and the names from the categories and persons in one query. This record set merged from all tables is not editable like the parameter query from a single table. It is used for the calculated aggregation (sum, count averages) and you may create a report from this query showing the exact same records as the green subform grid in a printable layout.

The key to databases is SQL not Basic.

P.S. One more thing regarding the 2 push buttons (see form navigator window):
btnCancel belongs to the yellow filtering form and resets the form to its stored state before editing.
btnRefresh (the OK button) belongs to the green filtered form. When you click it it takes away the focus from the filtering form. A modified form losing the focus is stored automatically. Then the button refreshes the filtered form it belongs to.