We have introduced a database as an application made of one or more lists. We also mentioned that, to make a list easy to view and explore, its items should be organized in categories. Here is the example we used:
In a list like this one, each category of information is called a column. In reality, the idea of a column is based on the fact that the categories are organized vertically. This, of course, makes it possible to know that under a column, the information is of the same type. When it comes to types, a database can also be configured so that each column is made for a particular type of data and some types of values would be excluded. Fortunately, Microsoft Access provides all the tools you need to create, configure, and maintain a column with the maximum flexibility. The columns of a table are used to organize data and they are appropriate for table design. When the users start performing data entry, some of them may not find tables user-friendly. An alternative is to create forms that would produce the same results as if working on a table.
As you may guess, a column must be part of a table. In fact, a table without a column is non-existent and a column must belong to a table. There are various techniques you can use to create a column, depending on how you started the table. Probably the easiest technique consists of using an existing column provided by Microsoft Access. This would be a column that has already been named and configured. To get such a column, while the table is display in Datasheet View, in the Fields & Columns section of the Ribbon, click the New Field button . This would display a list of fields you can choose from: To use one of these fields, drag it to a table in Datasheet View: The Design View of a table allows you to provide as much detail as possible about a column you are creating. The Datasheet View is primarily made for data entry but it still allows you to create columns. After creating the columns of a table, you can either generate a corresponding form or report, or you can design from scratch.
Like every object of the computer or of a database, the primary attribute of a column or a placeholder is its name. As mentioned for a table, Microsoft Access is very flexible with the names. On a table:
Names of columns such as @90T or Kw_3%3 can be confusing. Also, you are more likely to involve the names of columns in various expressions. Such expressions may not work with bizarre names. Based on this, we will adopt the same types of naming conventions we reviewed for tables:
If you are working in the Datasheet View of a table and if you use one of the columns from the Field Templates window, each column you select would already have a name. If you are working in the Design View of a table, to set the name of a column, click a new cell under the Field Name column, type the desired name and press Enter or Tab:
When showing a table in the Datasheet View, each column displays a string in its top section to indicate what it is used for. This string is referred to the column's caption. When creating a column in the Design View of a table, if you specify only the column name and switch it to Datasheet view, the column would use its name to display the caption. This means that the caption may appear in one word. If you want to display a friendlier caption, in the Design View of the table, after specifying a column's name, in the lower section of the window, enter the desired string in the Caption field. There is no significant relationship between the column's name and its caption. You can type anything you want in the caption but you should give it a string that resembles the name of the column. For example, if a column is named FirstName, its caption should be First Name.
We saw how to start a programmatic creation of a table in the Microsoft Access Object Library as follows: Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students") . . . ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub To support the creation of a column using the Microsoft Access Object Library, its table is equipped with a method named CreateField. This method takes three arguments and its syntax is: Set field = object.CreateField(ByValye ColumnName As String, _ ByVal DataType As FieldType, _ ByVal Size As Integer) Before calling this method, declare a variable of type Object for the new column. When calling this method, get a reference to it and assign it to the variable you would have created for the column. This would be done as follows: Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object Dim fldFullName As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students") Set fldFullName = tblStudents.CreateField(ColumnName, DataType, Size) ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub None of the arguments to the CreateField() method is required. The first argument of the CreateField() method, name, soecifies the name of the new column. The name follows the rules we saw for names of columns. Here is an example: Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object Dim fldFullName As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students") Set fldFullName = tblStudents.CreateField("colFullName", DataType, Size) ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub If you call the method without specifying the name of the column, at one time or another before actually creating the field, you will have to set its name. Otherwise you will receive an error. to support this, the field object has a property named Name. Therefore, to specify the name of the column, access its Name property and assign the desired string. Here is an example: Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object Dim fldFullName As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students1") Set fldFullName = tblStudents.CreateField fldFullName.Name = "FullName" ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub The second argument to the CreateField() method specifies the data type that would be applied on the column. We will review data types in other sections. Once again, the argument is optional. If you omit it when calling the method, you must specify it before actually creating the table. To support this, the field class is equipped with a property named Type. Therefore, to specify the data type of a column, assign the desired type to its Type property. The third argument to the CreateField() method holds a piece of information that has to do with either the computer memory or the number of characters. After creating a column, you must add it to the table. To support this, the table object is equipped with a property named Fields, which is a collection. We saw that the collections classes are equipped with a method named Add. The Fields collection of the table is equipped with an equivalent method but named Append method. This method takes as argument an object that represents the collection. Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object Dim fldFullName As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students") Set fldFullName = tblStudents.CreateField("colFullName", DataType, Size) tblStudents.Fields.Append fldFullName ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub
To support fields of a table, the DAO class is equipped with a property named Field, which represents a class of the same name. Therefore, before creating a column in the Microsoft DAO Object Library, declare a variable of type DAO.Field. Here are examples: Private Sub cmdCreateTable_Click() Dim fldEmployeeNumber As DAO.Field Dim fldFirstName As DAO.Field Dim fldLastName As DAO.Field End Sub Before creating the column(s), first initialize a table as we saw in the previous lesson: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef ' Open the database Set dbExercise = DBEngine.OpenDatabase("C:\My Documents\Exercise.accdb") ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") . . . End Sub After initializing the table, you can add the desired columns to it. To create a column, you can call the CreateField() method of the TableDef object and assign it to the column variable. The formula to follow is: Set fldEmployeeNumber = tblEmployees.CreateField(ByVal ColumnName As String, _ ByVal DataType As FieldType, _ ByVal FieldSize As Integer) The arguments of this method follow exactly the same descriptions we reviewed for the Microsoft Access Object Library. After creating the column, you can add it to the table. To do this, you can pass it to the Append() method of the Fields collection of the TableDef class. This would be done as follows: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef Dim fldEmployeeNumber As DAO.Field ' Specify the database to use Set dbExercise = DBEngine.OpenDatabase("C:\My Documents\Exercise.accdb") ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") Set fldEmployeeNumber = tblEmployees.CreateField(ColumnName, DataType, FieldSize) tblEmployees.Fields.Append fldEmployeeNumber ' Add the new table to the database. dbExercise.TableDefs.Append tblEmployees dbExercise.Close End Sub To specify the name of the new column, when calling the DAO.Database.CreateTableDef() method, pass the first argument as a string. Here is an example: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef Dim fldEmployeeNumber As DAO.Field ' Specify the database to use Set dbExercise = DBEngine.OpenDatabase("C:\My Documents\Exercise.accdb") ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") Set fldEmployeeNumber = _ tblEmployees.CreateField("EmployeeNumber", DataType, FieldSize) tblEmployees.Fields.Append fldEmployeeNumber ' Add the new table to the database. dbExercise.TableDefs.Append tblEmployees dbExercise.Close End Sub
As mentioned already, to work in SQL, you can use the DoCmd class. To get a DoCmd object, you can access it as a property of the Application that is readily available whenever you start Microsoft Access. To create a column in the SQL, specify its name, followed by its data type, and some possible options. In the parentheses of the CREATE TABLE TableName() expression, the formula of creating a column is: ColumnName DataType Options Notice that there is only space that separates the sections of the formula. This formula is for creating one column. If you want the table to have more than one column, follow this formula as many times as possible but separate them with commas. This would be done as follows: CREATE TABLE TableName(ColumnName DataType Options, ColumnName DataType Options) When creating a column, the first information you must provide is its name. Here is an example that starts a table with a column named Private Sub cmdCreateTable_Click() DoCmd.RunSQL "CREATE TABLE Customers(FirstName . . .);" End Sub
To programmatically add a new column, whether using the Microsoft Access Object Library or DAO, first declare an Object (Microsoft Access Object Library) or a Field (DAO) variable. After getting a reference to the table that will receive the new column, assign the CreateField() method of the table to the column's variable. Finally, call the Append() method of the Fields collection of the table and pass it the column variable. Here is an example: Private Sub cmdAddColumn_Click() Dim curDatabase As Object Dim tblStudents As Object Dim colFullName As Object ' Get a reference to the current database Set curDatabase = CurrentDb ' Get a reference to a table named Customers Set tblStudents = curDatabase.TableDefs("Students") Set colFullName = tblCustomers.CreateField("FullName", DB_TEXT) tblCustomers.Fields.Append colFullName End Sub
If you are working in SQL, to perform maintenance on a column, you can start with an ALTER TABLE expression as follows: ALTER TABLE TableName ... The TableName factor must specify the table on which the maintenance will be performed. After the table name, you can then issue the desired command. If you are using SQL, to add a new column, in an ALTER TABLE statement, include an ADD COLUMN expression using the following formula: ALTER TABLE TableName ADD COLUMN ColumnName DataType The ColumnName factor must be a valid name for the new column and you must follow the rules for naming columns. The data type must be one of those we reviewed. Here is an example that adds a new string-based column named CellPhone to a table named Contractors: Private Sub cmdDeleteColumn_Click() DoCmd.RunSQL "ALTER TABLE Contractors ADD COLUMN CellPhone TEXT;" End Sub
Column maintenance consists of renaming, copying, moving, or deleting a column. This operation is performed differently, of course, depending on the means you are using. You can work visually in the Table Wizard, in the Datasheet View, or in the Design View of a table. If you are programmatically maintaining a column, because a column must belong to a table, before performing any operation on it, you must first obtain a reference to the table. We saw how to do this for a Microsoft Access Object Library or for DAO. Here is an example for DAO: Private Sub cmdModifyPersons_Click() Dim curDatabase As DAO.Database Dim tblPersons As DAO.TableDef ' Get a reference to the current database Set curDatabase = CurrentDb ' Get a reference to a table named Persons Set tblPersons = curDatabase.TableDefs("Persons") End Sub If you are working in SQL, to perform maintenance on a column, you can start with an ALTER TABLE expression as follows: ALTER TABLE TableName ... The TableName factor must specify the table on which the maintenance will be performed. After the table name, you can then issue the desired command.
If you have a column you don't need anymore on a table, you can remove that column. To programmatically delete a column, if you are using either the Microsoft Access Object Library or DAO, call the Delete() method of the TableDef object and pass it the name of the column. The syntax of this method is: TableDef.Fields.Delete ColumnName In this formula, replace ColumnName with the name of the column you want to delete. Here is an example: Private Sub cmdModifyPersons_Click() Dim curDatabase As DAO.Database Dim tblPersons As DAO.TableDef ' Get a reference to the current database Set curDatabase = CurrentDb ' Get a reference to a table named Customers Set tblPersons = curDatabase.TableDefs("Persons") tblPersons.Fields.Delete "DateHired" End Sub Before deleting a column, make sure it exists, otherwise, you would receive a 3265 error: Even if the column exists, before deleting a column, make sure its table is closed. Otherwise, you would receive a 3211 error. You can check these issues using error handling.
If you are using SQL, to delete a column, after the ALTER TABLE TableName expression, follow it with a DROP COLUMN expression as in this formula: ALTER TABLE TableName DROP COLUMN ColumnName; Replace the name of the undesired column with the ColumnName factor of our formula. Here is an example: Private Sub cmdAlterPersons_Click() DoCmd.RunSQL "ALTER TABLE Persons DROP COLUMN FullName" End Sub
In the database environments, a column is called a field. In the various libraries used in Microsoft Access, a column is an object of type Field. The columns of a table are stored in a collection called Fields. To give access to this collection, in the Microsoft Access Object Library or in DAO, the TableDef object is equipped with a property named Fields that is of type Fields and each one of its items is an object of type Field.
After creating the columns of a table, you may want to know the number of columns that a table has. To give you this information, the Fields collection is equipped with a property named Count. To identify each column of a table, the Fields collection is equipped with a property named Item. This type of property is also referred to as indexed because it takes an argument that identifies the particular member that you want to access in the collection. To access a column, you can pass its name or its index to the Item() indexed property. If you know the name of the column, you can pass it as a string. Here is an example: Fields.Item("[Last Name]") Item is the default property of a Fields collection. Therefore, you can omit it. Based on this, we can also write: Fields("[Last Name]") If you don't know the name of a column or you prefer to access it by its index, you can pass that index to the Item property. Remember that the index starts at 0, followed by 1, and so on. Based on this, to access the third column of a table, you would use either of these two: Fields.Item(2) Fields(2)
After creating a table and filling it up with some values, you can explore them. One way you can do this consists of isolating records based on specific conditions. This technique of isolating records is also referred to as filtering. To filter records of a table and display the results to the user, you have various alternatives. Data filtering is performed using the SQL and other means provided by Microsoft Access. To filter data, you can either create a query or write a SQL statement.
To create a query in Microsoft Access, on the Ribbon, you can click Create. In the Other section, you would click Query Wizard or Query Design. To programmatically create a query using either the Microsoft Access Object Library or DAO, you can use the CreateQueryDef() method of the Database object. The syntax of this method is: CreateQueryDef(NewQueryName, SQLStatement) The first argument is the name you will give to the new query. The second argument is a SQL statement that specifies the contents of the query. After creating a query and saving it, you can use it as the source of data for a form. To do this, you can visually specify it in the Record Source property of the form. You can also assign the name of a query, as a string, to the form's RecordSource property. Here is an example: Private Sub cmdSetRecordSource_Click() Me.RecordSource = "ListOfEmployees" End Sub Instead of first creating a query, you can access the Record Source property of the form and click its ellipsis button. This would prompt you to visually create a query. After creating it, you can close the query window. Microsoft Access would create the necessary SQL statement for you and assign it to the Record Source property of the form. Instead of visually creating a query, you can write a SQL statement that selects the records and assign that statement to the RecordSource property of the form in code.
Once a query has been created and saved, it becomes a database object like the others we have used so far. If a query exists already, to use it, the user can open it like a table. To programmatically open a query, you can call the OpenQuery() method of the DoCmd object. This method takes one string argument as the name of the query. Here is an example: Private Sub cmdOpenVideoTitles_Click() DoCmd.OpenQuery "VideoTitles" End Sub
After using a query, the user can close it like a regular window by clicking its system Close button. To programmatically close a query, you can call the Close() method of the DoCmd object, passing the first argument as acQuery and the second argument as the name of the query. Here is an example: Private Sub cmdCloseVideoTitles_Click() DoCmd.Close acQuery, "VideoTitles" End Sub When this method is called, it checks whether the query is opened. If a query with that name is opened, it would be closed. If no query with that name is opened, nothing would happen. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|