Before performing data entry on a table, you must know how the table is structured, the sequence of its columns, the type of data that each column is made of: it is certainly undesirable to have a numeric value as somebody's first name. Before performing data entry, you must make sure that the table exists. Otherwise, you would receive a 3192 error:
To enter data in a table, you start with the INSERT combined with the VALUES keywords. The statement uses the following syntax: INSERT TableName VALUES(Column1, Column2, Column_n) Alternatively, or to be more precise, you can specify that you are entering data in the table using the INTO keyword between the INSERT keyword and the TableName factor. This is done with the following syntax: INSERT INTO TableName VALUES(Column1, Column2, Column_n) TableName must be a valid name of an existing table in the current database. If the name is wrong, the SQL interpreter would simply consider that the table you are referring to doesn't exist. Consequently, you would receive an error. The VALUES keyword announces the values for the columns. The values of the columns must be included in parentheses. If the data type of a column is a string type, include its value between double-quotes if you are using the DoCmd.RunSQL() method of Microsoft Access or you should include it in single-quotes.
The most common technique of performing data entry requires that you know the sequence of columns of the table in which you want to enter data. With this sequence in mind, enter the value of each field in its correct position. During data entry on adjacent fields, if you don't have a value for a string field, type two double-quotes to specify an empty field. Imagine you have a table equipped with two string columns. Here is an example that creates a record made of two strings: Private Sub cmdEnterData_Click() DoCmd.RunSQL "INSERT INTO Employees VALUES(""Jimmy"", ""Collen"");" End Sub
The adjacent data entry requires that you know the position of each column. The SQL provides an alternative that allows you to perform data entry using the name of a column instead of its position. This allows you to provide the values of fields in any order of your choice. To perform data entry at random, you must provide a list of the columns of the table in the order of your choice. You can either use all columns or provide a list of the same columns but in your own order. Here is an example: Private Sub cmdCreateTable_Click() DoCmd.RunSQL "CREATE Table Employees (" & _ "FirstName Text, " & _ "LastName Text, " & _ "EmailAddress Varchar, " & _ "HomePhone Char);" End Sub Private Sub cmdCreateNewRecord_Click() DoCmd.RunSQL "INSERT INTO Employees (" & _ "FirstName, LastName, EmailAddress, HomePhone) " & _ "VALUES(""Gertrude"", ""Monay"", " & _ " ""gmonay@ynb.com"", ""(104) 972-0416"");" End Sub You don't have to provide data for all columns, just those you want, in the order you want. To do this, enter the names of the desired columns on the right side of the name of the table, in parentheses. The syntax used would be: INSERT TableName(ColumnName1, Columnname2, ColumnName_n) VALUES(ValueFormColumnName1, ValueFormColumnName2, ValueFormColumnName_n); Here is an example: Private Sub cmdCreateTable_Click() DoCmd.RunSQL "CREATE Table Employees (" & _ "FirstName Text, " & _ "LastName Text, " & _ "EmailAddress Varchar, " & _ "HomePhone Char);" End Sub Private Sub cmdCreateNewRecord_Click() DoCmd.RunSQL "INSERT INTO Employees (" & _ "LastName, EmailAddress, FirstName) " & _ "VALUES(""Mukoko"", ""hmukoko@ynb.com"", " & _ " ""Helene"");" End Sub Notice that, during data entry, the columns are provided in an order different than that in which they were created.
When creating a text-based field, you have the option of controlling the number of characters or symbols that the field can hold. We saw that, in SQL, a text-based field can use the TEXT, CHAR, or VARCHAR data type. By default, when a column of a table has been set to TEXT, CHAR, or VARCHAR, it is made to hold 255 characters. To specify the number of characters of the string-based column, add an opening and a closing parentheses to the TEXT, the CHAR, or the VARCHAR data types. In the parentheses, enter the desired number. Here are examples: Private Sub cmdCreateTable_Click() DoCmd.RunSQL "CREATE TABLE Employees (" & _ "EmplNumber TEXT(6)," & _ "FirstName Text(20)," & _ "LastName Text(20)," & _ "Address varchar(100)," & _ "City VARCHAR(40)," & _ "State char(2));" End Sub
When performing data entry, you can expect the user to skip any column whose value is not available and move to the next. In some cases, you may want to require that the value of a column be specified before the user can move on. Such a field is referred to as required. If you are creating the table in the Design View, to require that the user enter a value for a particular column, in the lower section of the window, use the Required Boolean property. If you are programmatically creating the column using SQL, if you want to let the user add or not add a value for the column, type the NULL keyword on the right side of the data type. If you want to require a value for the column, type NOT NULL. Here are examples: Private Sub cmdTable_Click() DoCmd.RunSQL "CREATE TABLE Contractors(" & _ "FirstName TEXT NULL, " & _ "LastName VARCHAR NOT NULL);" End Sub In this case, when performing data entry, the user must always provide a value for the LastName column in order to create a record. If you omit to specify the nullity of a field, it is assumed NULL.
One of the primary concerns of records is their uniqueness. In a professional database, you usually want to make sure that each record on a table can be uniquely identified. There are various ways you can create a unique value, including applying the primary key to a field. To let you create a column whose values are unique, the SQL provides the UNIQUE keyword. To apply it on a column, after the data type, type UNIQUE. Here is an example: CREATE TABLE Students ( StudentNumber int UNIQUE, FirstName varchar(24), LastName varchar(24) NOT NULL ); When a column has been marked as unique, during data entry, the user must provide a unique value for each new record created. If an existing value is assigned to the column, this would produce an error: Private Sub cmdCreateTable_Click() DoCmd.RunSQL "CREATE TABLE Students" & _ "(" & _ " StudentNumber int UNIQUE," & _ " FirstName varchar(24)," & _ " LastName varchar(24)" & _ ");" DoCmd.RunSQL "INSERT INTO Students VALUES(24880, 'John', 'Scheels');" DoCmd.RunSQL "INSERT INTO Students VALUES(92846, 'Rénée', 'Almonds');" DoCmd.RunSQL "INSERT INTO Students VALUES(47196, 'Peter', 'Sansen');" DoCmd.RunSQL "INSERT INTO Students VALUES(92846, 'Daly', 'Camara');" DoCmd.RunSQL "INSERT INTO Students VALUES(36904, 'Peter', 'Sansen');" End Sub When the fourth record is entered, since it uses a student number that exists already, the database engine would produce an error:
The text data types we have above can hold a maximum of 255 characters. Of course, sometimes you will want to have a column that can hold longer text. If you want to create a field that uses longer text, in Microsoft Access, display the table in Design View and set its Data Type to Memo. Like the Text data type, the Memo type is used for any type of text, any combination of characters, and symbols, up to 64000 characters. If you are using SQL to create your table, you can apply the MEMO, the NOTE, or the LONGTEXT data types to a column that would hold long text.
The binary data type can let a column accept any type of data but it is equipped to interpret the value. For example, it can be used to receive hexadecimal numbers. To specify this when programmatically creating a column using SQL, set its data type to one of the following: IMAGE, OLEOBJECT, LONGBINARY, or GENERAL. |
|
|||||||||||||||||||||||||
|