Solved invalid use of null (1 Viewer)

FahadTiger

Member
Local time
Today, 08:46
Joined
Jun 20, 2021
Messages
115
Hi Expert..
I used module to sum Amount according to CustomerNumber And CurrencyType
its working Good and give me the Result im main form of saleInvoice
but..when there are No Records in the table give me Error "invalid use of null".. in "TotalAmount = rs!TotalAmount"
How can I FIX that
Code:
Function SumAmount(CustomerNumber As Long, CurrencyType As String) As Currency
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim TotalAmount As Currency

TotalAmount = 0
Set db = CurrentDb

strSQL = "SELECT Sum(Amount) AS TotalAmount FROM saleDetails WHERE CustomerID = " & CustomerNumber & " AND CurrencyType = '" & CurrencyType & "'"
Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
        TotalAmount = rs!TotalAmount
End If
rs.Close
db.Close
Set db = Nothing
SumAmount = TotalAmount
End Function
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:46
Joined
Sep 21, 2011
Messages
14,299
Uze the NZ() function?
However, I would probably looking to see how it became null.
 

tvanstiphout

Active member
Local time
Yesterday, 22:46
Joined
Jan 22, 2016
Messages
222
This doesn't make sense to me yet. If you set CustomerNumber to -1 (presumably non-existing) then the query would return 0 records and EOF would be true.
Something else must be going on.
Did you put a breakpoint in this function and step through? If this is an Access back-end, did you rapair & compact it?
Is Amount a required field? It probably should be.

BTW, you could have done this with a one-liner call to the DSum function.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:46
Joined
Sep 21, 2011
Messages
14,299
This doesn't make sense to me yet. If you set CustomerNumber to -1 (presumably non-existing) then the query would return 0 records and EOF would be true.
Something else must be going on.
Did you put a breakpoint in this function and step through? If this is an Access back-end, did you rapair & compact it?
Is Amount a required field? It probably should be.

BTW, you could have done this with a one-liner call to the DSum function.
No, I just tried it. It passes the EOF test and gets to the line.

Code:
Function SumAmount() ' CustomerNumber As Long, CurrencyType As String) As Currency
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim TotalAmount As Currency

TotalAmount = 0
Set db = CurrentDb

strSQL = "SELECT Sum(TestTransactions.Amount) AS TotalAmount FROM TestTransactions WHERE TestTransactions.ID=10000"
Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
        TotalAmount = rs!TotalAmount
End If
rs.Close
db.Close
Set db = Nothing
SumAmount = TotalAmount
End Function

so NZ() might be the way to go, or a DCount() first to see if any exist?
 

Josef P.

Well-known member
Local time
Today, 07:46
Joined
Feb 2, 2023
Messages
826
... then the query would return 0 records and EOF would be true.
Not for Sum(...) from without Group By.
In this case, EOF will never occur.

SELECT Sum(Amount) AS TotalAmount FROM saleDetails WHERE 1=0
=> 1 Record with Null

vs.
SELECT Sum(Amount) AS TotalAmount FROM saleDetails WHERE 1=0 Group by 0
 

FahadTiger

Member
Local time
Today, 08:46
Joined
Jun 20, 2021
Messages
115
this is my sample.. when No currency type in Euro gave me Error
 

Attachments

  • invalid use of null.accdb
    700 KB · Views: 63

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:46
Joined
Aug 30, 2003
Messages
36,125
Doesn't look like you tried what was suggested, though I would add an Else:

Code:
  If Not rs.EOF Then
    TotalAmount = Nz(rs!TotalAmount, 0)
  Else
    TotalAmount = 0
  End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:46
Joined
May 7, 2009
Messages
19,243
as suggested, use:

Nz(rsObject!TotalField, 0)
 

Attachments

  • invalid use of null.accdb
    804 KB · Views: 68

Users who are viewing this thread

Top Bottom