Details on Data Entry |
|
Details on Record Creation |
The Nullity of a Field |
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 required that the value of a column be specified before the user can move on. 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. By default, the value of the Required property is set to No, which means the user doesn't have to provide a value for the column in order to create the record. If you want to require the value, set this property to Yes. |
In this case, when performing data entry, the user must always provide a value for the FullName column in order to create a record. If you omit to specify the nullity of a field, it is assumed NULL.
Here is an example: Private Sub cmdTable_Click() DoCmd.RunSQL "CREATE TABLE Contractors(" & _ "ContractorNo COUNTER, " & _ "AvailableOnWeekend LOGICAL NULL, " & _ "OwnsACar BIT, " & _ "CanShareOwnCar YESNO);" End Sub By default, when you apply the COUNTER or the AUTOINCREMENT data type, when the user creates the first record, the field under the auto-incrementing value receives a number of 1. If the user creates a second record, the auto-incrementing value receives a number of 2, and so on. If you want, you can make the first record receive a number other than 1. You can also make it increment to a value other than 1. To apply this feature, the COUNTER and the AUTOINCREMENT types use a seed as their parentheses: COUNTER(x,y) or AUTOINCREMENT(x,y). The x value represents the starting value of the records. The y value specifies how much would be added to a value to get the next. Here is an example: Private Sub cmdTable_Click() DoCmd.RunSQL "CREATE TABLE Contractors(" & _ "ContractorNo AUTOINCREMENT(5, 10), " & _ "AvailableOnWeekend LOGICAL NULL, " & _ "OwnsACar BIT, " & _ "CanShareOwnCar YESNO);" End Sub
When reviewing the data types available for fields, we saw that some of them could use a string-based data type, namely TEXT, CHAR, or VARCHAR. By default, in the Design View of a table and when creating a column, if you set its data type to Text, it can hold (only) 50 characters. If you programmatically create a table and you set a column's data type to TEXT, CHAR, or VARCHAR, it is made to hold 255 characters. Fortunately, you can control the maximum number of characters that would be allowed in a column during data entry. If you are creating a table in the Design View, to control the number of characters that a user can enter under a column, after setting its Data Type to Text, in the lower section of the window, use the Field Size property to exercise this control. You can set a value from 1 to 255. If you are programmatically creating a string-based column using either the Microsoft Access Object Library or DAO, to specify the maximum number of characters it can hold, provide a third argument to the CreateField() method and enter the desired number. Here is an example for a column created using the Microsoft Access Object library: 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, 60) tblCustomers.Fields.Append colFullName End Sub If you are creating the table using SQL, to specify the number of characters of the string-based column, add the parentheses to the TEXT, the CHAR, or the VARCHAR data types, and in the parentheses, enter the desired number. Here are examples: Private Sub cmdTableCreator_Click() Dim conExercise As ADODB.Connection Dim strSQL As String Set conExercise = New ADODB.Connection conExercise.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\Programs\Exercise1.mdb'" strSQL = "CREATE TABLE Contractors (" & _ "DateHired DateTime," & _ "EmplNumber Text(6)," & _ "Dept int," & _ "FirstName VarChar(20)," & _ "LastName varchar(20)," & _ "Address varchar(50)," & _ "City VARCHAR(40)," & _ "State CHAR(2));" conExercise.Execute strSQL conExercise.Close Set conExercise = Nothing End Sub |
Practical Learning: Setting Field Sizes |
Field Name | Data Type | Field Size | Format |
CustomerName | 80 | ||
CustomerAddress | 80 | ||
CustomerCity | 50 | ||
CustomerState | 2 | ||
CustomerZIPCode | 20 | ||
CarMakeModel | 40 | ||
CarYear | 8 | ||
Part1Name | 60 | ||
Part1UnitPrice | Number | Double | Fixed |
Part1Quantity | Number | Byte | |
Part1SubTotal | Number | Double | Fixed |
Part2Name | 60 | ||
Part2UnitPrice | Number | Double | Fixed |
Part2Quantity | Number | Byte | |
Part2SubTotal | Number | Double | Fixed |
Part3Name | 60 | ||
Part3UnitPrice | Number | Double | Fixed |
Part3Quantity | Number | Byte | |
Part3SubTotal | Number | Double | Fixed |
Part4Name | 60 | ||
Part4UnitPrice | Number | Double | Fixed |
Part4Quantity | Number | Byte | |
Part4SubTotal | Number | Double | Fixed |
Part5Name | 60 | ||
Part5UnitPrice | Number | Double | Fixed |
Part5Quantity | Number | Byte | |
Part5SubTotal | Number | Double | Fixed |
JobPerformed1 | 60 | ||
JobPrice1 | Number | Double | Fixed |
JobPerformed2 | 60 | ||
JobPrice2 | Number | Double | Fixed |
JobPerformed3 | 60 | ||
JobPrice3 | Number | Double | Fixed |
JobPerformed4 | 60 | ||
JobPrice4 | Number | Double | Fixed |
JobPerformed5 | 60 | ||
JobPrice5 | Number | Double | Fixed |
TotalParts | Number | Double | Fixed |
TotalLabor | Number | Double | Fixed |
TaxRate | Number | Double | Percent |
TaxAmount | Number | Double | Fixed |
RepairTotal | Number | Double | Fixed |
RepairDate | Date/Time | Medium Date | |
TimeReady | Date/Time | Medium Time |
Masking a Value |
A mask is a technique of specifying how a value would display to the user. This depends on the type of value, the language settings in Control Panel, and probably the users' habits. The mask used by the values of a column is set in the Input Mask field in the lower section of the table or the Properties window of a form, in Design View. |
Practical Learning: Masking the Fields |
|
Default Values |
Introduction |
A default value allows a column to use a value that is supposed to be common to most cells of a particular column. The default value can be set as a constant value or it can use a function that would adapt to the time the value is needed. |
Practical Learning: Setting Default Values |
|
Default Values of Expressions |
When it comes to setting default values, the table has various limitations. For example, if you create a table with three columns such as FirstName, LastName, and FullName, obviously you may want the FullName to combine the first name and the last name but still give the user the opportunity to change the result. If you write an expression to do this in the Default Value property of the FullName column, you would not get the intended result. To specify a default value in a scenario like this one, you should use the form or report that will show the values. Fortunately, when the values are changed in the form, the table would also be updated. To specify the default value of a column using a form, you can write an expression that would be applied when necessary. |
Practical Learning: Using Expressions to Set Default Values |
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|