Results 1 to 7 of 7
  1. #1
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49

    IIF statement in control of form

    OK. Admittedly, I'm a noob and a goob.
    When I enter the code below into the control for txtPrimary on a form, the field shows the #Name? error.

    Client and PWSName exist in tblCustody
    CertNum exists in tblClient
    I can use just CertNum from tblClient on the form. I've tried for a while and give up. the IIF statement must have an error I can't find.

    All the tables are available to use in the form.

    The form should display either "M-MA072" (our cert number) or the cert number of the lab that sent the sample - tblClient.CertNum

    Spelling is correct


    [CODE]=IIf([tblCustody]![Client]=[tblCustody]![PWSName],]="M-MA072",[tblClient]![CertNum])[CODE]

    Any help is greatly appreciated.

    Thanks for looking.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Advise you to use code tags (# button on posting toolbar) because this forum will inject spaces and maybe other unwanted stuff into long expressions. At first glance, you have an extra ending bracket.
    [PWSName],]
    #Name error usually results from a reference that cannot be resolved. Sometimes it is due to misspelled names.
    EDIT - have to admit I'm getting rusty. Not sure if you can use IIF as a controlsource property.
    #2 - yes you can. AFAIK, the field used in the expression must be part of the form recordsource. I can't tell if you're trying to reference a different domain (table or query) aside from the one used by the form. If different, you may need to use DLookup with criteria instead/as well. Or get the value via code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    First off - sorry for not getting the code tags correctly. Second off - typos are corrected

    Code:
    =IIf([tblCustody]![Client]=[tblCustody]![PWSName],"M-MA072",[tblClient]![CertNum]) 
    I corrected the typos from the first posting.

    One question: Are you allowed to put an IIF in a forms control source field?

    Thanks.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Yes
    Code:
    =IIf([Idor]=1,"One","Not one")
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    One question: Are you allowed to put an IIF in a forms control source field?
    Perhaps you read my post before I edited it:
    EDIT - have to admit I'm getting rusty. Not sure if you can use IIF as a controlsource property.
    #2 - yes you can. AFAIK, the field used in the expression must be part of the form recordsource.
    I don't think you can refer to tables and fields that are not part of the form recordsource. You haven't cleared that up yet.
    I can't tell if you're trying to reference a different domain (table or query) aside from the one used by the form.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    OK.

    You can use an IIF statment in a form control. For example, the following snippet works just fine.

    Code:
    =IIf(1=1,1,2)

    Now I'm still stuck with the original issue.

    Is there a syntax issue in this line?
    Code:
    =IIf([tblCustody]![Client]=[tblCustody]![PWSName],"M-MA072",[tblClient]![CertNum]) 

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I don't think you can refer to tables and fields that are not part of the form recordsource. You haven't cleared that up yet.
    Asked twice and still not answered. Good luck, but I'm going to unwatch this one.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. IIF statement in control source propety of the textbox
    By AmanKaur123 in forum Programming
    Replies: 2
    Last Post: 03-09-2017, 04:30 AM
  2. Replies: 5
    Last Post: 03-02-2016, 12:37 PM
  3. For/Next Control Statement Issues
    By nick404 in forum Programming
    Replies: 4
    Last Post: 07-29-2015, 11:24 AM
  4. Replies: 2
    Last Post: 03-13-2015, 07:52 AM
  5. IIF statement in control source of text box
    By LilMissAttack in forum Reports
    Replies: 11
    Last Post: 08-18-2011, 10:02 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