![]() |
The Records of a Database: SQL and Data Entry |
|
Introduction |
|
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.
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.
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. |
|
|
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Private Sub CalculateOrder()
On Error GoTo CalculateOrder_Error
Dim dblPart1UnitPrice As Double, bytPart1Quantity As Byte
Dim dblPart2UnitPrice As Double, bytPart2Quantity As Byte
Dim dblPart3UnitPrice As Double, bytPart3Quantity As Byte
Dim dblPart4UnitPrice As Double, bytPart4Quantity As Byte
Dim dblPart5UnitPrice As Double, bytPart5Quantity As Byte
Dim dblPart1SubTotal As Double, dblPart2SubTotal As Double, _
dblPart3SubTotal As Double, dblPart4SubTotal As Double, _
dblPart5SubTotal As Double
Dim dblJob1Price As Double, dblJob2Price As Double, _
dblJob3Price As Double, dblJob4Price As Double, _
dblJob5Price As Double
Dim curTotalParts As Currency, curTotalLabor As Currency
Dim dblTaxRate As Double, curTaxAmount As Currency
Dim curRepairTotal As Currency
' Retrieve the unit price of each part
dblPart1UnitPrice = CDbl([txtPart1UnitPrice])
dblPart2UnitPrice = CDbl([txtPart2UnitPrice])
dblPart3UnitPrice = CDbl([txtPart3UnitPrice])
dblPart4UnitPrice = CDbl([txtPart4UnitPrice])
dblPart5UnitPrice = CDbl([txtPart5UnitPrice])
' Retrieve the quantity specified for each part
bytPart1Quantity = CByte([txtPart1Quantity])
bytPart2Quantity = CByte([txtPart2Quantity])
bytPart3Quantity = CByte([txtPart3Quantity])
bytPart4Quantity = CByte([txtPart4Quantity])
bytPart5Quantity = CByte([txtPart5Quantity])
' Calculate the sub-total of each part
dblPart1SubTotal = dblPart1UnitPrice * bytPart1Quantity
dblPart2SubTotal = dblPart2UnitPrice * bytPart2Quantity
dblPart3SubTotal = dblPart3UnitPrice * bytPart3Quantity
dblPart4SubTotal = dblPart4UnitPrice * bytPart4Quantity
dblPart5SubTotal = dblPart5UnitPrice * bytPart5Quantity
' Display the sub totals in the corresponding text boxes
[txtPart1SubTotal] = dblPart1SubTotal
[txtPart2SubTotal] = dblPart2SubTotal
[txtPart3SubTotal] = dblPart3SubTotal
[txtPart4SubTotal] = dblPart4SubTotal
[txtPart5SubTotal] = dblPart5SubTotal
' Calculate the total spent on parts
curTotalParts = CCur(dblPart1SubTotal + dblPart2SubTotal + _
dblPart3SubTotal + dblPart4SubTotal + _
dblPart5SubTotal)
' Retrive the labor amount of each job performed
dblJob1Price = CDbl([txtJob1Price])
dblJob2Price = CDbl([txtJob2Price])
dblJob3Price = CDbl([txtJob3Price])
dblJob4Price = CDbl([txtJob4Price])
dblJob5Price = CDbl([txtJob5Price])
' Calculate the total labor for this repair
curTotalLabor = CCur(dblJob1Price + dblJob2Price + dblJob3Price + _
dblJob4Price + dblJob5Price)
' Retrieve the tax rate applied
dblTaxRate = CDbl([txtTaxRate])
' Calculate the tax amount based on the tax rate applied
' on the parts and the labor
curTaxAmount = CLng(curTotalLabor + curTotalParts) * (dblTaxRate * 100) / 100
curRepairTotal = curTotalLabor + curTotalParts + curTaxAmount
' Displays the values in the Order Summary section
[txtTotalParts] = curTotalParts
[txtTotalLabor] = curTotalLabor
[txtTaxAmount] = curTaxAmount
[txtRepairTotal] = curRepairTotal
Exit Sub
' If there was a problem, address it here
CalculateOrder_Error:
' One of the most common problems that may occur here is if the
' user enters an invalid value in a text box. Normally, the database
' engine is equipped to take care of that and warn the user because
' we specified a format for each text box. But just in case...
If Err.Number = 94 Then
MsgBox "Make sure you enter the unit price and the quantity " & _
"of each part used." & vbCrLf & "Please try again!"
End If
End Sub
|
Private Sub txtPart1Quantity_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtPart2Quantity_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtPart3Quantity_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtPart4Quantity_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtPart5Quantity_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtJob1Price_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtJob2Price_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtJob3Price_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtJob4Price_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtJob5Price_LostFocus()
CalculateOrder
End Sub
|
Private Sub txtTaxRate_LostFocus()
CalculateOrder
End Sub
|
Private Sub cmdResetOrder_Click()
txtCustomerName = ""
txtCustomerAddress = ""
txtCustomerCity = ""
txtCustomerState = ""
txtCustomerZIPCode = ""
txtCarMakeModel = ""
txtCarYear = ""
txtProblemDescription = ""
txtPart1Name = ""
txtPart1UnitPrice = "0.00"
txtPart1Quantity = "0"
txtPart1SubTotal = "0.00"
txtPart2Name = ""
txtPart2UnitPrice = "0.00"
txtPart2Quantity = "0"
txtPart2SubTotal = "0.00"
txtPart3Name = ""
txtPart3UnitPrice = "0.00"
txtPart3Quantity = "0"
txtPart3SubTotal = "0.00"
txtPart4Name = ""
txtPart4UnitPrice = "0.00"
txtPart4Quantity = "0"
txtPart4SubTotal = "0.00"
txtPart5Name = ""
txtPart5UnitPrice = "0.00"
txtPart5Quantity = "0"
txtPart5SubTotal = "0.00"
txtJobPerformed1 = ""
txtJobPrice1 = "0.00"
txtJobPerformed2 = ""
txtJobPrice2 = "0.00"
txtJobPerformed3 = ""
txtJobPrice3 = "0.00"
txtJobPerformed4 = ""
txtJobPrice4 = "0.00"
txtJobPerformed5 = ""
txtJobPrice5 = "0.00"
txtTotalParts = "0.00"
txtTotalLabor = "0.00"
txtTaxRate = "7.75%"
txtTaxAmount = "0.00"
txtRepairTotal = "0.00"
txtRepairDate = ""
txtTimeReady = ""
txtRecommendations = ""
End Sub
|
Private Sub cmdSubmitRepairOrder_Click()
On Error GoTo cmdSubmitRepairOrder_Error
Dim conCPAS As ADODB.Connection
Dim strCustomerName As String, strCustomerAddress As String
Dim strCustomerCity As String, strCustomerState As String
Dim strCustomerZIPCode As String, strCarMakeModel As String
Dim intCarYear As Integer, strProblemDescription As String
Dim strPart1Name As String, strPart2Name As String, _
strPart3Name As String, strPart4Name As String, _
strPart5Name As String
Dim dblPart1UnitPrice As Double, dblPart2UnitPrice As Double, _
dblPart3UnitPrice As Double, dblPart4UnitPrice As Double, _
dblPart5UnitPrice As Double
Dim intPart1Quantity As Integer, intPart2Quantity As Integer, _
intPart3Quantity As Integer, intPart4Quantity As Integer, _
intPart5Quantity As Integer
Dim dblPart1SubTotal As Double, dblPart2SubTotal As Double, _
dblPart3SubTotal As Double, dblPart4SubTotal As Double, _
dblPart5SubTotal As Double
Dim strJobPerformed1 As String, dblJobPrice1 As Double
Dim strJobPerformed2 As String, dblJobPrice2 As Double
Dim strJobPerformed3 As String, dblJobPrice3 As Double
Dim strJobPerformed4 As String, dblJobPrice4 As Double
Dim strJobPerformed5 As String, dblJobPrice5 As Double
Dim curTotalParts As Currency, curTotalLabor As Currency
Dim dblTaxRate As Double, curTaxAmount As Currency
Dim curRepairTotal As Currency, dteRepairDate As Date
Dim dteTimeReady As Date, strRecommendations As String
If IsNull([txtCustomerName]) Then
MsgBox "Please enter the name of the customer to process an order"
txtCustomerName.SetFocus
Exit Sub
End If
If IsNull([txtCustomerAddress]) Then
strCustomerAddress = ""
Else
strCustomerAddress = [txtCustomerAddress]
End If
If IsNull([txtCustomerCity]) Then
strCustomerCity = ""
Else
strCustomerCity = [txtCustomerCity]
End If
If IsNull([txtCustomerState]) Then
strCustomerState = ""
Else
strCustomerState = [txtCustomerState]
End If
If IsNull([txtCustomerZIPCode]) Then
strCustomerZIPCode = ""
Else
strCustomerZIPCode = [txtCustomerZIPCode]
End If
If IsNull([txtCarMakeModel]) Then
MsgBox "You must specify the make and the model of the car"
txtCarMakeModel.SetFocus
Exit Sub
End If
If IsNull([txtCarYear]) Then
intCarYear = 0
Else
intCarYear = CInt([txtCarYear])
End If
If IsNull(txtProblemDescription) Then
MsgBox "Make sure you describe the problem that needs " & _
"to be fixed on the car"
txtProblemDescription.SetFocus
Exit Sub
End If
If IsNull([txtPart1Name]) Then
strPart1Name = ""
Else
strPart1Name = [txtPart1Name]
End If
If IsNull([txtPart1UnitPrice]) Then
dblPart1UnitPrice = ""
Else
dblPart1UnitPrice = CDbl([txtPart1UnitPrice])
End If
If IsNull([txtPart1Quantity]) Then
intPart1Quantity = 0
Else
intPart1Quantity = CInt([txtPart1Quantity])
End If
If IsNull([txtPart1SubTotal]) Then
dblPart1SubTotal = 0
Else
dblPart1SubTotal = CDbl([txtPart1SubTotal])
End If
If IsNull([txtPart2Name]) Then
strPart2Name = ""
Else
strPart2Name = [txtPart2Name]
End If
If IsNull([txtPart2UnitPrice]) Then
dblPart2UnitPrice = 0
Else
dblPart2UnitPrice = CDbl([txtPart2UnitPrice])
End If
If IsNull([txtPart2Quantity]) Then
intPart2Quantity = 0
Else
intPart2Quantity = CInt([txtPart2Quantity])
End If
If IsNull([txtPart2SubTotal]) Then
dblPart2SubTotal = 0
Else
dblPart2SubTotal = CDbl([txtPart2SubTotal])
End If
If IsNull([txtPart3Name]) Then
strPart3Name = ""
Else
strPart3Name = [txtPart3Name]
End If
If IsNull([txtPart3UnitPrice]) Then
dblPart3UnitPrice = 0
Else
dblPart3UnitPrice = CDbl([txtPart3UnitPrice])
End If
If IsNull([txtPart3Quantity]) Then
intPart3Quantity = 0
Else
intPart3Quantity = CInt([txtPart3Quantity])
End If
If IsNull([txtPart3SubTotal]) Then
dblPart3SubTotal = 0
Else
dblPart3SubTotal = CDbl([txtPart3SubTotal])
End If
If IsNull([txtPart4Name]) Then
strPart4Name = ""
Else
strPart4Name = [txtPart4Name]
End If
If IsNull([txtPart4UnitPrice]) Then
dblPart4UnitPrice = 0
Else
dblPart4UnitPrice = CDbl([txtPart4UnitPrice])
End If
If IsNull([txtPart4Quantity]) Then
intPart4Quantity = 0
Else
intPart4Quantity = CInt([txtPart4Quantity])
End If
If IsNull([txtPart4SubTotal]) Then
dblPart4SubTotal = 0
Else
dblPart4SubTotal = CDbl([txtPart4SubTotal])
End If
If IsNull([txtPart5Name]) Then
strPart5Name = ""
Else
strPart5Name = [txtPart5Name]
End If
If IsNull([txtPart5UnitPrice]) Then
dblPart5UnitPrice = 0
Else
dblPart5UnitPrice = CDbl([txtPart5UnitPrice])
End If
If IsNull([txtPart5Quantity]) Then
intPart5Quantity = 0
Else
intPart5Quantity = CInt([txtPart5Quantity])
End If
If IsNull([txtPart5SubTotal]) Then
dblPart5SubTotal = 0
Else
dblPart5SubTotal = CDbl([txtPart5SubTotal])
End If
If IsNull([txtJobPerformed1]) Then
strJobPerformed1 = ""
Else
strJobPerformed1 = [txtJobPerformed1]
End If
If IsNull([txtJobPrice1]) Then
dblJobPrice1 = 0
Else
dblJobPrice1 = CDbl([txtJobPrice1])
End If
If IsNull([txtJobPerformed2]) Then
strJobPerformed2 = ""
Else
strJobPerformed2 = [txtJobPerformed2]
End If
If IsNull([txtJobPrice2]) Then
dblJobPrice2 = 0
Else
dblJobPrice2 = CDbl([txtJobPrice2])
End If
If IsNull([txtJobPerformed3]) Then
strJobPerformed3 = ""
Else
strJobPerformed3 = [txtJobPerformed3]
End If
If IsNull([txtJobPrice3]) Then
dblJobPrice3 = 0
Else
dblJobPrice3 = CDbl([txtJobPrice3])
End If
If IsNull([txtJobPerformed4]) Then
strJobPerformed4 = ""
Else
strJobPerformed4 = [txtJobPerformed4]
End If
If IsNull([txtJobPrice4]) Then
dblJobPrice4 = 0
Else
dblJobPrice4 = CDbl([txtJobPrice4])
End If
If IsNull([txtJobPerformed5]) Then
strJobPerformed5 = ""
Else
strJobPerformed5 = [txtJobPerformed5]
End If
If IsNull([txtJobPrice5]) Then
dblJobPrice5 = 0
Else
dblJobPrice5 = CDbl([txtJobPrice5])
End If
If IsNull([txtTotalParts]) Then
curTotalParts = 0
Else
curTotalParts = [txtTotalParts]
End If
If IsNull([txtTotalLabor]) Then
curTotalLabor = 0
Else
curTotalLabor = CCur([txtTotalLabor])
End If
If IsNull([txtTaxRate]) Then
dblTaxRate = 0
Else
dblTaxRate = CDbl([txtTaxRate])
End If
If IsNull([txtTaxAmount]) Then
curTaxAmount = 0
Else
curTaxAmount = CCur([txtTaxAmount])
End If
If IsNull([txtRepairTotal]) Then
curRepairTotal = 0
Else
curRepairTotal = CCur([txtRepairTotal])
End If
If IsNull([txtRepairDate]) Then
dteRepairDate = 0
Else
dteRepairDate = CDate([txtRepairDate])
End If
If IsNull([txtTimeReady]) Then
dteTimeReady = 0
Else
dteTimeReady = [txtTimeReady]
End If
If IsNull([txtRecommendations]) Then
strRecommendations = ""
Else
strRecommendations = [txtRecommendations]
End If
' Just in case the order total was not calculated, do it now
CalculateOrder
Dim strInsertInto As String, strValues As String
strInsertInto = "INSERT INTO RepairOrders(CustomerName, CustomerAddress, " & _
"CustomerCity, CustomerState, CustomerZIPCode, CarMakeModel, " & _
"CarYear, ProblemDescription, Part1Name, Part1UnitPrice, " & _
"Part1Quantity, Part1SubTotal, Part2Name, Part2UnitPrice, " & _
"Part2Quantity, Part2SubTotal, Part3Name, Part3UnitPrice, " & _
"Part3Quantity, Part3SubTotal, Part4Name, Part4UnitPrice, " & _
"Part4Quantity, Part4SubTotal, Part5Name, Part5UnitPrice, " & _
"Part5Quantity, Part5SubTotal, JobPerformed1, JobPrice1, " & _
"JobPerformed2, JobPrice2, JobPerformed3, JobPrice3, " & _
"JobPerformed4, JobPrice4, JobPerformed5, JobPrice5, " & _
"TotalParts, TotalLabor, TaxRate, TaxAmount, RepairTotal, " & _
"RepairDate, TimeReady, Recommendations)"
strValues = "VALUES(""" & [txtCustomerName] & " "", """ & strCustomerAddress & """, """ & _
strCustomerCity & """, """ & strCustomerState & """, """ & _
strCustomerZIPCode & """, """ & [txtCarMakeModel] & """, """ & _
intCarYear & """, """ & [txtProblemDescription] & """, """ & _
strPart1Name & """, """ & dblPart1UnitPrice & """, """ & _
intPart1Quantity & """, """ & dblPart1SubTotal & """, """ & _
strPart2Name & """, """ & dblPart2UnitPrice & """, """ & _
intPart2Quantity & """, """ & dblPart2SubTotal & """, """ & _
strPart3Name & """, """ & dblPart3UnitPrice & """, """ & _
intPart3Quantity & """, """ & dblPart3SubTotal & """, """ & _
strPart4Name & """, """ & dblPart4UnitPrice & """, """ & _
intPart4Quantity & """, """ & dblPart4SubTotal & """, """ & _
strPart5Name & """, """ & dblPart5UnitPrice & """, """ & _
intPart5Quantity & """, """ & dblPart5SubTotal & """, """ & _
strJobPerformed1 & """, """ & dblJobPrice1 & """, """ & _
strJobPerformed2 & """, """ & dblJobPrice2 & """, """ & _
strJobPerformed3 & """, """ & dblJobPrice3 & """, """ & _
strJobPerformed4 & """, """ & dblJobPrice4 & """, """ & _
strJobPerformed5 & """, """ & dblJobPrice5 & """, """ & _
curTotalParts & """, """ & curTotalLabor & """, """ & _
dblTaxRate & """, """ & curTaxAmount & """, """ & _
curRepairTotal & """, """ & dteRepairDate & """, """ & _
dteTimeReady & """, """ & strRecommendations & """);"
Set conCPAS = Application.CurrentProject.Connection
conCPAS.Execute strInsertInto & " " & strValues
MsgBox "The new repair order has been added to the database"
' Reset the form in case the user wants to create a new record
cmdResetOrder_Click()
Exit Sub
cmdSubmitRepairOrder_Error:
MsgBox "There was a problem processing this order" & vbCrLf & _
"Please call the IT Support team and report the error as" & vbCrLf & _
CStr(Err.Number) & ": " & Err.Description
Resume Next
End Sub
|

|
|
||
| Previous | Copyright © 2005-2010 FunctionX, Inc. | Next |
|
|
||