Microsoft Access VBA Macro Programming

(Tina Sui) #1

Intervalis the time period based on the following table, and date is the date you want to
inspect.


Time Period Interval
Year yyyy
Quarter q
Month m
Day of Year y
Day d
Weekday w
Week ww
Hour h
Minute n
Second s

TheDatePartsyntax is as follows:

MsgBox DatePart("q", "1-mar-03")


This will return the result 1 because 1-Mar-03 is in quarter 1.
The following will return the result 3 because March is the third month:


MsgBox DatePart("m", "1-mar-03")


DateSerial


DateSerialreturns the date serial for a specific year, month, and day entered as integers. The
date serial is the actual number representing that date:


DateSerial (year, month, day)


whereyearis a number between 100 and 9999 or a numeric expression;monthis a number
between 1 and 12 or a numeric expression; anddayis a number between 1 and 31 or a
numeric expression.
For example, the following will return the value 37686, which is the date 6-Mar-2003:


MsgBox CDbl(DateSerial(2003, 3, 6))


You need to use CDbl (convert to double) in this code, or the message box will display the
date per the format in the Windows Control Panel rather than as an actual number.


Chapter 5: Strings, Functions, and Message Boxes 59

Free download pdf