Results 1 to 4 of 4
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to return values found within the table?

    Hello all - I have a table where I have a "Yes/No" column. I need a new field; if that record contains "Yes" then I need to look up the ParentComp field and return whatever the debt is for that company. Example, Comp3 should return the debt for Comp1 which is 504. If the field = "No", then return the debt it has, ex. Comp1 would return a debt of 504.



    Any ideas on what function to use? DLookup? Not sure here.
    Attached Files Attached Files

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    something like: getDebtViaID()


    function getDebtViaID(byval pvCustID)
    dim bValid as boolean
    dim vID


    'here yes/no is boolean , not a string 'yes' or 'no'
    bValid = dlookup("[field]","table","[CustID]=" & pvCustID)

    if bValid then 'get parents debt
    vID = dlookup("[ParentComp]","table","[CustID]=" & pvCustID)
    getDebtViaID= getDebt(vID)
    else 'get normal debt
    getDebtViaID= getDebt(pvCustID)
    endif
    exit function


    function getDebt(byval pvCustID)
    getDebt = dlookup("[field]","table","[CustID]=" & pvCustID)
    end function

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    IMHO your table needs to be changed. I have removed the "Yes/No" field. As far as I can see it serves no purpose. If the "ParentComp" field has data, then the company has a Parent Comp.
    In any case "Special" keys like "/" should not be used in the name of any object in Access. Neither should "Reserved" words be used, so I have changed the first field to "CName" rather than "Name".
    I have not added one but all tables should have a Primary Key. An auto-number field is often a suitable choice.

    Take a look at the query in the attached db. I believe this fulfils your requirement.
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @ Bob,
    I modified your query so that when the YN column is No, the debt field is returned:
    Code:
    NEW: IIf(Len([ParentComp] & "")<=0,[debt],DLookUp("debt","sheet1","[CName] = '" & [parentcomp] & "'"))

    If the field = "No", then return the debt it has, ex.
    Comp1 would return a debt of 504.
    Here is the SQL of the query
    Code:
    SELECT Sheet1.CName, IIf(Len([ParentComp])>0,"Yes","No") AS YN, Sheet1.debt, Sheet1.ParentComp, IIf(Len([ParentComp] & "")<=0,[debt],DLookUp("debt","sheet1","[CName] = '" & [parentcomp] & "'")) AS NEW
    FROM Sheet1;

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

Similar Threads

  1. Look for two values in table and return message
    By MikeN in forum Programming
    Replies: 3
    Last Post: 11-18-2015, 10:39 AM
  2. Query to return name if no record found
    By jrosen12 in forum Queries
    Replies: 1
    Last Post: 12-18-2014, 06:32 PM
  3. Replies: 7
    Last Post: 11-21-2014, 05:28 PM
  4. Replies: 8
    Last Post: 11-04-2011, 06:52 AM
  5. Return 0 if no records found
    By skwilliamson in forum Programming
    Replies: 1
    Last Post: 12-01-2009, 10:54 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