=IIf(IsNull([Forms]![CloseForm].[SettlementPage2]),Null,DLookUp("[L1400x]","TSettlementPage2","[Page2ID]=Forms![CloseForm].[SettlementPage2]"))
Is it possible to test in an IIf statement if the record you need has actually been created. If it is could someone please suggest the syntax?
I can get the data if the record has been created, but don't know how to test for a record that is not there.
Thanks
I found this which goes someway to answer the problem, but I want to test a different form than the one I have open. Could I add open other form, test and close other form to this code?
The problem does not arise in forms that display the new record. It does occur if the form's Allow Additions property is Yes, or if the form is bound to a non-updatable query.To avoid the problem, test the RecordCount of the form's Recordset. In older versions of Access, that meant changing:
=Sum([Amount])
to:
=IIf([Form].[Recordset].[RecordCount] > 0, Sum([Amount]), 0)
Access 2007 and later have a bug, so that expression fails. You need a function.
Copy this function into a standard module, and save the module with a name such as Module1:
Public Function FormHasData(frm As Form) As Boolean
'Purpose: Return True if the form has any records (other than new one).
' Return False for unbound forms, and forms with no records.
'Note: Avoids the bug in Access 2007 where text boxes cannot use:
' [Forms].[Form1].[Recordset].[RecordCount]
On Error Resume Next 'To handle unbound forms.
FormHasData = (frm.Recordset.RecordCount <> 0&)
End Function
Now use this expression in the Control Source of the text box:
=IIf(FormHasData([Form]), Sum([Amount]), 0)
Notes
- Leave the [Form] part of the expression as it is (i.e. do not substitute the name of your form.)
- For Access 97 or earlier, use RecordsetClone instead of Recordset in the function.
- A form with no records still has display problems. The workaround may not display the zero, but it should suppress the #Error.