MS ACCESS - INTRODUCTION MS Access is an example of a relational database. It allows to build and maintain small and medium-sized databases and to supply them with a graphical user interface. The aim of the exercise is to get acquainted with MS Access environment and basic techniques of creating a database. MS Access database objects MS Access databases consist of a single file (.mdb). The file contains the following objects: TABLES data sets related to the same subject (each subject should be given a separate table). Data in tables is organized in records (rows) and fields (columns). QUERIES stored database queries retrieving data from tables or requests for performing some specific database operation (create or drop an object, insert, update or delete records). They allow to find data from different tables according to some given criteria and perform some operation on it. FORMS GUI structures meant for manual data management (displaying, entering, updating or deleting). The operate on a one or more tables and can be based on the queries. Forms can be created manually or with a wizard. REPORTS they are meant form data analysis (with some calculations) or presentation (usually in printed form) according to user criteria. They also can be based on the queries. DATA ACCESS PAGES HTML pages performing operations similar to forms. They can also use different data sources, e.g. MS Excel worksheets. MACROS predefined actions (sets of actions) responsible for operations such as form opening or report printing. They introduce some automation without need of programming (although typing or correcting the macro code is still possible). MODULES VBA structures, similar to the ones in MS Excel. Creating a database Launch MS Access, select New from File menu (or click on a toolbar icon, Ctrl + N). Choose empty database option and save it (.mdb file). Once a database is created, one can manage it and all its objects with the object manager: object actions tables queries object-group specific actions forms reports data access pages macros existing objects modules Exercise 1 Create a database with a database wizard. Check options available in the wizard. Exercise 2 Create an empty database and save it as collector. page 1 of 6
TABLES Table is an object meant for storing data related to the same subject. Each record of a table contains information about a particular object within the subject and consists of fields with the detailed data. Tables consist on many fields separately for each portion on information that can be stored in the table. Creating a table requires naming each field and assigning it a specific type. Fields in MS Access tables can be assigned one of the following types: TEXT alphanumeric string with the maximum length on 255 characters. Even if the field consists of a digits (its value looks like a number), it cannot be used for calculations. MEMO as above, but with the maximum length if 65535 characters. NUMBER a numeric field whose value can be used for calculations (except for the financial ones). The number can be 1, 2, 4 or 8-byte long. DATE/TIME date or time, 8 bytes. CURRENCY similar to a number, however during the calculations they are not rounded. Accuracy: 15 integer places and 4 decimal, 8 bytes. AUTONUMBER unique uneditable number automatically assigned to a new row, 4 bytes. YES/NO logical (boolean) values, i.e. true/false, yes/no, on/off, 1 bit. OLE OBJECT object created with any software compliant withe OLE protocol (linked or embeded), e.g. MS Word document, MS Excel document, picture, sound, video, up to 1 GB. HYPERLINK hyperlink, maximum length 3 2048 characters. LINK CREATOR creates a field enabling selecting value from another table or from a list, usually 4 bytes. When designing a a table and choosing a data type for the fields it is necessary to consider the following: what values will be allowed for the field? how long will be the maximum data (bytes)? what operations will be performed on the field values? will the field values be sorted (and how) or indexed? will the field be used for grouping data in queries or reports? Field attributes Besides the data type, the fields can be assigned additional attributes responsible for data presentation and entering. SIZE a maximum character count for a text or a number. Number type Range Precision Memory byte 0 255 N/A 1 B integer -32768 32767 N/A 2 B long integer -2147483648 2147483647 N/A 4 B single precision 1.4 10-45 3.4 10 45 4 4 B double precision 4.9 10-324 1.8 10 308 15 8 B decimal -10-28 -1 10-28 -1 28 12 B FORMAT a standard data presentation (display) format number, currency 6 versions, date/time 7 versions, page 2 of 6
yes/no 3 versions. The most common symbols are: for numeric fields:. decimal separator,, thousand separator, 0 arbitrary digit, # arbitrary digit or no digit, $ dolar symbol, % percent symbol, E-, e- exponential notation, E+, e+ exponential notation, [colour] number colour; for text fields: @ obligatory character, & optional character, < all letters will be converted to lower case, > all letters will be converted to upper case; for date/time fields : hour separator (separators are dependent on Windows settings), / date separator, d one or two digit month day (from 1 to 31), dd two digit month day (from 01 to 31), ddd first three letters of a week day (from Sun do Sat), dddd full week day (from Sunday do Saturday),, m one or two digit month (from 1 to 12), mm two digit month(from 01 to 12), mmm first three letters of a month (from Jan to Dec), mmmm full month (from January to December), k date displayed as a quarter (from 1 to 4), r day number in the year (from 1 to 366), rr last two digits of year (from 01 to 99), rrrr full year (from 0100 to 9999), g one or two digit hour (from 0 to 23), gg two digit hour (from 00 to 23), n one or two digit minutes (from 0 to 59), nn two digit minutes (from 00 to 59), s one or two digit seconds (from 0 to 59), ss two digit seconds (from 00 to 59), AM/PM twelve hour clock with AM or PM (upper case), am/pm twelve hour clock with am or pm (lower case), A/P twelve hour clock with A or P (upper case), a/p twelve hour clock with a or p (lower case); for logical fields (text displayed instead of yes and text displayed instead of no ). DECIMAL PLACES displayed (not stored) number of decimal places for numbers and currencies. Auto according to the format settings or number from 0 to 15 (does not matter in case of undefined or general format). page 3 of 6
INPUT MASK a pattern that the entered data must match. It allows to control a way of entering information. The mask can be designed with a mask creator (text or data/time fields) or manually according to the pattern. The most common symbols are: 0 digit (obligatory), 9 digit or space character (optional), # digit, space, + or - (optional, in edition mode spaces are displayed as empty places but they are removed when saving data), L letter (obligatory),? letter (optional), A letter or digit (obligatory), a letter or digit (optional), & arbitrary character or space (obligatory), C arbitrary character or space (optional),., : ; - / separators, < all letters will be converted to lower case, > all letters will be converted to upper case, n any following character will be displayed as a regular one; Setting the input mask property to Password creates a format for entering passwords. Any character will be displayed as an asterisk *. TITLE displays a column header different from the actual field name (up to 2048 characters). DEFAULT VALUE a value automatically inserted into a field in order to accelerate data entering. Ctrl+Alt+Space restores the default value, Ctrl+' inserts a value from the record above. VALIDATION RULE a criterion responsible for preventing erroneous data. ERROR MESSAGE a message displayed when attempting to enter some data not complying with the validation rule. OBLIGATORY defines if a field must contain some data or may be empty (Null value). If this property is set to No, the integrity rule must be extended with a condition Is Null. ZERO LENGTH ALLOWED defines if an empty string is a valid value (for text, memo and hyperlink fields). Together with the previous property it enables to distinguish between non-existing information and existing unknown information. The combination of both properties results in: Obligatory Zero length allowed Null Empty string No No OK - No Yes OK OK Yes No - - Yes Yes - OK INDEXED indices accelerate data finding and sorting when performing queries. Indexing of memo, hyperlink and OLE object fields is not supported, usually it is useless to index yes/no fields, autonumber fields are indexed by default. The available options are: No no index, Yes (Duplicates OK) field values may duplicate, Yes (No duplicates) field values must be unique. A field should be indexed if: page 4 of 6
data searching or sorting will involve a field, field values will be various, a field will be used as a one of query condition. Index edition is possible from View Indices. Its properties are: Primary if yes, index is automatically set for a field being a primary key, Unique if yes does not allow field values duplicates, Null determines if null-values should be also indexed (affects memory consumption), Multifield index does not rely on a single field, but on a set of fields constituting to the indexed value (e.g. none of the fields is unique, but the set is). Be aware, that indices may deteriorate database state updates (inserting, updating or deleting data), therefore they should be created very carefully, only if really needed. Creating a table In order to create a table follow the steps: choose Tables in object manager and then open Designer (or Create table in designer view), enter the subsequent fields (names and data types), define attributes and properties of all the fields, create indices and primary keys (by means of a manager) keys and indices Exercise 3 Create tables according to the description: page 5 of 6
Exercise 4 Fill the tables with sample data (each table minimum 10 rows). Identifiers in STAMPS table should refer to existing records in ALBUMS and COUNTRIES. page 6 of 6