ACCESS VBA MACROS EXERCISES▼
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.
You can learn how to do this exercise if you attend the course listed below!
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:
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:
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.