Results 1 to 10 of 10
  1. #1
    N20GF is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2024
    Posts
    2

    Simple calculated field question

    Relatively new Access user because I am tired of using Excel as a psuedo-database. :-)

    Q: Given one field with a date entry, I need a calculated field that generates a date that is the last day of the sixth calendar month following from the manual date entry. For instance, today's date of January 25, 2024, the calculated field results in July 31, 2024. For the life of me I can't figure out what the syntax is to do this. I've had plenty of caffeine today, so that's not my problem.



    Forgive the newbie question; I checked answers already here and cruised a few other forums. Even the Microsoft help pages don't seem to .. umm, help.

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    This may help to get started
    ?Date()
    25-Jan-24
    ?DateSerial(year(Date()), month( Date() ) +6 +1, 0)
    31-Jul-24

    DateSerial gives a Date where:

    Year(date()) Gives current year
    month( Date() ) +6 +1) gives the Month that is 6 months from Date, and the +1 adds 1 month (July)
    , 0 gives the day before or gives the last day of the previous month

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You're not storing this calculation in a table, I hope?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    N20GF is offline Novice
    Windows 11 Office 365
    Join Date
    Jan 2024
    Posts
    2
    At the risk of sounding like a complete Access rookie, well wouldn't the calculations be kept within the table?

    Here's what I am trying to do, and as I mentioned I'm using Access for the first time - and essentially have no other db building experience - ha ha Microsoft Works does not count:

    Keep a list of employees ... easy enough to build.
    Enter certain training qualifications, with dates completed. Also simple.
    Have Access calculate when the requalification is due to be completed again. There is a drop-dead date for each that is the final day of x months after the qualification was completed initially.
    Then be able to generate a report that gives me a list of employees a b and c that are expiring with qualification x or y or whatever in a specific month in which I'm interested. For instance, hey Access tell me all the employees that expire in the month of August this year so I can anticipate and plan for training events required several months in advance.

    Sounds simple .. and the basic concept is. Can it be done on Excel? Yup. But my dataset of employees who have multiple training quals, with different expiration dates, who build different widgets with that have different machine quals, is getting too cumbersome to be tracked with Excel. So this is a good project for me to work with and self-educate on Access in the meantime.

  5. #5
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    Oh, I see your problem. In Excel, there are "tables" (sort of) and there are calculations/expressions/whatever, where you use date etc functions to do this kind of thing. Access is a little bit different. Basically, tables are for storing data. Anything that you can derive, you can basically create queries for. (For example, the date you're talking about).

    SELECT col1, col2, SomeDate, DATEADD("m",3,SomeDate) AS ThreeMonthsAway
    FROM Mytable

    and then ThreeMonthsAway gets calculated when you open the query. (So you could do something like base a report on that query, or base another query on it.)

  6. #6
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    There is also the possibility of adding a calculated field to the table. Doing so will make the field available for every query, form or report. In most databases you can make those fields persistent and index them, but I don't think that's possible working with native Access tabels. So the general rule is: if it has to be available in a lot of objects: create a calculated field in in the table. If the field is only used in 1 or 2 reports/forms: calculate it in a query/view.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    wouldn't the calculations be kept within the table
    in theory they can be although you say

    Enter certain training qualifications, with dates completed. Also simple.
    Have Access calculate when the requalification is due to be completed again. There is a drop-dead date for each that is the final day of x months after the qualification was completed initially.
    the problem here is that you also say

    employees who have multiple training quals, with different expiration dates
    I'm reading that as different periods (i.e. not all qualifications have a 6 month expiry date, some will last for 6 months , some for 12, etc) so I would expect the expiration period to be related to the qualification, not the individual - so that value is in a different table - and table calculations can only reference values in the same record

    The other issue is that calculated fields cannot be indexed, so if you have large data sets the query will be slow.

    I would expect you to have 3 tables

    tblEmployees
    employeePK
    employeeName

    tblQualifications
    qualificationPK
    qualificationName
    expiryPeriod

    tblQualified
    QualifiedPK
    employeeFK
    qualificationFK
    qualifiedDate

    and the query would be

    Code:
    SELECT employeeName, qualificationName
    FROM (tblEmployees E INNER JOIN tblQualifications Q ON E.employeePK=Q.employeeFK) INNER JOIN tblQuailified R ON Q.qualificationPK=R.qualificationFK
    WHERE Format(DateSerial(year(qualifiedDate), month(qualifiedDate) +expiryPeriod +1, 0),'yyyymm')=[enterDate (yyyymm)]
    

  8. #8
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    The other issue is that calculated fields cannot be indexed, so if you have large data sets the query will be slow.

    Indexes only make a difference when you put a selection criterium on the indexed field, and doing the calculations in queries is certainly not faster than doing them in the table.
    But if the value is in another table, you need to do the calculations in a query. But NEVER do the calculation on the left side of the equality. That is one of the worst things you can do in a query. No index can be used and the calculation has to be made for every record.

    Always do something like

    Code:
    WHERE [MyField] = calculatedValue
    The value has to be calculated once and, if there exists an index on [MyField], the engine can use it.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    wouldn't the calculations be kept within the table?
    Personally, I would steer away from calculated table fields for the most part.
    http://allenbrowne.com/casu-14.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Indexes only make a difference when you put a selection criterium on the indexed field
    good point - in the query I posted below, if the qualifiedDate is indexed then I would modify the where clause to

    Code:
    WHERE qualifiedDate Between dateadd("m",-expiryPeriod -1,datevalue([enterDate (yyyy-mm)] & "-01")) AND dateadd("m",-expiryPeriod,datevalue([enterDate (yyyy-mm)] & "-01"))-1

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

Similar Threads

  1. Replies: 10
    Last Post: 03-15-2020, 01:46 AM
  2. Replies: 5
    Last Post: 05-03-2019, 10:16 AM
  3. Calculated field question
    By Hello1 in forum Access
    Replies: 3
    Last Post: 12-16-2015, 06:43 PM
  4. Replies: 5
    Last Post: 06-13-2015, 04:29 PM
  5. Calculated Field on form (beginner question)
    By Williams485 in forum Access
    Replies: 21
    Last Post: 02-26-2013, 09:02 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