Expression Logic in Field Query

D

Dave

My Field query expression is:
Expr1: IIf(IsNull((SELECT Sum(Kgs) AS SumOfKgs FROM
DailyProductShipments;)),0,(SELECT Sum(Kgs) AS SumOfKgs
FROM DailyProductShipments;))

The reason I use IsNull is because I need this query to
return a "zero" if it finds no record (shipments for the
time period defined in DailyProductShipments). This query
properly returns a value for Sum(Kgs) as long as there are
one or more records. However, it returns no value if there
are no records. How can I get it to return a zero or is
there a beter way to do this?

Thanks in advance,
Dave
 
A

Alex Ivanov

It does not work because there is no records to return.
I would recommend to use a function instead of query. Assuming using DAO it
might look like this:

Public Function GetValue(SQL As String)
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(SQL)
If Not rs.EOF Then
GetValue = SQL(0)
Else
GetValue = Null 'or 0, if you prefer
End If
End Sub

HTH
Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top