Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47

    display one of the the multiple fields in query

    Hi

    I need help to write the expression of a conditional field ([InsCL]) in my query table to display either one of the six following fields in the same query table:



    [INVHI]
    [INVCL]
    [INVCL1]
    [INVCL2]
    [INVCL3]
    [INVCL4]

    The criteria as follows :

    Condition 1: If all fields of [INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVHI]
    Condition 2: If fields of [INVCL1], [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVCL]
    Condition 3: If fields of [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVCL1]
    Condition 4: If fields of [INVCL3], [INVCL4] contains null value, the field InsCL should equal [INVCL2]
    Condition 5: If only field of [INVCL4] contains a null value, the field InsCL should equal [INVCL3], else InsCL should equal [INVCL4]
    I tried with below expression but the control doesn't work as the value of InsCL get stuck with [INVHI].
    InsCL: IIf(IsNull([INVCL1]+[INVCL2]+[INVCL3]+[INVCL4]),[INVHI] IIf(IsNull([INVCL1]+[INVCL2]+[INVCL3]+[INVCL4]),[INVCL], IIf(IsNull([INVCL2]+[INVCL3]+[INVCL4]),[INVCL1], IIf(IsNull([INVCL3]+[INVCL4]),[INVCL2],IIf(IsNull(INVCL4]),[INVCL3],[INVCL4])))))

    + was then replaced with "And", but still failed to get what I want.

    Any clue to the above problem ?

    Regards
    Kiang

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,526
    Build this function in a module, then In your query, make a virutal field: GetInsCLValue([INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4], [INVHI]) as InsCL


    Code:
    public function GetInsCLValue(pvINVCL,pvINVCL1, pvINVCL2, pvINVCL3, pvINVCL4,pvINVHI)
    dim vRet
    select case true
       case isnull( pvINVCL) and isnull( pvINVCL1) and isnull( pvINVCL2) and isnull( pvINVCL3) and isnull( pvINVCL4)
          vRet = pvINVHI
     
      case  isnull( pvINVCL1) and isnull( pvINVCL2) and isnull( pvINVCL3) and isnull( pvINVCL4)
          vRet = pvINVCL
       case isnull( pvINVCL2) and isnull( pvINVCL3) and isnull( pvINVCL4)
          vRet = pvINVHI1
       case  isnull( pvINVCL3) and isnull( pvINVCL4)
          vRet = pvINVHI2
       case   isnull( pvINVCL4)
          vRet = pvINVHI3
    end select
    GetInsCLValue= vRet
    end function

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,960
    Looks like non-normalized data structure is at root of this issue. Multiple similar name fields indicates non-normalized structure.

    The nested IIf is missing a comma after [INVHI]. This expression should error and not return anything. The first 2 conditions are identical, I think the first one is missing INVCL in the sum.

    Be aware that arithmetic with Null returns Null. If any one term in the sum is Null the answer will be Null, therefore the expression will always return [INVHI], unless every term is not Null.

    Maybe you want:

    Nz(INVCL, Nz(INVCL1, Nz(INVCL2, Nz(INVCL3, Nz(INVCL4, INVHI)))))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    [QUOTE=ranman256;243257]Build this function in a module, then In your query, make a virutal field: GetInsCLValue([INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4], [INVHI]) as InsCL

    Hi ranman256,

    I have established the function as module 2, copy and paste below code in a new field of the query table in design view :

    GetInsCLValue([INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4], [INVHI]) as InsCL

    I got error message of "The expression you entered contains invalid syntax".

    How do I get around this ?

    Thanks & regards
    Kiang

  5. #5
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    Quote Originally Posted by June7 View Post
    Looks like non-normalized data structure is at root of this issue. Multiple similar name fields indicates non-normalized structure.

    The nested IIf is missing a comma after [INVHI]. This expression should error and not return anything. The first 2 conditions are identical, I think the first one is missing INVCL in the sum.

    Be aware that arithmetic with Null returns Null. If any one term in the sum is Null the answer will be Null, therefore the expression will always return [INVHI], unless every term is not Null.

    Maybe you want:

    Nz(INVCL, Nz(INVCL1, Nz(INVCL2, Nz(INVCL3, Nz(INVCL4, INVHI)))))

    Thanks June7. The fields contain of text only.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,960
    Then the + character will concatenate. The & is preferred operator for concatenation.

    If any term of concatenation expression is not null then the result will be the text value.

    Does the nested Nz solve this issue?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    Quote Originally Posted by June7 View Post
    Then the + character will concatenate. The & is preferred operator for concatenation.

    If any term of concatenation expression is not null then the result will be the text value.

    Does the nested Nz solve this issue?
    I have fixed some careless error of comma etc and nested the field with Nz:

    InsCL: IIf(IsNull(Nz([INVCL])& Nz([INVCL1]) & Nz([INVCL2]) & Nz([INVCL3]) & Nz([INVCL4])),Nz([INVHI]),IIf(IsNull(Nz([INVCL1])& Nz([INVCL2]) & Nz([INVCL3]) & Nz([INVCL4])),Nz([INVCL]),IIf(IsNull(Nz([INVCL2])& Nz([INVCL3]) & Nz([INVCL4])),Nz([INVCL1]),IIf(IsNull(Nz([INVCL3])& Nz([INVCL4])),Nz([INVCL2]),IIf(IsNull(Nz([INVCL4])),Nz([INVCL3]),Nz([INVCL4])))))

    Still [InsCL] returns to empty field, even though all the fields are not null.

    I wonder how Nz(INVCL, Nz(INVCL1, Nz(INVCL2, Nz(INVCL3, Nz(INVCL4, INVHI))))) can be fitted into the above expression ?

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,960
    That expression won't give you what you want. The concatenated value will never be Null because Nz converts Null to empty string. Test for empty string.

    IIf(Nz([INVCL]) & Nz([INVCL1]) & Nz([INVCL2]) & Nz([INVCL3]) & Nz([INVCL4])="", [INVHI] ...

    Just replace the entire nested IIf() expression with the nested Nz() and see what happens. No VBA is needed with this approach.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,526
    in the query field its just: GetInsCLValue([INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4], [INVHI])

  10. #10
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    I copied & pasted GetInsCLValue([INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4], [INVHI]) as it is in design view of query table and Access auto changed it to :

    Expr1: GetInsCLValue([INVCL],[INVCL1],[INVCL2],[INVCL3],[INVCL4],[INVHI])

    when I clicked datasheet view, error message of undefined function of 'GetInsCLValue' in expression appeared.

  11. #11
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    I have done this :

    InsCL: Nz(IIf(IsNull([INVCL] & [INVCL1] & [INVCL2] & [INVCL3] & [INVCL4]),[INVHI],IIf(IsNull([INVCL1] & [INVCL2] & [INVCL3] & [INVCL4]),[INVCL],IIf(IsNull([INVCL2] & [INVCL3] & [INVCL4]),[INVCL1],IIf(IsNull([INVCL3] & [INVCL4]),[INVCL2],IIf(IsNull([INVCL4]),[INVCL3],[INVCL4]))))))

    Only Condition 5 of below works.

    Condition 1: If all fields of [INVCL],[INVCL1], [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVHI]
    Condition 2: If fields of [INVCL1], [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVCL]
    Condition 3: If fields of [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVCL1]
    Condition 4: If fields of [INVCL3], [INVCL4] contains null value, the field InsCL should equal [INVCL2]
    Condition 5: If only field of [INVCL4] contains a null value, the field InsCL should equal [INVCL3], else InsCL should equal [INVCL4]

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,960
    Have you tried the nested Nz()?

    InsCL: Nz(INVCL, Nz(INVCL1, Nz(INVCL2, Nz(INVCL3, Nz(INVCL4, INVHI)))))

    Or if I have the order of priority backwards try:

    InsCL: Nz(INVCL4, Nz(INVCL3, Nz(INVCL2, Nz(INVCL1, Nz(INVCL, INVHI)))))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    I pasted InsCL: Nz(INVCL, Nz(INVCL1, Nz(INVCL2, Nz(INVCL3, Nz(INVCL4, INVHI))))) in Design view of query table, only Condition 2 worked with [INVCL] was displayed. When [INVCL] is null, [INVHI] will not appeared etc.

    Condition 2: If fields of [INVCL1], [INVCL2], [INVCL3], [INVCL4] contain null value, then the field InsCL should equal [INVCL]

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,960
    Read post 12 again.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    gykiang is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2013
    Posts
    47
    Tried backward : InsCL: Nz(INVCL4, Nz(INVCL3, Nz(INVCL2, Nz(INVCL1, Nz(INVCL, INVHI)))))

    Only Condition 5 worked.

    Condition 5: If only field of [INVCL4] contains a null value, the field InsCL should equal [INVCL3], else InsCL should equal [INVCL4]

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filtering and Display of Multiple Date Fields
    By stevekroll in forum Queries
    Replies: 3
    Last Post: 03-17-2014, 07:59 PM
  2. Query to display null fields.
    By dazibit in forum Queries
    Replies: 1
    Last Post: 01-20-2014, 04:00 PM
  3. display multiple fields combo box
    By statty01 in forum Access
    Replies: 5
    Last Post: 07-03-2013, 05:29 PM
  4. Replies: 2
    Last Post: 01-08-2013, 04:59 PM
  5. Query to Display Tables & Fields
    By foxerator in forum Queries
    Replies: 0
    Last Post: 04-24-2008, 09:57 AM

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