Access VBA Macros | Creating functions exercise | Write a function to show how long someone lived

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the course listed below!

Software ==> Access VBA Macros  (25 exercises)
Version ==> Access 2010 and later
Topic ==> Creating functions  (4 exercises)
Level ==> Relatively easy
Course ==> Access VBA macros
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

The aim of this exercise is to show a list of actors who are no longer with us, sorted by how long they were alive:

Actors by lifespan

Whoever knew Zsa Zsa lived so long?

To do this, create a function called YearsLived which will have two input arguments:

Argument What it will hold
BirthDate The date someone was born
DeathDate The date someone died

Think carefully not only what data type you should assign to these arguments, but also what data type the function should return.

Use the DATEDIFF function to determine someone's lifespan in years from these two bits of information, and return this from your function.  The syntax is:

DateDiff( "yyyy", earlier date, later date)

Test that your function works in the immediate window, then incorporate it into a query to show the years each actor was alive (only shows actors whose DoD field is not null).  Zsa Zsa Gabor lived the longest; who lived the shortest number of years?

Save and close any queries, modules or tables you have open.

This page has 0 threads Add post