Ask Your Question
0

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

asked 2020-01-24 15:40:29 +0200

Michellia gravatar image

updated 2020-01-24 19:32:41 +0200

Lupp gravatar image

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.)

edit retag flag offensive close merge delete

Comments

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?

ve3oat gravatar imageve3oat ( 2020-01-24 16:56:01 +0200 )edit

@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.

Lupp gravatar imageLupp ( 2020-01-24 18:55:06 +0200 )edit

3 Answers

Sort by » oldest newest most voted
0

answered 2020-01-24 18:45:27 +0200

Michellia gravatar image

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.

edit flag offensive delete link more

Comments

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

Lupp gravatar imageLupp ( 2020-01-24 18:57:08 +0200 )edit
1

answered 2020-01-24 19:15:56 +0200

Lupp gravatar image

updated 2020-01-24 19:30:27 +0200

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.

edit flag offensive delete link more
1

answered 2020-01-24 19:49:13 +0200

Ratslinger gravatar image

updated 2020-01-24 19:59:40 +0200

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.

edit flag offensive delete link more

Comments

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

Lupp gravatar imageLupp ( 2020-01-24 22:30:07 +0200 )edit

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

Ratslinger gravatar imageRatslinger ( 2020-01-24 22:50:25 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-01-24 15:40:29 +0200

Seen: 46 times

Last updated: Jan 24