Results 1 to 6 of 6
  1. #1
    Ansiep1 is offline Novice
    Windows 10 Office 365
    Join Date
    Jul 2022
    Posts
    1

    Query expr causing the expression you entered has a function with the wrong number of arguments


    Hi
    I have to run a query to concatenate 3 surnames. Surname1 and Surname2 can be the same. Surname3 may be blank

    I am using the following expression:

    Surnames: IIf([Surname2]=[Surname1],[Surname1],[Surname1] & " and " & [Surname2] & " and " & [Surname3],IIF(IsNull([Surname3]),[Surname1] & " and " & [Surname2],[Surname1] & " and " & [Surname2] & " and " & [Surname3]))


    I receive the following error message: the expression you entered has a function with the wrong number of arguments" How do I correct this expression?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    IIF() function needs True and False arguments.
    I would tend to work from the inside out, if I was to create such a convoluted statement.

    I would most likely create a function and do it bit by bit.
    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

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Your second iif needs to be one of the first iff's arguments; it can't stand alone like that.
    An example of the names and what you expected as the outcome would have been helpful, but with assumptions of your expectations on my part, try this:

    Surnames:IIf([surname2] = [surname1], [surname1], [surname1] & " and " & [surname2] & IIf(Not IsNull([surname3]), " and " & [surname3], ""))

    Edit: simpler and more accurate:

    Surnames: surname1 & IIf([surname2] = [surname1], "", " and " & surname2) & IIf(Not IsNull(surname3), " and " & surname3, "")

    Edit: Third attempt, works better

    Surnames: surname1 & IIf([surname2] = [surname1], "", IIf(IsNull(surname2), "", " and " & surname2) & IIf(Not IsNull(surname3), " and " & surname3, ""))

    And here's a function that works and only took 5 minutes to write and debug (Don't you just hate those compound IIFs ?)

    Code:
    Public Function fcnSurnames(n1 As String, n2 As String, n3 As String)
        Dim rslt As String
        rslt = n1
        Select Case (n2 & "") = ""
            Case False
                Select Case n1 = n2
                    Case False
                        rslt = rslt & " and " & n2
                End Select
        End Select
        Select Case (n3 & "") = ""
            Case False
                rslt = rslt & " and " & n3
        End Select
        fcnSurnames = rslt
    End Function

    None of these account for Surname 1 being blank
    OR surname1 = surname3
    OR surname2 = surname3
    Last edited by davegri; 07-21-2022 at 04:01 PM.

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    If I'm not mistaken would this be be even simpler ?

    Surnames: Surname1 & IIf([surname2] <> [surname1], "", (" and " + [surname2]) & (" and " + [surname3]))
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    @Minty
    Use of + is clever to account for nulls.
    However, the <> should be = in your expression.

    The OP did specify Nulls, but the function I posted (as of late) in post#3 will handle both nulls and zls.

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    Quote Originally Posted by davegri View Post
    @Minty
    Use of + is clever to account for nulls.
    However, the <> should be = in your expression.

    The OP did specify Nulls, but the function I posted (as of late) in post#3 will handle both nulls and zls.
    I originally coded it the other way around hence <> , but when I looked at it, it looked weird/awkward so I swapped the true/false expressions around, only to forget to change it to =.
    Ooops!
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. Replies: 4
    Last Post: 11-05-2021, 11:17 AM
  2. Expression Builder - wrong number of arguments
    By Frannilea in forum Access
    Replies: 1
    Last Post: 08-23-2020, 04:41 AM
  3. Wrong number of arguments
    By Perfac in forum Programming
    Replies: 10
    Last Post: 05-20-2020, 05:52 PM
  4. function has the wrong number of arguments
    By Darla in forum Programming
    Replies: 6
    Last Post: 02-09-2017, 02:03 PM
  5. Replies: 3
    Last Post: 02-12-2014, 03:36 PM

Tags for this Thread

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