Connecting Tech Pros Worldwide Help | Site Map

Adding up fields

Newbie
 
Join Date: Sep 2008
Posts: 7
#1: Sep 2 '08
Hi,

This would be very simple but I cant seem to get it working, all I am trying to do is get textbox "a" and textbox "b" to add together so that textbox "c" displays the total of the two automatically.

Any help would be great, Thanks.

Rob.
Member
 
Join Date: Aug 2008
Posts: 77
#2: Sep 2 '08

re: Adding up fields


You may try using the following code :

Expand|Select|Wrap|Line Numbers
  1. Private Sub textboxA_Change()
  2. textboxC.value=textboxA.value+textboxB.value
  3. End Sub
  4.  
  5. Private Sub textboxB_Change()
  6. textboxC.value=textboxA.value+textboxB.value
  7. End Sub
  8.  
  9.  
  10.  
Doing this should display the sum of both the text boxes when the value in any of them changes.

Do let us know the outcome.
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#3: Sep 2 '08

re: Adding up fields


Hi. It depends on whether or not your textboxes are bound to underlying fields. If they are, the data type of the value contained in the textbox is determined by the data type of the underlying field. If they are unbound, the contents are treated as text values (not numbers).

In the bound case, and assuming that the underlying fields are numbers, then in C's control source you enter:

=[a]+[b]

But if they are unbound, you will need to convert them to an appropriate type either using CLng for long integers or CDbl for double-precision floating point:

=CLng([a]) + CLng([b])
=CDbl([a]) + CDbl([b])

If you try

=[a]+[b]

on text values it will result in their concatenation:

'1' + '2' = '12'

-Stewart

ps @ Yaara: no need for code in this case - the control source can be set to be the sum of the two fields directly as shown. If code was used it would need to be associated with an After Update or similar event.
Member
 
Join Date: Aug 2008
Posts: 77
#4: Sep 2 '08

re: Adding up fields


Actually you are right (as always) Steve.. I quite often get confused with the coding pattern in VB6 against VBA in Access.. The code I mentioned would have worked perfectly for VB6 but in VBA, it surely needs to be associated with either BeforeUpdate or AfterUpdate event. Changing the ControlSource does give the required output :-)
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#5: Sep 2 '08

re: Adding up fields


Quote:

Originally Posted by yaaara

Actually you are right (as always) Steve..

Thanks, Yaara.

I'm used to people calling me Stuart a lot, though rarely Steve... but what's in a name anyway?? I appreciate your kind words in any event.

-Stewart
Member
 
Join Date: Aug 2008
Posts: 77
#6: Sep 2 '08

re: Adding up fields


Actually I've met several people with your name and all of them liked them to be called Steve... Will call you Stuart as per your liking..

As for that famous quote "What's in a name?" by Shakespeare (or whichever way you spell it), I'd like to say that he did mention this statement, but he didn't forget to write his own name below it.. So there has to be something in a name? ;-)

Quote:

Originally Posted by Stewart Ross Inverness

Thanks, Yaara.

I'm used to people calling me Stuart a lot, though rarely Steve... but what's in a name anyway?? I appreciate your kind words in any event.

-Stewart

Reply