Read two integers in text box and add them (macro)

Hi
Been trying for 5 hours now.
Lots of tries from things on various places.
I have database.
Table and forms.
on form is a cPoints text box set to integer
Have another text box txt_AddRemove set to integer.
Need to take the value from txt_AddRemove and add it to cPoints with the push of a button.

this is what I got.

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

Sub Main ( Event as Object )
rem MsgBox "You pressed Me!"
   
   DIM oForm as Object
   DIM oField as object
   DIM oPointsField as object
   DIM oTotalPoints as object

   'gets the form
   oForm = Event.Source.Model.Parent
   
   oField = oForm.getByName("txt_AddRemove")
  ' oField.Text = "500"
   
   oPointsField = oForm.getByName("cPoints")
   
   oTotalPoints = oField + oPointsField
   
   oPointsField.Text =  oTotalPoints
   
   oPointsField.commit()
   
End Sub

first error only in debug
oForm = Event.Source.Model.Parent Argument is not optional.

when running by clicking on button
oTotalPoints = oField + oPointsField incorrect property value

tried to find convert text to int and int to text. cannot find anything.

if I can get this working I am done.

Thanks all

Not sure how to reply to a comment on my post

Can you not convert text to int, then add, then convert to text. Also all boxes are integers. Can I not pull the value off as an integer.

(Slighly edited by @Lupp for better readability.)

Text = “500”

While it might be possible (I don’t know), in principle it is not advisable to do arithmatic on text. A bit like checking numbers for literary expression. Can you find a different approach?

@Michellia: Please use the tool ‘preformatted text’ for code.
Please only use the ‘Add Answer’ box for an answer to the original question.
Please anwer to questions in return posted correctly using ‘add a comment’ using the same feature.

This is not a forum supporting ongoing discussions post after post, but a Q&A site.

If the numbers you got as texts are in correct syntax (using decimal digits and everything) you can use the appropriate conversion function CInt() or CDbl() to get the value. (These functions are buggy concerning the + as positive sign. So also is IsNumeric().)
You may also rely on the automatic conversion using expressions like
theValue = 0 + theText where text needs to be a string of correct number syntax.
(Make sure NOT to write “0” there. It would make the “+” interpreted as a concatenation operator. Yes. this Basic is sometimes really silly. Always use & for concatenation to disambiguate.)
Trying theSum = theText1 + theText2 would also concatenate instead of doing arithmetic.
Best you explicitly Dim a varibale for the result As Long or As Double and assign the expression to it in advance of passing the result to the next step. Either automatic conversion works then as expected, or you get an error message and will know what changes are needed.

(These functions are buggy concerning the + as positive sign. So also is IsNumeric().)

See https://bugs.documentfoundation.org/show_bug.cgi?id=136801.
The mentioned bug should be fixed in Version 7.1 or higher. (Not yet marked RESOLVED today: 2020-12-16)

Hello,

Edit: Not to step on answer by @Lupp, but you also need to retrieve the values and not use the object.

End Edit

Sub Main ( Event as Object )
   DIM oForm as Object
   DIM oField as object
   DIM oPointsField as object
   DIM oTotalPoints as object
   DIM iAddRem as Integer
   DIM iPoints as Integer
   oForm = Event.Source.Model.Parent
   oField = oForm.getByName("txt_AddRemove")
iAddRem = oField.Text
   oPointsField = oForm.getByName("cPoints")
iPoints = oPointsField.Text
   oPointsField.Text =  iAddRem + iPoints
   oPointsField.commit()
End Sub

Basic does convert text to integer fairly well provided the data is actually a number. You, however were trying to do math with an object. Instead you first need to retrieve the data into a proper variable. Once you have this add them and then place back into wanted control.

Instead of text boxes, for this type of functionality you should be using a numeric control and retrieving the “Value”. Much safer process.

Grummel… I have to adtmit that I hadn’t read the code posted in the question thoroughly. Should have done.

Happens to all of us. I have certainly been guilty of this enough times. :slight_smile:

Can you not convert text to int, then add, then convert to text.
Also all boxes are integers. Can I not pull the value off as an integer.

Please read my comment on the question (the second comment there).