Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency data type is rounding figures...Can you prevent this???

Status
Not open for further replies.

Mirak

Programmer
Aug 1, 2001
28
0
0
Hi,

I am comparing two variables of the same currency data type. The first variable (PayinSumAmountCheck) is taken from a single row returned from an sql statement based on a parameter text box which the user inputs. Then second variable2(curTReceiptAmountCheck), however is derived by adding a number of values from an sql statement. The values are stored on the table as currency. The variable2(curTReceiptAmountCheck) which holds the final result is also currency, however, it is rounding the numbers. Thus when the variable1 and varable2 are compared, they do not match because of the rounding. How do I get vb to not round the numbers when performing addition.

Below are the codes I am using.

This is used to get variable1:

Public Function PayinSumAmountCheck() As Currency
'will return the actual amount entered from summary total of paying form
Dim rs As Recordset
Dim strsql As String
strPayinNum = Me.DBCPayinNumber.Text
strsql = ""
strsql = "Select Amount from tblpayinform where Payin_Number = " & "'" & strPayinNum & "'"
Set rs = mdb.OpenRecordset(strsql, dbOpenSnapshot)

Select Case rs.EOF
Case Is = True
PayinSumAmountCheck = 0
rs.Close

Case Is = False
PayinSumAmountCheck = rs!amount
rs.Close
End Select

Set rs = Nothing
End Function

This is used to get the second variable2:

Public Sub CheckTotal()
'This sub will check the total on the receipt against the total addition of
'transactions attached to treasury numbers
On Error GoTo error

Dim strsql As String
On Error Resume Next
strsql = ""
strsql = "SELECT * FROM tblTransactions where TransDate = " & "'" & Me.RtxtDate.Text & "'" & _
" and Branch = " & "'" & strUserBranch & "'" & " and Treasury_Num in (" & _
Me.RtxtTreasuryNum.Text & ")"

Set rsCheck = mdb.OpenRecordset(strsql, dbOpenSnapshot)
'MsgBox strsql, vbOKOnly

If rsCheck.RecordCount < 0 Then
MsgBox &quot;No records&quot;
Exit Sub
End If

'Loop throug recordset to get grand total
Do Until rsCheck.EOF
curTReceiptAmountCheck = curTReceiptAmountCheck + rsCheck!amount
rsCheck.MoveNext
Loop


Exit Sub
error: MsgBox Err.Description

End Sub


 
Instead of summing everything a bit at a time, use the aggregate function Sum() to get the value instead?
Code:
Public Sub CheckTotal()
'This sub will check the total on the receipt against the total addition of
'transactions attached to treasury numbers
On Error GoTo error

Dim strsql As String
On Error Resume Next
strsql = &quot;&quot;
Code:
strsql = &quot;SELECT Sum([Amount]) AS TOT FROM tblTransactions where TransDate = &quot; & &quot;'&quot; & Me.RtxtDate.Text & &quot;'&quot; & _
                 &quot; and Branch = &quot; & &quot;'&quot; & strUserBranch & &quot;'&quot; & &quot; and Treasury_Num in (&quot; & _
                  Me.RtxtTreasuryNum.Text & &quot;)&quot;
Set rsCheck = mdb.OpenRecordset(strsql, dbOpenSnapshot)
'MsgBox strsql, vbOKOnly

If rscheck.BOF and rsCheck.EOF and rsCheck.RecordCount = 0 Then
Code:
MsgBox &quot;No records&quot;
Exit Sub
End If

'Loop throug recordset to get grand total
 Do Until rsCheck.EOF
      curTReceiptAmountCheck = curTReceiptAmountCheck + rsCheck!TOT
              rsCheck.MoveNext
    Loop


Exit Sub
error: MsgBox Err.Description

End Sub



Of course all this can be replaced by:
Code:
Public Sub CheckTotal()
    On Error Resume Next
    Dim whereClause as string

    whereClause = &quot;TransDate = &quot; & &quot;'&quot; & Me.RtxtDate.Value & &quot;'&quot; & _
    &quot; and Branch = &quot; & &quot;'&quot; & strUserBranch & &quot;'&quot; & &quot; and Treasury_Num in (&quot; & _
    Me.RtxtTreasuryNum.Value & &quot;)&quot;

    curTReceiptAmountCheck = curTReceiptAmountCheck + _
Nz(DSum(&quot;Amount&quot;,&quot;tblTransactions&quot;,whereClause),0)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top