By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

Adding up fields

P: 7
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.
Sep 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: 77
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.
Sep 2 '08 #2

Expert Mod 2.5K+
P: 2,545
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.
Sep 2 '08 #3

P: 77
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 :-)
Sep 2 '08 #4

Expert Mod 2.5K+
P: 2,545
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
Sep 2 '08 #5

P: 77
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? ;-)

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
Sep 2 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.