Calculation in a form

Creating and using forms
Post Reply
TicklishPig
Posts: 8
Joined: Thu Jul 10, 2008 8:54 pm

Calculation in a form

Post by TicklishPig »

I have a form with several values and I want to calculate a percentage from 2 of them and have the result updated in the form, as well as be written to the table. I have searched this forum for help on this and found some macro code here: http://user.services.openoffice.org/en/ ... =39&t=2655

Here is the provided code:

Code: Select all

Sub calcTotal(Event As Object)
   Dim Form As Object
   Dim Quantity As Object
   Dim UnitPrice As Object
   Dim Total As Double

   Form=Event.Source.Model.Parent
   Quantity=Form.GetByName("QUANTITY") REM text box w/ quantity
   UnitPrice=Form.GetByName("UNITPRICE") REM text box with price
                Total=CInt(Quanity.Text)*CDbl(UnitPrice.Text)
   Form.Columns.getByName("TOTAL").updateDouble(Total)
End Sub
And here is how I modified it for my use:

Code: Select all

Sub calcPercent(Event As Object)
   Dim Form As Object
   Dim Attendance As Object REM this is a data column name
   Dim RC As Object REM this is a data column name
   Dim PERCENT As Double REM this is a data column name

   Form=Event.Source.Model.Parent
   Attendance=Form.GetByName("Attendance") REM getting a value from form
   RC=Form.GetByName("RC") REM getting a value from form
                PERCENT=CInt(RC.Text)/CDbl(Attendance.Text)
   Form.Columns.getByName("PERCENT").updateDouble(PERCENT)
End Sub
When I run the macro (either in the macro editor or by activating it in the form with the "after updating" event) it stops with a pointer on the "Form=Event.Source.Model.Parent" line and the message "BASIC runtime error. Property or method not found".

Any suggestions as what the problem is?

Additional questions:
1. The fields in my table are of the "decimal" type, does that mean that I should replace ".text" with ".decimal" in the equation line?
2. Do I use a "/" to indicate a division operation on the equation line?
OOo 2.4.X on Ms Windows XP + Linux
N Morrison
Posts: 5
Joined: Mon Aug 04, 2008 10:18 pm

Re: Calculation in a form

Post by N Morrison »

TicklishPig wrote:Additional questions:
1. The fields in my table are of the "decimal" type, does that mean that I should replace ".text" with ".decimal" in the equation line?
2. Do I use a "/" to indicate a division operation on the equation line?
1. Probably not. You are taking the 'text' of the box and converting it to a number - you may possibly want something other than CDbl for example. But unless the text box has a property named as .decimal you will get an error with that substitution.
2. Probably yes. + - * and / mean what they indicate.
OOo 2.4.X on Ms Windows XP
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calculation in a form

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply