|
To enter data in a table, after displaying it in the Datasheet
View, the user can click a box under a column and type the necessary value. After
entering data in a box, the user can press Tab or Enter to move to the next box on
the right. Whenever the user enters a value and moves to the next box, Microsoft
Access automatically saves that value and it becomes part of the record.
When either all horizontal boxes have been covered or the
user simply
decides to move to another horizontal range of boxes, he or she is said to have
created a record. In summary, a record is a series of the same horizontal boxes.
A record is actually represented as a row. The intersection of a column and a
row is called a cell. This means that data is actually entered into cells.
By default, if you generate a form based on a table
and intended for data entry, all records would be accessible. When it's
time to add a new record, the user must move from one record to an empty
one. An alternative is to create a form specially made for data entry.
This type of form comes up with empty controls and expects the user to
simply enter the new values.
To create a form for direct data entry, you have many
alternatives. One of the techniques you can use to create a data
entry-only form is as follows:
- Generate a form based on the table that holds the information
- Set its Navigation Buttons property to No and its Data Entry
property to Yes
You can also allow the user to open a form for data
entry, that is, to a new record. To do this, you
can call the GoToRecord() method of the DoCmd object. The
syntax of this method is:
GoToRecord(ObjectType, ObjectName, Record, Offset)
The first argument to this method must be a constant
value. In this case, it would be acDataForm. If you are calling it
to act on the current form, you can set this argument to acActiveDataObject.
In this case, you can omit this argument.
The second argument is the name of the form to which
the new record will be added. If the record is being added to the same
form that is making the call, you can omit this argument.
The third argument specifies the action that would be
performed. This argument holds a constant value. In the case of adding a
new record, the value of this argument would be acNewRec.
The last argument has to do with other values of the
third argument.
Here is an example that opens a form named Customers
at a new record:
Private Sub cmdAddCustomer_Click()
DoCmd.OpenForm "Customers1"
DoCmd.GoToRecord acDataForm, "Customers", acNewRec
End Sub
Instead of writing this code, you can use the Command
Button Wizard where you would select Record Operations followed by Add New
Record.
|
Practical
Learning: Creating a Data Entry Form
|
|
- Open the GCS2 database you created in the previous lesson
- In the Forms section of the Database window, right-click
CleaningOrders and click Save As...
- In the Save Form To text box, change the name to NewCleaningOrder
and click OK
- Right-click NewCleaningOrder and click Design View
- Double-click the button at the intersection of both rulers the
access the Properties window for the form and, in the All tab, change
the properties as follows:
Caption: Georgetown Cleaning Services - New Cleaning Order
Data Entry: Yes
Navigation Buttons: No
- Switch the form to Form View

