Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107

    Calculation in text box

    Hello,



    I would like to display a % complete on my form. i have built a query that sums my earnings and required score to give me the % complete. But when I include this query in my form query it prevents the form from being able to update.

    SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [EarnedScore]/[RequiredScore] AS [%Complete]
    FROM SumScore;

    I would like to add a unbound text box to my form and code the % Complete calculation to it. Can someone help me with this?

    Thank you.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    in the textbox put

    =[EarnedScore]/[RequiredScore]

    format the textbox as required to display as a percentage

    you may need to protect from divide by zero errors e.g.

    =iif(nz([[RequiredScore],0)=0,0,EarnedScore]/[RequiredScore])

  3. #3
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    My issue is that I use a query to build the sum of earnedscore and requiredscore. If I add this query to my form it will prevent me from updating records. So if I put the formula =[EarnedScore]/[RequiredScore] in the text box it gives me the ?name error.

    The link between this form and the earnedscore and requiredscore query is OrderNum

  4. #4
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If you add a left join from your order details to the sum query , and use that as your record source you can probably edit the records.

    Try in datasheet view in the query editor before rearranging your form, if you can't edit in query datasheet view, you definitely won't be able to edit the form.
    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
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    Left Join does not work. Could I code this in VBA and display in the text box?

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by sparker75 View Post
    Could I code this in VBA and display in the text box?
    Assuming that the query returns the correct value, you could write a UDF....
    BTW, you shouldn't use special characters in object names: don't use "AS [%Complete]", use "AS [PctComplete]" as the column alias.


    This is based on what you posted, so something like:
    Code:
    Function fnCalcPct() As Double
        Dim r as DAO.Recordset
        Dim sSQL as String
    
        'Default return value
        fnCalcPct = 0
    
       ' open inline SQL (or saved query)
        sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [EarnedScore]/[RequiredScore] AS [PctComplete]"
        sSQL = sSQL & " FROM SumScore;"
      
        set r = CurrentDb.OpenRecordset(sSQL)
        fnCalcPct = r!PctComplete
    
        r.Close
        Set r = Nothing
    End Function
    Follow Alex's advise about the divide by 0 problem.


    In the text box, you would use
    Code:
    =fnCalcPct()

  7. #7
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    HI Steve,

    Thank you! fnCalcPct = r!PctComplete is giving me an error fnCalcPct= r!PctComplete = Null

    Public Function fnCalcPct() As Double
    Dim r As DAO.Recordset
    Dim sSQL As String


    'Default return value
    fnCalcPct = 0


    ' open inline SQL (or saved query)
    sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]"
    sSQL = sSQL & " FROM SumScore;"

    Set r = CurrentDb.OpenRecordset(sSQL)
    fnCalcPct = r!PctComplete


    r.Close
    Set r = Nothing
    End Function

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try
    Code:
    Set r = CurrentDb.OpenRecordset(sSQL)
    If Not r.BOF and Not r.EOF Then
        fnCalcPct = r("PctComplete")
    End If
    If you create a saved query
    Code:
    SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore,  [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete] FROM SumScore;
    then execute the query, is there a value returned for "PctComplete"?

  9. #9
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    This is the saved query I'm using for the SumScore

    SELECT tblTransactionReviews.OrderNum, Sum(tblTransactionReviews.WeightTotal) AS RequiredScore, Sum(tblTransactionReviews.WeightEarned) AS EarnedScore
    FROM tblTransactionReviews
    GROUP BY tblTransactionReviews.OrderNum, tblTransactionReviews.NotRequired
    HAVING (((tblTransactionReviews.NotRequired)=False));

    This is the VBA code as it sits now and the error Im getting "Run-Time error '94': Invalid use of Null"

    Public Function fnCalcPct() As Double
    Dim r As DAO.Recordset
    Dim sSQL As String


    'Default return value
    fnCalcPct = 0


    ' open inline SQL (or saved query)
    sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]"
    sSQL = sSQL & " FROM SumScore;"

    Set r = CurrentDb.OpenRecordset(sSQL)
    If Not r.BOF And Not r.EOF Then
    fnCalcPct = r("PctComplete")
    End If


    r.Close
    Set r = Nothing
    End Function

  10. #10
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    Anyone have any ideas on my this is giving the error?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If it's possible the return value from the query is Null, you'd have to use Variant:

    Public Function fnCalcPct() As Variant
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    That got rid of the error. Thank you Paul. Now my issue is that the value is always null even when there should be a value. Is there a way to test this on each step?


    Public Function fnCalcPct() As Variant
    Dim r As DAO.Recordset
    Dim sSQL As String


    'Default return value
    fnCalcPct = 0


    ' open inline SQL (or saved query)
    sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]"
    sSQL = sSQL & " FROM SumScore;"

    Set r = CurrentDb.OpenRecordset(sSQL)
    fnCalcPct = r!PctComplete


    r.Close
    Set r = Nothing
    End Function

  13. #13
    sparker75 is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jul 2018
    Posts
    107
    Its missing the where?

    This is a test query I made

    SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]
    FROM SumScore
    WHERE (((SumScore.OrderNum)=70026286));


    and it produces a result. But I dont see a where in the above code.






  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try this (note the table name removed from the calculation, the brackets were wrong):

    sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [EarnedScore]/[RequiredScore] AS [PctComplete]"
    sSQL = sSQL & " FROM SumScore;"

    I assume SumScore is returning a single record? Otherwise, do you need to add a criteria to your SQL?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by sparker75 View Post
    Its missing the where?

    This is a test query I made

    SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]
    FROM SumScore
    WHERE (((SumScore.OrderNum)=70026286));

    and it produces a result. But I dont see a where in the above code.
    Your code has no criteria. Where would that number come from? It could be hard coded or more likely retrieved from the form:

    sSQL = "SELECT SumScore.OrderNum, SumScore.EarnedScore, SumScore.RequiredScore, [SumScore.EarnedScore]/[SumScore.RequiredScore] AS [PctComplete]"
    sSQL = sSQL & " FROM SumScore "
    sSQL = sSQL & "WHERE OrderNum= " & Me.TextboxName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Create a Calculation to Produce Text
    By DigitalAdrenaline in forum Access
    Replies: 6
    Last Post: 08-12-2018, 10:55 AM
  2. Text Box Change Color on Calculation
    By MarkV in forum Access
    Replies: 2
    Last Post: 03-10-2015, 05:34 PM
  3. Help with Text calculation
    By yagerlegi in forum Forms
    Replies: 4
    Last Post: 12-04-2014, 08:54 PM
  4. Replies: 12
    Last Post: 11-13-2014, 12:08 PM
  5. Replies: 29
    Last Post: 06-05-2014, 09:34 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