Simple addition calculation Macro in Base

Just downloaded LibreOffice a few days ago. I am running version 7.2.5.2 on Mac OS 12.1. I have looked all over and can’t find an answer and can’t wrap my head around this.

We’re trying to transfer everything over from MS Access to Base. I need to get the database to do a simple calculation though.

I have fields for people that give every month named "Jan Total, Feb Total, Mar Total’ and so on. I need to add all those together into another field I have named “Year To Date.” I need it to all be in the same database and not in a separate query. I found an answer on here (and now can’t find again) that said to either do it as a query and save the file, close, and re-open to update it or have it automatically update using a Macro. I really need it to automatically update.

If there is another way, please let me know.

I’ve been looking at the Macro code the other poster had I was trying to see about changing it to fit my scenario, but can’t get it working. Any help would be greatly appreciated. Thanks so much.

This is the code the other person had for their macro:

REM  *****  BASIC  *****

Option Explicit

Sub UpdateVat

Dim oForm		 As Object
Dim oColumns	 As Object
Dim oVat		 As Object
Dim oTotalCost	 As Object
Dim dVatValue	 AS Double
Dim dDelCost	 AS Double
Dim dCost		 AS Double
Dim dTotalCost	 AS Double

oForm = ThisComponent.Drawpage.Forms.getByName("MainForm") 'Get Form to access payment grid
dDelCost = oForm.fmtDELCOST.Value
dCost = oForm.fmtCOST.Value
dVatValue = dCost * .2
oForm.fmtVAT.Value = dVatValue
dTotalCost = dCost + dDelCost + dVatValue
oForm.fmtTOTALCOST.Value = dTotalCost
oColumns = oForm.getColumns()
oVat = oColumns.getByName("VAT")
oVat.updateDouble(dVatValue)
oTotalCost = oColumns.getByName("TOTALCOST")
oTotalCost.updateDouble(dTotalCost)
UpdateTotalCost	

End Sub

Sub UpdateTotalCost

Dim oForm		 As Object
Dim oColumns	 As Object
Dim oVat		 As Object
Dim oTotalCost	 As Object
Dim dVatValue	 AS Double
Dim dDelCost	 AS Double
Dim dCost		 AS Double
Dim dTotalCost	 AS Double

oForm = ThisComponent.Drawpage.Forms.getByName("MainForm") 'Get Form to access payment grid
dDelCost = oForm.fmtDELCOST.Value
dDelCost = oForm.fmtDELCOST.Value
dCost = oForm.fmtCOST.Value
dVatValue = dCost * .2
oForm.fmtVAT.Value = dVatValue
dTotalCost = dCost + dDelCost + dVatValue
oForm.fmtTOTALCOST.Value = dTotalCost
oColumns = oForm.getColumns()
oVat = oColumns.getByName("VAT")
oVat.updateDouble(dVatValue)
oTotalCost = oColumns.getByName("TOTALCOST")
oTotalCost.updateDouble(dTotalCost)

End Sub

Hello,

There is hardly ever a good reason to save a total which can be computed from existing data.
.
Regardless, what database are you using?
.
Can you post a sample which will make it much easier to help you?

Here’s what I’m working on. Still trying to get all the bugs worked out and get everything working how it needs to be.

2022 Donations.odb (20.8 KB)

Please do not use answer to respond but rather a comment.
.
This sample is far from normalized. Much duplication and should be broken out into another table to take advantage of a database relation. Also HSQLDB embedded is very old and should be used cautiously.
.
If not utilizing relationships, might as well use a spreadsheet.
.
Will look at code a bit later.

Edit:
.
Have looked further at the sample. The table/form have no association with the code you presented in the question. This makes matters more complex as there is no telling exactly what is wanted.
.
Do not understand this construction. Would not consider this for a database. Needs a lot of work and a complete re-organization of data.
.
Need more information on what you have and are attempting and not just and so on.

Something like this may be more sensible:
.
Edit: added Total using Query:
.
2022 Donations.odb (31.1 KB)

.
See form Donor. Has form (for Donor table) and subform (for Donation table). Probably a few misc. items missing but makes things much more simple. No need for worrying about the year. Selection for reports can be with SQL.

hmmm…this form looks like it was originally just a spreadsheet. If it was in Access…the power of Access was not implemented at all. Basically just simulated another spreadsheet in Access and did not utilize the relational databases ability to save storage space and eye-clutter when dealing with large amounts of inter-related data. This happens when you start out in Excel just trying to track a small amount of data…but then outgrew Excel. Not your fault…happens to everybody. Oh, just reviewed the example that Ratslinger made for you. Do you understand the elegance and power of the layout change he is showing you? Plus, it uses no macros/base coding at all. No need to waste screenspace with mostly-empty form boxes. Also no need to waste storage space with enormous data records having only a few fields filled in. The cascading grid tables can display an ENORMOUS amount of diverse (but related) data on one screen…hard to beat. Think Access only allowed you to link-cascade down to 7 levels…but imagine Libre has no limit to how deep you can go.