Results 1 to 3 of 3
  1. #1
    Abacus1234 is offline Competent Performer
    Windows Vista Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    212

    How to write an IIf statement in a form control to test if record you need has been created?

    =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

    1. Leave the [Form] part of the expression as it is (i.e. do not substitute the name of your form.)
    2. For Access 97 or earlier, use RecordsetClone instead of Recordset in the function.
    3. A form with no records still has display problems. The workaround may not display the zero, but it should suppress the #Error.

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Your question is confusing, at least to me! Records don't really 'exist' in Forms, they're only displayed in them! They actually exist in Tables. To check to see if a Record exist, you could use DCount() with a Criteria (like a Where Clause) consisting of a Field that is unique to a given Record.

    An example would be something like this, if the unique Field was defined as Text:
    Code:
    If DCount("*", "TableName", "[EmployeeID] = '" & Me.EmpID & "'") > 0 Then
     'a Matching Record exists...do something
    Else
     'a Matching Record does not exist...do something else  
    End If


    If defined as a Number:
    Code:
    If DCount("*", "TableName", "[EmployeeID] = " & Me.EmpID) > 0 Then
     'a Matching Record exists...do something
    Else
     'a Matching Record does not exist...do something else  
    End If


    Exactly where you'd place the code depends on what, exactly, you want to do if a Record does or doesn't exist.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Abacus1234 is offline Competent Performer
    Windows Vista Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    212
    Thank you for the code. I Have something working at the moment which I have attached to my post. It allows me to use an IIf statement in a form control to do a DLookup. I appreciate your help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2013, 10:28 AM
  2. Replies: 2
    Last Post: 03-28-2012, 02:41 PM
  3. How would I write this IF statement
    By robsworld78 in forum Programming
    Replies: 12
    Last Post: 02-27-2012, 08:01 PM
  4. Autofilling when New Record Created from Form
    By SpdRacerX in forum Forms
    Replies: 6
    Last Post: 01-24-2012, 10:24 AM
  5. Replies: 2
    Last Post: 11-16-2011, 07:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums