Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9

    calculated fields appearing in table


    Hi

    I have read some threads about this topic but I need more detailed instructions. I have a form that has two calculated fields--age and year. The following formula appears in the control source:
    =Right(Str(Year(Date())),2) in Year field.
    =Int((DateDiff("d",[Birthday],Now())/365)) in Age field.

    I have the two fields in my table but the results do not appear. How do I get the results to appear in my table. I know that this is not recommended practice but I need to solve.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Access 2010 has a new field type - Calculated. You can do these calcs in the table then these calculated fields are available to the form or report. Otherwise, need VBA code to save the calculation result from form to table.
    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.

  3. #3
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    I am using 2010 and I do not see that choice calculated in table design--for future reference I would like to know how to do? I need to solve this problem in Access 2007. Can you supply the VBA code?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    If the fields are included with the RecordSource of the form then simply:
    Me!Birthday = Me.tbxAge
    or do the calc in the code in place of the textbox reference.

    The trick is figuring out what event to put the code in. Could be button Click or form Close.

    Re Access2010: http://allenbrowne.com/casu-14.html
    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.

  5. #5
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    Hi,
    Need more instruction. I put this code in an after update event procedure on the birthday field. I typed in Me!birthday = Me.tbxAge. It did not work. I did read that article. What does tbx stand for?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    tbx is a prefix I use when naming textboxes. Some people use txt. Other examples are cbx or cbo for combobox, lbx or lbo for listbox, etc. Use what you like or nothing. What you need to use is whatever names you have assigned to the textboxes.
    Last edited by June7; 07-18-2011 at 07:32 PM.
    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
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    Hi

    Still not sure exactly what I should be doing. I have the field birthday. I have the field age. In the Age field on the control source I have: =Int((DateDiff("d",[Birthday],Now())/365)) . The Birthday field is named birthday, the age field is named age. The event procedure is on the birthday field.
    Private Sub Birthday_AfterUpdate()
    Me!Birthday = Me.Age
    End Sub
    In the form, it changes the date in the birthday field to 1900 and gives me an age of 111. I do not get any results showing in the table.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Sorry, my goof.
    Me!Age = Me.tbxAge
    Me![Year] = Me.tbxYear

    Year is a reserved word. Should not use reserved words as names. Better would be BirthYear.
    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
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    Hi

    This is what I have now:
    Private Sub Birthday_AfterUpdate()
    Me!Age = Me.Age
    Me![Year] = Me.Year
    End Sub
    I get "You can't assign a value to this object"
    When I use tbx:
    Private Sub Birthday_AfterUpdate()
    Me!Age = Me.tbxAge
    Me![Year] = Me.tbxYear
    End Sub
    I get error--method or data member not found.
    This error occurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What are the names of the textboxes that the calculations are in?

    Do you intend to update the Age field every year?

    You are experiencing reasons saving calculated values is not advised. These two calcs are so simple and easy to accomplish when needed, as opposed to the programming effort and making sure the saved value does not get stale (the Age changes).
    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.

  11. #11
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    HI

    The text box names are Birthday and Age. The age field gets updated when the user enters info into the form which can be a daily entry.
    Willing to abandon this if you think it is not worth the effort.

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Give the controls a name different from the fields in the table. That's why I use tbx prefix.

    Even though I know it can be done, I really don't think you should. Sure, the age will be calculated and saved when the birthdate is entered but that data will get stale, would have to update it every year.
    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
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    Hi

    I renamed the text boxes and it worked in the form with the following:
    Private Sub Birthday_Change()
    Me!tbxBirthday = Me.tbxAge
    End Sub
    The event procedure is on change.
    How do you get the results to appear in the table?

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I would use AfterUpdate instead of Change event.

    Why are you setting the birthday control to the age value? Change the code to:

    Me!Age = Me.tbxAge

    This code should save value to table. I know, I tested it.

    Oh, did forget to mention that record and any edits to it will be committed to table when the form closes or move to another record.
    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
    jamhome is offline Novice
    Windows 2K Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    9
    Hi

    Works great! The user will be happy. Thank you.

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

Similar Threads

  1. Calculated fields
    By Madmax in forum Forms
    Replies: 1
    Last Post: 06-17-2011, 08:36 AM
  2. Replies: 1
    Last Post: 04-25-2011, 12:36 PM
  3. Calculated fields
    By graciemora in forum Forms
    Replies: 1
    Last Post: 02-04-2011, 06:07 AM
  4. calculated fields
    By nashr1928 in forum Forms
    Replies: 1
    Last Post: 07-21-2010, 04:49 PM
  5. Sum of calculated fields
    By nkuebelbeck in forum Forms
    Replies: 9
    Last Post: 03-12-2010, 01:32 PM

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