View Full Version : Visual Basic 6

19-06-2005, 12:42 PM
I Have column in access database table called Package weight

Using VB6 and either ADODB or DAO (Data Object on form) I want to add up all the package weights and put them into a variable how can I do this?


19-06-2005, 08:52 PM
Do you mind my asking why you want to store the value? Also, do you want to add up the total for every item stored in the table, or are you wanting to group these by another field?

In any case you can calculate the total of a field on the fly (i.e. within a query) using DSum. The syntax is DSum("[your_field_name]", "your_table_name") for a query and I'll bet it's identical for VB, except in VB you might go Dim TotWeight, TotWeight = DSum("[your_field_name]", "your_table_name"). If the values in the table are going to change (or be added to or deleted) then it may be safer just to calculate this on the fly anyway.

Another way would be via a totals query where you add the weights and leave it at that. If you wanted to get fancy, you could use that query as an update query to write the total into a one field table - which you could reference from within any other query or VB script. Using this method on a form, you could point the unbound text box on the form to this one field table using Dlookup - although I suppose there is no reason why you couldn't do the same thing with a Dsum in an unbound text box (I have never tried that myself, although there is no reason why it can't be done).

There are a couple of options there so I guess it comes down to what you are trying to achieve.....?

Just my thoughts, Andrew