A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Summing numbers in a ListBox



 
 
Thread Tools Display Modes
  #1  
Old January 7th, 2008, 07:20 PM posted to microsoft.public.access.forms
gator
external usenet poster
 
Posts: 166
Default Summing numbers in a ListBox

Can I put the following formula in a text box on a form to add numbers listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator
  #2  
Old January 7th, 2008, 07:30 PM posted to microsoft.public.access.forms
Pat Hartman
external usenet poster
 
Posts: 392
Default Summing numbers in a ListBox

No. You would create a separate query and use DSum() to sum the data.

"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator



  #3  
Old January 7th, 2008, 07:41 PM posted to microsoft.public.access.forms
Douglas J. Steele
external usenet poster
 
Posts: 9,313
Default Summing numbers in a ListBox

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the value
in the second column of the selected row in the list box (assuming that it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue: Access will
automatically do the conversion for you if things are set up properly).

If what you're saying is that you want the sum of all of the values in the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator



  #4  
Old January 7th, 2008, 08:16 PM posted to microsoft.public.access.forms
gator
external usenet poster
 
Posts: 166
Default Summing numbers in a ListBox

works good except my sum seems to be rounding up. the amount is .47 and the
text box is showing .50
--
Gator


"Douglas J. Steele" wrote:

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the value
in the second column of the selected row in the list box (assuming that it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue: Access will
automatically do the conversion for you if things are set up properly).

If what you're saying is that you want the sum of all of the values in the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator




  #5  
Old January 7th, 2008, 08:31 PM posted to microsoft.public.access.forms
Douglas J. Steele
external usenet poster
 
Posts: 9,313
Default Summing numbers in a ListBox

If you're getting a decimal point, then obviously you changed my code, since
I assume Long Integers, which don't support decimal points.

What data type did you choose instead of Long? If you're working with
currency, you should use the Currency data type, and the CCur function.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
works good except my sum seems to be rounding up. the amount is .47 and
the
text box is showing .50
--
Gator


"Douglas J. Steele" wrote:

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the
value
in the second column of the selected row in the list box (assuming that
it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue: Access
will
automatically do the conversion for you if things are set up properly).

If what you're saying is that you want the sum of all of the values in
the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator






  #6  
Old January 7th, 2008, 08:48 PM posted to microsoft.public.access.forms
gator
external usenet poster
 
Posts: 166
Default Summing numbers in a ListBox

I formatted the Table field as Currency & Auto Decimal.
--
Gator


"Douglas J. Steele" wrote:

If you're getting a decimal point, then obviously you changed my code, since
I assume Long Integers, which don't support decimal points.

What data type did you choose instead of Long? If you're working with
currency, you should use the Currency data type, and the CCur function.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
works good except my sum seems to be rounding up. the amount is .47 and
the
text box is showing .50
--
Gator


"Douglas J. Steele" wrote:

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the
value
in the second column of the selected row in the list box (assuming that
it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue: Access
will
automatically do the conversion for you if things are set up properly).

If what you're saying is that you want the sum of all of the values in
the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator






  #7  
Old January 7th, 2008, 08:51 PM posted to microsoft.public.access.forms
Douglas J. Steele
external usenet poster
 
Posts: 9,313
Default Summing numbers in a ListBox

But what did you use in the function?

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
I formatted the Table field as Currency & Auto Decimal.
--
Gator


"Douglas J. Steele" wrote:

If you're getting a decimal point, then obviously you changed my code,
since
I assume Long Integers, which don't support decimal points.

What data type did you choose instead of Long? If you're working with
currency, you should use the Currency data type, and the CCur function.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
works good except my sum seems to be rounding up. the amount is .47
and
the
text box is showing .50
--
Gator


"Douglas J. Steele" wrote:

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the
value
in the second column of the selected row in the list box (assuming
that
it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue:
Access
will
automatically do the conversion for you if things are set up
properly).

If what you're saying is that you want the sum of all of the values in
the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add
numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator








  #8  
Old January 7th, 2008, 09:14 PM posted to microsoft.public.access.forms
gator
external usenet poster
 
Posts: 166
Default Summing numbers in a ListBox

I used Long...but I changed it to Currency and it works...many many thanks
--
Gator


"Douglas J. Steele" wrote:

But what did you use in the function?

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
I formatted the Table field as Currency & Auto Decimal.
--
Gator


"Douglas J. Steele" wrote:

If you're getting a decimal point, then obviously you changed my code,
since
I assume Long Integers, which don't support decimal points.

What data type did you choose instead of Long? If you're working with
currency, you should use the Currency data type, and the CCur function.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
works good except my sum seems to be rounding up. the amount is .47
and
the
text box is showing .50
--
Gator


"Douglas J. Steele" wrote:

No, for a couple of reasons.

First is that all [Forms].[Form1].[ListBox].column(1) gives you is the
value
in the second column of the selected row in the list box (assuming
that
it's
not set for MultiSelect).

Second is that values in list boxes are actually strings, even if they
strictly contain digits. (Okay, this isn't necessarily an issue:
Access
will
automatically do the conversion for you if things are set up
properly).

If what you're saying is that you want the sum of all of the values in
the
second column of a given listbox, try the following function:

Function AddListbox(WhichListbox As Control) As Long
Dim lngLoop As Long
Dim lngSum As Long

lngSum = 0

For lngLoop = 0 To (WhichListbox.ListCount - 1)
lngSum = lngSum + CLng(WhichListbox.Column(1, lngLoop))
Next lngLoop

AddListbox = lngSum

End Function


You can then set the ControlSource of the text box to

=AddListbox(Forms].[Form1].[ListBox])


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Gator" wrote in message
...
Can I put the following formula in a text box on a form to add
numbers
listed
in a list box, column 2......=Sum[Forms].[Form1].[ListBox].column(1)
--
Gator









 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:06 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.