Results 1 to 9 of 9

Thread: [RESOLVED] Trying to get SUM total

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Resolved [RESOLVED] Trying to get SUM total

    I have a table with 6 fields and I am trying to get the total of one of them, Finance_Expenditure.
    I have the following and I am getting '0' returned everytime.
    Code:
        Dim TotalEx As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as TotalEx FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        dblExpenditure = TotalEx
    Do I have to something with grouping, although I cannot see why as this is the only field I am working with!
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to get SUM total

    Your local VB side variable TotalEx has nothing to do with a column name in a SELECT statement.

    The "rs" object has your value - you can use rs(0) to get it - or use rs("TotalEx") to get at it.

    By the name of the dblExpenditure variable you are scaring me that it might be a double-float variable. That should never, ever, ever be used with monetary values - they should be put into a CURRENCY datatype or a DECIMAL(x,y) datatype.

    You might want to do it like this:

    Code:
    Dim TotalEx As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as TotalEx FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        TotalEx = CCur(rs(0))
    That would use your already declared TotalEx variable.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Trying to get SUM total

    What data type is Finance_Expenditure?
    Anyway, you never use the recordset to set dblExpenditure.
    vb Code:
    1. dblExpenditure = rs.Fields("TotalEx")
    2. MsgBox dblExpenditure

    It's bad practice (imho) to use the same variable name as a field name, because you often may forget which is which. Your code sets dblExpenditure to the value in the currency variable TotalEx, but you're thinking it's the recordset field TotalEx. Two different objects that you've given the same name.
    For example:
    vb Code:
    1. Dim TotalEx As Currency
    2. TotalEx = 500
    3.     strSQL = "SELECT SUM (Finance_Expenditure) as TotalEx FROM tbl_Finances"
    4.     rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
    5.     dblExpenditure = TotalEx
    6. MsgBox dblExpenditure
    will show 500.
    Another possible caveat: dblExpenditure is presumably a double, but TotalEx is declared as Currency. Never used Currency data type, may not be compatible with double. Don't know about that.
    Tengo mas preguntas que contestas

  4. #4

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Trying to get SUM total

    By the name of the dblExpenditure variable you are scaring me that it might be a double-float variable. That should never, ever, ever be used with monetary values
    Sorry to say this is so, but I will go through and change it. I didn't know this.

    What data type is Finance_Expenditure?
    It is currency.
    TotalEx is just a variable I added for this function, it is not a field name.

    So, provided I get the doubles changed to currency variables, this should work:
    Code:
    Dim TotalEx As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as TotalEx FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        TotalEx = CCur(rs(0))
    Could somebody explain about using rs(0)
    What is the 0 for/doing?

    Ans also, if I didn't declare 'TotalEx ' what would / could I use instead?
    Last edited by aikidokid; Jul 28th, 2007 at 02:52 PM.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to get SUM total

    This

    Code:
    Dim TotalEx As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as TotalEx FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        TotalEx = CCur(rs(0))
    Could just as easily been

    Code:
    Dim SomeVariable As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as CallMeAnything FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        SomeVariable = CCur(rs(0))
    TotalEX or SomeVariable in your VB code is just that - a variable.

    rs(0) means to take the first column of the recordset - and since you recordset only has one column - rs(0) works fine in my book.

    Some people say never use ordinal positions (that's what the 0, 1, 2... are called) - but instead say to always use a named reference.

    That would be this instead...

    Code:
    Dim SomeVariable As Currency
        strSQL = "SELECT SUM (Finance_Expenditure) as CallMeAnything FROM tbl_Finances"
        rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
        SomeVariable = CCur(rs("CallMeAnything"))

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to get SUM total

    Quote Originally Posted by salvelinus
    ... dblExpenditure is presumably a double, but TotalEx is declared as Currency. Never used Currency data type, may not be compatible with double. Don't know about that.
    Actually it's the DOUBLE that's evil. DOUBLE math will lose pennies - exactly what you don't want to happen in a financial application!!!

    20/5 in double can be 3.99999999999999 - it's just the way floating point math works. It might still display as 4 - but internally it's 3.999999999999999 and that lost digit will eventually burn you.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Trying to get SUM total

    Well, either way, the main problem is not using the rs value.
    I really hate to disagree with szlamany, who knows a heck of a lot more than me, but I prefer to use rs.Fields("someField") just to make things clear to anyone coming after me, including myself a year down the road.
    Tengo mas preguntas que contestas

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Trying to get SUM total

    Quote Originally Posted by salvelinus
    I really hate to disagree with szlamany, who knows a heck of a lot more than me, but I prefer to use rs.Fields("someField") just to make things clear to anyone coming after me, including myself a year down the road.
    We nearly always use the ORDINAL because in most of our apps we loop through the columns and use the .NAME from the recordset to determine what to do with the column. Barely ever have a variable set like the OP does.

    Otherwise I would have to agree that using the "somefield" is better form.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Trying to get SUM total

    Thanks guys, that's a really good in depth explanation for me, and answers a few more questions I had coming up.
    I'm going to rate you both
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width