Results 1 to 11 of 11
  1. #1
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232

    Calculated firld in a table

    I have a calculated field in a table named FinancedPrice I have the following in the expression Round([price],0)

    I have a field MiscPrice that I would like to add the total to the calculated field financedprice.

    Sometime this miscprice field is null.

    Thank you Angie

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You can use the Nz() function I think.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    An alternative to pbaldy's suggestion is to use the IIF() function. TotalPrice: IIF([MiscPrice] is null, 0, [MiscPrice]*[FinancedPrice])
    The advantages here being your results will be of the correct data type.
    http://allenbrowne.com/QueryPerfIssue.html

  4. #4
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    Quote Originally Posted by kd2017 View Post
    An alternative to pbaldy's suggestion is to use the IIF() function. TotalPrice: IIF([MiscPrice] is null, 0, [MiscPrice]*[FinancedPrice])
    The advantages here being your results will be of the correct data type.
    http://allenbrowne.com/QueryPerfIssue.html

    I am trying to add the two together in a calculated table field.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I believe the Nz() will return the correct type, as long as you specify the replacement value. I'm not sure if it's allowed in calculated fields though.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Quote Originally Posted by pbaldy View Post
    I believe the Nz() will return the correct type, as long as you specify the replacement value. I'm not sure if it's allowed in calculated fields though.
    As I understand it vba's Nz() returns a variant which jet doesn't understand so jet turns them into strings in a query. I've just confirmed this with a quick test query in access 2016, both nz parameters were numerics but the query returned strings.

    The Nz() function replaces Null with another value (usually a zero for numbers, or a zero-length string for text.) The new value is a Variant data type, and VBA tags it with a subtype: String, Long, Double, Date, or whatever.
    This is great in VBA: a function can return different subtypes at different times. But in a query, a column can be only be ONE data type. JET therefore treats Variants as Text, since anything (numbers, dates, characters, ...) is valid in a Text column.
    The visual clue that JET is treating the column as Text is the way it left-aligns. Numbers and dates display right-aligned.

    If you expected a numeric or date column, you now have serious problems. Text fields are evaluated character-by-character. So 2 is greater than 19, because the first character (the 2) is greater than the first character of the other text (the 1 in 19.) Similarly, 4/1/2009 comes after 1/1/2010 in a Text column, because 4 comes after 1.

    Alarm bells should ring as soon as you see a column left-aligned as Text, when you expected it handled numerically. Wrong records will be selected, and the sorting will be nonsense.
    You could use typecast the expression with another VBA function call, but a better solution would be to let JET do the work instead of calling VBA at all.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    In a query I agree. I'm not sure that applies in a calculated field. I don't use them, so I'll bow out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    I don't believe Nz() is an available function within a calculated field expression.

  9. #9
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    Quote Originally Posted by kd2017 View Post
    I don't believe Nz() is an available function within a calculated field expression.
    I need to calculate a field in a form that will hold the information in the table. If I do this using a query will it show in the table in the field. Are should I use a Vb code in the field on the form ( on change) to add the two fields together.

  10. #10
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Quote Originally Posted by angie View Post
    I have a calculated field in a table named FinancedPrice I have the following in the expression Round([price],0)

    I have a field MiscPrice that I would like to add the total to the calculated field financedprice.

    Sometime this miscprice field is null.

    Thank you Angie
    As pbaldy alluded to most prefer not to use calculated fields to store calculations within a table itself, it can lead to problems. Here is a short article you can read on the subject http://www.allenbrowne.com/casu-14.html

    What most would do is instead of storing the results of a calculation in a table, they would instead store all the variables required to make the calculation, then they would do the arithmetic in a query (or form, or report) at runtime. If you have stored the results of a calculation in a table and the variables used to get calculation change the will not automatically be recalculated and now your calculation is wrong. But if you redo the calculation each time in a query you know the figure is reliable.

    These videos will show you how to do calculations in a query:
    https://www.youtube.com/watch?v=XeGUubpAcE0
    https://www.youtube.com/watch?v=pAnfjCu0c-Q
    https://www.youtube.com/watch?v=TKHyv1GhdDY

    Quote Originally Posted by angie View Post
    I am trying to add the two together in a calculated table field.
    If you must use a calculated field directly within a table structure then you'll have to use the IIF() statement to check that your miscprice field is not null. IIF([miscprice] is null, 0, [miscprice]) will return zero if the miscprice is null or return miscprice if it is not null. To use it in the calculated field it might look like Round([price] + IIF([miscprice] is null, 0, [miscprice]), 0)

    Quote Originally Posted by angie View Post
    I need to calculate a field in a form that will hold the information in the table. If I do this using a query will it show in the table in the field. Are should I use a Vb code in the field on the form ( on change) to add the two fields together.
    I think there should be no coding required for what you've described. You can make a query that performs your calculations and build your forms off that query just like you would build a form off a table. You can also do calculations directly in a textbox's control source.

  11. #11
    angie is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    232
    Quote Originally Posted by kd2017 View Post
    As pbaldy alluded to most prefer not to use calculated fields to store calculations within a table itself, it can lead to problems. Here is a short article you can read on the subject http://www.allenbrowne.com/casu-14.html

    What most would do is instead of storing the results of a calculation in a table, they would instead store all the variables required to make the calculation, then they would do the arithmetic in a query (or form, or report) at runtime. If you have stored the results of a calculation in a table and the variables used to get calculation change the will not automatically be recalculated and now your calculation is wrong. But if you redo the calculation each time in a query you know the figure is reliable.

    These videos will show you how to do calculations in a query:
    https://www.youtube.com/watch?v=XeGUubpAcE0
    https://www.youtube.com/watch?v=pAnfjCu0c-Q
    https://www.youtube.com/watch?v=TKHyv1GhdDY



    If you must use a calculated field directly within a table structure then you'll have to use the IIF() statement to check that your miscprice field is not null. IIF([miscprice] is null, 0, [miscprice]) will return zero if the miscprice is null or return miscprice if it is not null. To use it in the calculated field it might look like Round([price] + IIF([miscprice] is null, 0, [miscprice]), 0)



    I think there should be no coding required for what you've described. You can make a query that performs your calculations and build your forms off that query just like you would build a form off a table. You can also do calculations directly in a textbox's control source.
    Thank you for the information. I went back to using a query.

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

Similar Threads

  1. Add calculated results on table
    By kallu in forum Forms
    Replies: 8
    Last Post: 05-13-2017, 05:12 AM
  2. Add Calculated Field to Table
    By jmccon in forum Access
    Replies: 1
    Last Post: 10-12-2015, 06:23 AM
  3. Add a calculated fields in a Table
    By Eef in forum Database Design
    Replies: 2
    Last Post: 12-08-2014, 07:24 AM
  4. Replies: 2
    Last Post: 10-24-2012, 02:53 PM
  5. Replies: 5
    Last Post: 10-04-2012, 07:40 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