Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: rounding numbers in query


Message #1 by "Idelle Grant" <crawleriver@h...> on Tue, 28 Jan 2003 13:04:40
I have a table that keeps account of debits and credits.  I want to keep a 
running balance so I created the procedure below to
do so.  

I am not getting the cents rounded correctly.  When I calculate the 
running balance on my calculator the calculated one is 4 cents higer than 
the one I get from the procedure below.
What do I do to get the rounding correctly ?

Private Sub Form_AfterUpdate()
    'calculate running account balance
    
    
    Dim strCr As String
    Dim strDr As String
    Dim dblCr As Double
    Dim dblDr As Double
    
    Dim rstDr As ADODB.Recordset
    Dim rstCr As ADODB.Recordset
    Dim cnn As ADODB.Connection
    
    Set cnn = CurrentProject.Connection
    Set rstDr = New ADODB.Recordset
    Set rstCr = New ADODB.Recordset
    
    strCr = "SELECT Sum(tblBudgetCat.Credit) AS SumOfCredit FROM 
tblBudgetCat"
    strDr = "SELECT Sum(tblBudgetCat.Debit) AS SumOfDebit FROM 
tblBudgetCat"
    
    rstCr.Open strCr, cnn, adOpenKeyset
    rstDr.Open strDr, cnn, adOpenKeyset
    
  
    dblCr = CDbl(rstCr!SumOfCredit)
    dblDr = CDbl(rstDr!SumOfDebit)
    
    RunBal = Format(dblCr - dblDr, "Standard")  ' put blance in textbox 
RunBal
        
    
    
End Sub
Message #2 by "zak willis" <zak@z...> on Tue, 28 Jan 2003 14:40:17
Should you check your precision level on your original table? 
Why not do a union query on your recordsets too, rather than opening two 
recordsets via ADO?
Regards,
Zak
> I have a table that keeps account of debits and credits.  I want to keep 
a 
r> unning balance so I created the procedure below to
d> o so.  

> I am not getting the cents rounded correctly.  When I calculate the 
r> unning balance on my calculator the calculated one is 4 cents higer 
than 
t> he one I get from the procedure below.
W> hat do I do to get the rounding correctly ?

> Private Sub Form_AfterUpdate()
 >    'calculate running account balance
 >    
 >    
 >    Dim strCr As String
 >    Dim strDr As String
 >    Dim dblCr As Double
 >    Dim dblDr As Double
 >    
 >    Dim rstDr As ADODB.Recordset
 >    Dim rstCr As ADODB.Recordset
 >    Dim cnn As ADODB.Connection
 >    
 >    Set cnn = CurrentProject.Connection
 >    Set rstDr = New ADODB.Recordset
 >    Set rstCr = New ADODB.Recordset
 >    
 >    strCr = "SELECT Sum(tblBudgetCat.Credit) AS SumOfCredit FROM 
t> blBudgetCat"
 >    strDr = "SELECT Sum(tblBudgetCat.Debit) AS SumOfDebit FROM 
t> blBudgetCat"
 >    
 >    rstCr.Open strCr, cnn, adOpenKeyset
 >    rstDr.Open strDr, cnn, adOpenKeyset
 >    
 >  
 >    dblCr = CDbl(rstCr!SumOfCredit)
 >    dblDr = CDbl(rstDr!SumOfDebit)
 >    
 >    RunBal = Format(dblCr - dblDr, "Standard")  ' put blance in textbox 
R> unBal
 >        
 >    
 >    
E> nd Sub
Message #3 by "Richard" <r_lesh@h...> on Tue, 28 Jan 2003 16:14:28
Remember that the sum of rounded numbers is not the same as the sounded 
sum.  I mean that: sum(rnd(x)) <> rnd(sum(x)).

If the numbers in the database are stored with 'fractions of cents', this 
may be your problem.
Message #4 by "Idelle Grant" <crawleriver@h...> on Tue, 28 Jan 2003 17:10:50
The values entered into the table would be like $167.76 or $1156.39. In 
other words the user enters the dollar and the cents.

I then take these values and use them in the query.

  Return to Index