Lesson 8 - More Complex Queries
Lesson 8 - More Complex Queries
Lesson 8 - More Complex Queries
Contents
Next
In ancient times, like 20 years ago, dates were stored as strings and we all remember what that
brought about in 1999. Now all DBMSs handle dates in a Date/Time format, which makes our
lives a lot simpler, but we have to be aware of the particular properties of Date formats.
Before we begin writing queries there is an aside we must make about formatting.
Access is a Microsoft product, obviously. As with all Microsoft products it works closely with
Windows, obviously. When it comes to formatting for different countries Access gets its
information from the Windows Control panel.
The Windows default values are basically American. But, if you live in France for example, the
standard date format is dd-mm-yy and not mm-dd-yy as you are used to. And some countries use
the comma as a decimal separator and put the currency sign to the right of the amount and so on.
Therefore, before starting with Access you should go to the Control panel ---> Regional settings
and adjust all the settings for your particular situation.
To avoid confusion with dates I preach and preach to all who will hear that you should
automatically adopt the ANSI standard for dates and that is: yyyy-mm-dd. For example, January
1st 2003 should always be input into Access as: 2003-01-01.
function to display the number rounded to 'digits' positions after the decimal:
ROUND(Datediff('m', p_StartDate, p_EndDate), 2).
In theory, Datediff('yyyy', c_BirthDate, now()) returns the customer's age, expressed in years.
In practice however, you will find that it works or doesn't work depending on whether the person
has had his birthday yet this year or not. Usually it doesn'y work very well.
To calculate the exact age, the following formula is quite accurate. There may be a small
variation of a day or so once in a while.
INT(Datediff('d', c_BirthDate, now())/365.25)
Calculate the number of days between birth and now and divide by the exact number of days in a
year, which, as you know, is 365.25 and not 365. That takes leap years into account.
The Integer function, INT( ) , truncates the result so that 25.9 becomes 25, for example; the
person is 25 years old until the day she turns 26; after the age of 5, you rarely hear people say
that they are 25 and a half years old.
For example, if you have a Sales table with a s_Quantity column and a s_Amount column, you
would do:
LineTotal: [s_Quantity] * [s_Amount]
One of the frequent uses of this is to do concatenation, that is, stick two strings together.
For example, instead of having Last name and First name in separate boxes on a form, I want to
see First name and Last name together.
To get that I have to put it into a query:
Once in a while you will have to do maintenance operations on the tables: change a group of
prices, delete all obsolete items, etc. For those operations you use a different type of query
called: an action query.
We'll look at two: the Update and the Delete.
First, create a new query and specify the table that will be affected, same as the Select query.
Then, choose the Query type from the toolbar.
For the Update query, specify the operation on the update line: what you want to change and the
criteria: which records will be changed.
For the Delete query, all you have to specify is the Criteria.
You cannot ask to delete certain columns in certain records; you can only delete entire records.
After choosing Delete query, specify the Criteria to delete records:
Next week: Creating a Master/Detail Form for transactions. See you then!
Top