- Save the form
- Click Customer Phone
- Type 1026882250 and press Tab
- In the Customer Name, type Telanie Marba
- Click the arrow of the Date Left and select a data that corresponds
to June 12, 2002
- Set the Time Left to 10:15 AM
- Set the Date Expected by adding 1 day to the date left
- Set the Time Expected to 8AM
- Click Tax Rate and type 0.0575
- Complete the cleaning order with the following values:
| Item |
Unit Price |
Qty |
| Shirts |
1.75 |
4 |
| Pants |
2.65 |
2 |
| Jacket |
3.25 |
1 |
| Tie |
2.25 |
5 |
- Close the form
With SQL, 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)
The TableName factor must be a valid name
of an existing table in the currently selected 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 indicates that you are
ready to list the values of the columns. The values of the columns
must be included in parentheses. Specify the value of each column in
the parentheses that follow the VALUES keyword.
If the column is Boolean-based, you
must specify its value as 0 or 1.
If the column is a numeric type, you should pay
attention to the number you type. If the number is an integer, you
should provide a valid natural number without the decimal separator.
If the column is for a decimal number, you can type the value with
its character separator (the period for US English).
|
|
If the data type of a column is a string type,
you should 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 if you are using ADO. For example, a
shelf number can be specified as "HHR-604" for DoCmd.RunSQL()
or 'HHR-604' for ADO and a middle initial can
be given as "D" for Microsoft Access or 'D' for ADO.
If the column was created for a date or a time data
type, you should/must use an appropriate formula with the year
represented by 2 or 4 digits. You should also include the date in
single-quotes. If you want to specify the year with 2 digits, use
the formula:
'yy-mm-dd'
Or
'yy/mm/dd'
You can use the dash symbol "-" or the
forward slash "/" as the date separator. The year, the
month, and the day can each be specified with a single digit. When
the year is specified with 1 digit, its number is added to the
current decade. For example, a year with 6 is
represented as 2006. The 1-year digit formula is suitable for a date
that occurs in the current decade. As you may guess, it is better to represent a date with at least two
digits, including a leading 0. The 2-year digit formula is suitable
for a date that occurs in the current century.
An alternative to representing a year is with 4
digits. In this case, you would use the formulas:
'yyyy-mm-dd'
Or
'yyyy/mm/dd'
The year with 4 digits is more precise as it
properly expresses a complete year.
A month from January to September can be
represented as 1, 2, 3, 4, 5, 6, 7, 8, or 9. Day numbers follow the
same logic.
|
Practical
Learning: Introducing SQL Data Entry
|
|
- To create a new database, on the main menu, click File -> New...
-
In the New dialog box, click Database and click OK
- Change the name of the database to College Park Auto Shop1 and change the
folder in the Save In combo box to the folder that contains your exercises
- Click Create
- To create a new table, on the main menu, click Insert -> Table
- In the New Table dialog box, click Table Wizard and click OK
- In the Sample Tables, click Orders
- In the Sample Fields, double-click OrderID and click Rename Field
- Type RepairOrderID and press Enter
- Click Next and change the Name to RepairOrders
- Click Next
- Click the Modify The Table Design radio button and click Finish
- Complete the table with the following fields (Don't change the properties
that are not specified):
| Field Name |
Data Type |
Caption |
| RepairOrderID |
|
Receipt Number |
| CustomerName |
|
Name |
| CustomerAddress |
|
Address |
| CustomerCity |
|
City |
| CustomerState |
|
State |
| CustomerZIPCode |
|
ZIP Code |
| CarMakeModel |
|
Make/Model |
| CarYear |
|
Year |
| ProblemDescription |
Memo |
|
| Part1Name |
|
|
| Part1UnitPrice |
Number |
|
| Part1Quantity |
Number |
|
| Part1SubTotal |
Number |
|
| Part2Name |
|
|
| Part2UnitPrice |
Number |
|
| Part2Quantity |
Number |
|
| Part2SubTotal |
Number |
|
| Part3Name |
|
|
| Part3UnitPrice |
Number |
|
| Part3Quantity |
Number |
|
| Part3SubTotal |
Number |
|
| Part4Name |
|
|
| Part4UnitPrice |
Number |
|
| Part4Quantity |
Number |
|
| Part4SubTotal |
Number |
|
| Part5Name |
|
|
| Part5UnitPrice |
Number |
|
| Part5Quantity |
Number |
|
| Part5SubTotal |
Number |
|
| JobPerformed1 |
|
|
| JobPrice1 |
Number |
|
| JobPerformed2 |
|
|
| JobPrice2 |
Number |
|
| JobPerformed3 |
|
|
| JobPrice3 |
Number |
|
| JobPerformed4 |
|
|
| JobPrice4 |
Number |
|
| JobPerformed5 |
|
|
| JobPrice5 |
Number |
|
| TotalParts |
Number |
Total Parts |
| TotalLabor |
Number |
Total Labor |
| TaxRate |
Number |
Tax Rate |
| TaxAmount |
Number |
Tax Amount |
| RepairTotal |
Number |
Repair Total |
| RepairDate |
Date/Time |
Repair Date |
| TimeReady |
Date/Time |
Time Ready |
| Recommendations |
Memo |
|
- Save and close the table
- While the table is still selected in the Database window, on the Database
toolbar, click New Object: AutoForm
- Save the form as RepairOrders
- Right-click it and click Form Header/Footer
- On the Toolbox, make sure the Control Wizard button is down. Click Command
Button and click the bottom right section of the Form Footer section
- Follow the section to create a button that would be used to close the
form. Set the button's caption to Close and its name to cmdClose
- Change its design as follows:

- Save and close the form
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 subsequent list 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 numeric field, you should type 0 as its value. For a
string field whose data you don't have and cannot provide, 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
Here are two examples of creating a record with
two date values:
Private Sub cmdTable_Click()
DoCmd.RunSQL "CREATE Table Employees (" & _
"DateHired Date, " & _
"DateModified Date);"
End Sub
Private Sub cmdEnterData_Click()
DoCmd.RunSQL "INSERT INTO Employees " & _
"VALUES(""02/08/2004"", ""16-Aug-05"");"
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 cmdTable_Click()
DoCmd.RunSQL "CREATE Table Employees (" & _
"DateHired Date, " & _
"FirstName Varchar(20), " & _
"MI Char(1), " & _
"LastName Varchar(20));"
End Sub
Private Sub cmdEnterData_Click()
DoCmd.RunSQL "INSERT INTO Employees (" & _
"DateHired, FirstName, MI, LastName) " & _
"VALUES(#02/08/2004#, ""Walter"", ""G"", ""Theal"");"
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 cmdTable_Click()
DoCmd.RunSQL "CREATE Table Employees (" & _
"DateHired Date, " & _
"FirstName Varchar(20), " & _
"MI Char(1), " & _
"LastName Varchar(20));"
End Sub
Private Sub cmdEnterData_Click()
DoCmd.RunSQL "INSERT INTO Employees (" & _
"LastName, DateHired, MI, FirstName) " & _
"VALUES(""Theal"", #02/08/2004#, ""G"", ""Walter"");"
End Sub
Notice that, during data entry, the columns are
provided in an order different than that in which they were created.
|
Practical
Learning: Creating a Table
|
|
|