Well, it turns out that virtually any ms-access application I made has code,
and quite a bit of it...
Some simple, but useful examples:
Display a person age on a form.
You can have a birthdate field (text box control) on a form where you enter
the persons birthday. Beside that text box, you also want
to display the age of the person. So, you put some code in the module to
calculate the age.
The code:
Function Age(BirthDate) As Integer
Dim DateToday As Date
DateToday = Date ' "date is a built in function that
' returns the current date.
'
' We could have used this "date"
' function in the code below, but
' by placing the "date" into a
' variable called DateToday, the code
' is easy to read and understand.
' Returns the Age in years
Age = Year(DateToday) - Year(BirthDate)
' we still need to check if the month of the year
' has past..if not...birthday has not yet occureed!!
If Month(DateToday) < Month(BirthDate) Then
Age = Age - 1
End If
' perhaps it is the "same month", but the day
' of birth has not yet ocucred
If Month(DateToday) = Month(BirthDate) Then
' ok...same month as today..but check for the
' date in the month...
If Day(DateToday) < Day(BirthDate) Then
' ok..same month..but the day
' has not yet happened...so
Age = Age - 1
End If
End If
End Function
now, place a text box on the form, and we put the persons birthday, and also
today's date...
=Age([PersonBirthDay])
Of course, you change the above age expression "personbirthday" to whatever
the name of the field is that you have that is the actual birth day.
The advantage of using this function is now that when we build a report, we
also can place a text box on the report, and use the above function also.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.members.shaw.ca/AlbertKallal