Handling Shopping Sessions in Visual Basic
|
|
A shopping session consists of a
customer buying items from a store. To take of shopping, we will use a form. To
start, an employee (or clerk) must be identified so we would know who handled
the transaction. Each item in the store has a unite number (called an item
number), a name (as a short description a size (or item size), and a unit price.
Some items can also be on sale. When an item is not on sale, it is sold at
normal or full price. When an item is on sale, a discount rate applies. When a
discount rate applies, we will calculate the discount amount, then the sale
price will be deducted from that. In a receipt, we will show all these values.
Practical Learning: Handling Shopping Sessions
|
|
- Start a text editor such as Notepad and type the following code in it:
Imports System
Imports System.IO
Imports System.Data
Imports System.Drawing
Imports System.Data.OleDb
Imports System.Windows.Forms
Public Class ShoppingSession
Inherits Form
Dim lblItemNumberSelected As Label
Friend WithEvents txtItemNumberSelected As TextBox
Dim lblReceiptNumber As Label
Dim txtReceiptNumber As TextBox
Dim colItemNumber As ColumnHeader
Dim colItemName As ColumnHeader
Dim colItemSize As ColumnHeader
Dim colUnitPrice As ColumnHeader
Dim colDiscountRate As ColumnHeader
Dim colDiscountAmount As ColumnHeader
Dim colSalePrice As ColumnHeader
Dim lvwSelectedItems As ListView
Friend WithEvents txtEmployeeNumber As TextBox
Dim lblEmployeeNumber As Label
Dim dtpSaleDate As DateTimePicker
Dim lblSaleTime As Label
Friend WithEvents txtAmountTendered As TextBox
Dim lblTendered As Label
Dim txtChange As TextBox
Dim lblChange As Label
Dim txtOrderTotal As TextBox
Dim lblOrderTotal As Label
Dim txtEmployeeName As TextBox
Dim txtItemNumberRemove As TextBox
Dim lblItemNumberToRemove As Label
Friend WithEvents btnRemoveItem As Button
Friend WithEvents btnSubmit As Button
Friend WithEvents btnReset As Button
Friend WithEvents btnClose As Button
Friend WithEvents tmrDateTime As Timer
Private strFilePath As String
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
' Label: Item Number to Add
lblItemNumberSelected = New Label()
lblItemNumberSelected.AutoSize = True
lblItemNumberSelected.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblItemNumberSelected.Location = New Point(16, 22)
lblItemNumberSelected.TabIndex = 2
lblItemNumberSelected.Text = "Item # to Add:"
Controls.Add(lblItemNumberSelected)
' Text Box: Item Number to Add
txtItemNumberSelected = New TextBox()
txtItemNumberSelected.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtItemNumberSelected.Location = New Point(235, 18)
txtItemNumberSelected.Size = New System.Drawing.Size(155, 41)
txtItemNumberSelected.TabIndex = 3
Controls.Add(txtItemNumberSelected)
' Label: Receipt Number
lblReceiptNumber = New Label()
lblReceiptNumber.AutoSize = True
lblReceiptNumber.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblReceiptNumber.Location = New Point(1081, 22)
lblReceiptNumber.TabIndex = 5
lblReceiptNumber.Text = "Receipt #:"
Controls.Add(lblReceiptNumber)
' Text Box: Receipt Number
txtReceiptNumber = New TextBox()
txtReceiptNumber.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtReceiptNumber.Location = New Point(1260, 18)
txtReceiptNumber.Size = New System.Drawing.Size(105, 41)
txtReceiptNumber.TabIndex = 6
Controls.Add(txtReceiptNumber)
' Column: umn: Item Number
colItemNumber = New ColumnHeader()
colItemNumber.Text = "Item #"
colItemNumber.Width = 100
' Column: umn: Item Name
colItemName = New ColumnHeader()
colItemName.Text = "Item Name/Description"
colItemName.Width = 610
' Column: umn: Item Size
colItemSize = New ColumnHeader()
colItemSize.Text = "Size"
colItemSize.Width = 150
' Column: umn: Unit Price
colUnitPrice = New ColumnHeader()
colUnitPrice.Text = "Unit Price"
colUnitPrice.TextAlign = HorizontalAlignment.Right
colUnitPrice.Width = 140
' Column: umn: Discount Rate
colDiscountRate = New ColumnHeader()
colDiscountRate.Text = "Dscnt Rt"
colDiscountRate.TextAlign = HorizontalAlignment.Right
colDiscountRate.Width = 120
' Column: umn: Discount Amount
colDiscountAmount = New ColumnHeader()
colDiscountAmount.Text = "Dscnt Amt"
colDiscountAmount.TextAlign = HorizontalAlignment.Right
colDiscountAmount.Width = 140
' Column: umn: Sale Price
colSalePrice = New ColumnHeader()
colSalePrice.Text = "Sale Price"
colSalePrice.TextAlign = HorizontalAlignment.Right
colSalePrice.Width = 130
' List View: Selected Items
lvwSelectedItems = New ListView()
lvwSelectedItems.Columns.AddRange(New ColumnHeader() {colItemNumber, colItemName, colItemSize, colUnitPrice, colDiscountRate, colDiscountAmount, colSalePrice})
lvwSelectedItems.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lvwSelectedItems.FullRowSelect = True
lvwSelectedItems.GridLines = True
lvwSelectedItems.Location = New Point(16, 71)
lvwSelectedItems.Size = New System.Drawing.Size(1424, 321)
lvwSelectedItems.TabIndex = 8
lvwSelectedItems.View = View.Details
Controls.Add(lvwSelectedItems)
' Date/Time Picker: Sale Date
dtpSaleDate = New DateTimePicker()
dtpSaleDate.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
dtpSaleDate.Location = New Point(16, 469)
dtpSaleDate.Size = New System.Drawing.Size(402, 39)
dtpSaleDate.TabIndex = 9
Controls.Add(dtpSaleDate)
lblSaleTime = New Label()
lblSaleTime.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblSaleTime.Location = New Point(429, 469)
lblSaleTime.Text = "Sale Time"
lblSaleTime.Size = New System.Drawing.Size(177, 39)
lblSaleTime.TabIndex = 11
Controls.Add(lblSaleTime)
' Label: Tendered
lblTendered = New Label()
lblTendered.AutoSize = True
lblTendered.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblTendered.Location = New Point(1089, 449)
lblTendered.TabIndex = 15
lblTendered.Text = "Tendered:"
Controls.Add(lblTendered)
' Text Box: Amount Tendered
txtAmountTendered = New TextBox()
txtAmountTendered.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtAmountTendered.Location = New Point(1278, 445)
txtAmountTendered.Size = New System.Drawing.Size(130, 41)
txtAmountTendered.TabIndex = 14
txtAmountTendered.Text = "0.00"
txtAmountTendered.TextAlign = HorizontalAlignment.Right
Controls.Add(txtAmountTendered)
' Label: Item Number to Remove
lblItemNumberToRemove = New Label()
lblItemNumberToRemove.AutoSize = True
lblItemNumberToRemove.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblItemNumberToRemove.Location = New Point(16, 416)
lblItemNumberToRemove.TabIndex = 18
lblItemNumberToRemove.Text = "Item # to Remove:"
Controls.Add(lblItemNumberToRemove)
' Text Box: Item Number to Remove
txtItemNumberRemove = New TextBox()
txtItemNumberRemove.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtItemNumberRemove.Location = New Point(290, 412)
txtItemNumberRemove.Size = New System.Drawing.Size(155, 41)
txtItemNumberRemove.TabIndex = 19
Controls.Add(txtItemNumberRemove)
' Button: Remove Item
btnRemoveItem = New Button()
btnRemoveItem.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
btnRemoveItem.Location = New Point(451, 408)
btnRemoveItem.Size = New System.Drawing.Size(155, 46)
btnRemoveItem.TabIndex = 20
btnRemoveItem.Text = "Remove"
Controls.Add(btnRemoveItem)
' Label: Order Total
lblOrderTotal = New Label()
lblOrderTotal.AutoSize = True
lblOrderTotal.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblOrderTotal.Location = New Point(1089, 401)
lblOrderTotal.TabIndex = 12
lblOrderTotal.Text = "Order Total:"
Controls.Add(lblOrderTotal)
' Text Box: Order Total
txtOrderTotal = New TextBox()
txtOrderTotal.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtOrderTotal.Location = New Point(1278, 396)
txtOrderTotal.Size = New System.Drawing.Size(130, 41)
txtOrderTotal.TabIndex = 13
txtOrderTotal.Text = "0.00"
txtOrderTotal.TextAlign = HorizontalAlignment.Right
Controls.Add(txtOrderTotal)
' Label: Change
lblChange = New Label()
lblChange.AutoSize = True
lblChange.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblChange.Location = New Point(1089, 496)
lblChange.TabIndex = 16
lblChange.Text = "Change:"
Controls.Add(lblChange)
' Text Box: Change
txtChange = New TextBox()
txtChange.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtChange.Location = New Point(1278, 492)
txtChange.Size = New System.Drawing.Size(129, 41)
txtChange.TabIndex = 17
txtChange.Text = "0.00"
txtChange.TextAlign = HorizontalAlignment.Right
Controls.Add(txtChange)
' Label: Employee Number
lblEmployeeNumber = New Label()
lblEmployeeNumber.AutoSize = True
lblEmployeeNumber.Font = New System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
lblEmployeeNumber.Location = New Point(10, 558)
lblEmployeeNumber.TabIndex = 0
lblEmployeeNumber.Text = "Employee #:"
Controls.Add(lblEmployeeNumber)
' Text Box: Employee Number
txtEmployeeNumber = New TextBox()
txtEmployeeNumber.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtEmployeeNumber.Location = New Point(206, 554)
txtEmployeeNumber.Size = New System.Drawing.Size(155, 41)
txtEmployeeNumber.TabIndex = 1
Controls.Add(txtEmployeeNumber)
' Text Box: Employee Name
txtEmployeeName = New TextBox()
txtEmployeeName.Font = New System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
txtEmployeeName.Location = New Point(370, 554)
txtEmployeeName.Size = New System.Drawing.Size(499, 41)
txtEmployeeName.TabIndex = 7
Controls.Add(txtEmployeeName)
' Button: Reset
btnReset = New Button()
btnReset.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
btnReset.Location = New Point(915, 551)
btnReset.Size = New System.Drawing.Size(172, 46)
btnReset.TabIndex = 10
btnReset.Text = "Reset"
Controls.Add(btnReset)
' Button: Submit
btnSubmit = New Button()
btnSubmit.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
btnSubmit.Location = New Point(1097, 551)
btnSubmit.Size = New System.Drawing.Size(177, 46)
btnSubmit.TabIndex = 4
btnSubmit.Text = "Submit"
Controls.Add(btnSubmit)
' Button: Close
btnClose = New Button()
btnClose.Font = New System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0)
btnClose.Location = New Point(1281, 551)
btnClose.Size = New System.Drawing.Size(129, 46)
btnClose.TabIndex = 21
btnClose.Text = "Close"
Controls.Add(btnClose)
tmrDateTime = New Timer()
tmrDateTime.Enabled = True
strFilePath = "C:\FunDS\FunDS1.accdb ' "FunDS1.mdb"
ClientSize = New System.Drawing.Size(1454, 615)
MaximizeBox = False
MinimizeBox = False
StartPosition = FormStartPosition.CenterScreen
Text = "FunDS - Shopping Session"
End Sub
Private Sub ResetForm()
Dim iReceiptNumber As Integer = 100000
Using odcStoreSales As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath) ' Provider=Microsoft.JET.OLEDB.4.0
Dim cmdStoreSales As OleDbCommand = New OleDbCommand("SELECT ReceiptNumber " & _
"FROM StoreSales;", _
odcStoreSales)
odcStoreSales.Open()
Dim sdaStoreSales As OleDbDataAdapter = New OleDbDataAdapter(cmdStoreSales)
Dim dsStoreSales As DataSet = New DataSet("StoreSalesSet")
sdaStoreSales.Fill(dsStoreSales)
For Each drStoreSale As DataRow In dsStoreSales.Tables(0).Rows
iReceiptNumber = CInt(drStoreSale("ReceiptNumber"))
Next
End Using
txtReceiptNumber.Text = CStr(iReceiptNumber + 1)
txtItemNumberSelected.Text = ""
lvwSelectedItems.Items.Clear()
txtItemNumberRemove.Text = ""
dtpSaleDate.Value = DateTime.Now
lblSaleTime.Text = DateTime.Now.ToString()
txtEmployeeNumber.Text = ""
txtEmployeeName.Text = ""
txtOrderTotal.Text = "0.00"
txtAmountTendered.Text = "0.00"
txtChange.Text = "0.00"
End Sub
Private Sub FormLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ResetForm()
End Sub
Private Sub btnResetClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
ResetForm()
End Sub
Private Sub tmrDateTimeTick(ByVal sender As Object, ByVal e As EventArgs) Handles tmrDateTime.Tick
dtpSaleDate.Value = DateTime.Today
lblSaleTime.Text = DateTime.Now.ToLongTimeString()
End Sub
Private Sub txtItemNumberSelectedKeyUp(ByVal sender As Object, ByVal e As keyEventArgs) Handles txtItemNumberSelected.KeyUp
Dim dblTotal As Double = 0.0
If e.KeyCode = Keys.Enter Then
If (String.IsNullOrEmpty(txtItemNumberSelected.Text)) Then
MsgBox("You must enter an item number.", vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
Exit Sub
Else
Using odcStoreItems As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath)
Dim cmdStoreItems As OleDbCommand = New OleDbCommand("SELECT ItemNumber, ItemName, ItemSize, UnitPrice, DiscountRate " & _
"FROM StoreItems " & _
"WHERE ItemNumber = '" & txtItemNumberSelected.Text & "';", _
odcStoreItems)
odcStoreItems.Open()
Dim sdaStoreItems As OleDbDataAdapter = New OleDbDataAdapter(cmdStoreItems)
Dim dsStoreItems As DataSet = New DataSet("StoreItemSet")
sdaStoreItems.Fill(dsStoreItems)
For Each drStoreItem As DataRow In dsStoreItems.Tables(0).Rows
Dim lviStoreItem As ListViewItem = New ListViewItem(CStr(drStoreItem("ItemNumber")))
lviStoreItem.SubItems.Add(CStr(drStoreItem("ItemName")))
If IsDBNull(drStoreItem("ItemSize")) Then
lviStoreItem.SubItems.Add("")
Else
lviStoreItem.SubItems.Add(CStr(drStoreItem("ItemSize")))
End If
lviStoreItem.SubItems.Add(FormatNumber(CStr(drStoreItem("UnitPrice"))))
If IsDBNull(drStoreItem("DiscountRate")) Then
lviStoreItem.SubItems.Add("0") ' Discount Rate
lviStoreItem.SubItems.Add("0.00") ' Discount Amount
lviStoreItem.SubItems.Add(FormatNumber(CStr(drStoreItem("UnitPrice")))) ' Sale Price
Else
Dim unitPrice = CDbl(CStr(drStoreItem("UnitPrice")))
Dim discountRate As Double = CDbl(drStoreItem("DiscountRate"))
Dim discountAmount = unitPrice * discountRate
Dim salePrice = unitPrice - discountAmount
lviStoreItem.SubItems.Add(FormatNumber(CStr(discountRate * 100), 0) & "%") ' Discount Rate
lviStoreItem.SubItems.Add(FormatNumber(CStr(discountAmount))) ' Discount Amount
lviStoreItem.SubItems.Add(FormatNumber(CStr(salePrice))) ' Sale Price
End If
lvwSelectedItems.Items.Add(lviStoreItem)
Next
txtItemNumberSelected.Text = ""
If lvwSelectedItems.Items.Count > 0 Then
For Each lviStoreItem As ListViewItem In lvwSelectedItems.Items
dblTotal = dblTotal + CDbl(lviStoreItem.SubItems(6).Text)
Next
txtOrderTotal.Text = FormatNumber(dblTotal)
End If
End Using
End If
End If
End Sub
Private Sub txtEmployeeNumberLeave(ByVal sender As Object, ByVal e As EventArgs) Handles txtEmployeeNumber.Leave
If IsDBNull(txtEmployeeNumber.Text) Or IsNothing(txtEmployeeNumber.Text) Then
Exit Sub
Else
Using odcEmployees As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath)
Dim cmdEmployees As OleDbCommand = New OleDbCommand("SELECT FirstName, LastName " & _
"FROM Employees " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber.Text & "';", _
odcEmployees)
odcEmployees.Open()
Dim sdaEmployees As OleDbDataAdapter = New OleDbDataAdapter(cmdEmployees)
Dim dsEmployees As DataSet = New DataSet("EmployeesSet")
sdaEmployees.Fill(dsEmployees)
For Each drEmployee As DataRow In dsEmployees.Tables(0).Rows
txtEmployeeName.Text = CStr(drEmployee("LastName")) & ", " & CStr(drEmployee("FirstName"))
Next
End Using
End If
End Sub
Private Sub txtAmountTenderedKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtAmountTendered.KeyUp
Dim orderTotal As Double = 0.0
Dim amountTendered As Double = 0.0
Dim change As Double = 0.0
If (e.KeyCode = Keys.Tab) Or (e.KeyCode = Keys.Enter) Then
If IsDBNull(txtOrderTotal.Text) Then
Exit Sub
End If
ElseIf IsDBNull(txtAmountTendered.Text) Then
Exit Sub
Else
orderTotal = CDbl(txtOrderTotal.Text)
amountTendered = CDbl(txtAmountTendered.Text)
change = amountTendered - orderTotal
txtChange.Text = FormatNumber(change)
End If
End Sub
Private Sub txtAmountTenderedLeave(ByVal sender As Object, ByVal e As EventArgs) Handles txtAmountTendered.Leave
Dim orderTotal As Double = 0.0
Dim amountTendered As Double = 0.0
Dim change As Double = 0.0
If IsDBNull(txtOrderTotal.Text) Then
Exit Sub
ElseIf IsDBNull(txtAmountTendered.Text) Then
Exit Sub
Else
orderTotal = CDbl(txtOrderTotal.Text)
amountTendered = CDbl(txtAmountTendered.Text)
change = amountTendered - orderTotal
txtChange.Text = FormatNumber(change)
End If
End Sub
Private Sub btnRemoveItemClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnRemoveItem.Click
Dim dblTotal As Double = 0.0
Dim itemFound As Boolean = False
If lvwSelectedItems.Items.Count = 0 Then
MsgBox("The list view is empty.", vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
Exit Sub
ElseIf IsDBNull(txtItemNumberRemove.Text) Then
MsgBox("You must enter an item number and that exists in the list view.",
vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
Exit Sub
Else
For Each lviStoreItem As ListViewItem In lvwSelectedItems.Items
If lviStoreItem.SubItems(0).Text = txtItemNumberRemove.Text Then
itemFound = True
lvwSelectedItems.Items.Remove(lviStoreItem)
End If
Next
If itemFound = False Then
MsgBox("That item number is not in the list view.",
vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
Exit Sub
End If
If lvwSelectedItems.Items.Count > 0 Then
For Each lviStoreItem As ListViewItem In lvwSelectedItems.Items
dblTotal = dblTotal + CDbl(lviStoreItem.SubItems(6).Text)
Next
txtOrderTotal.Text = FormatNumber(dblTotal)
txtAmountTendered.Text = "0.00"
txtChange.Text = "0.00"
Else
txtOrderTotal.Text = "0.00"
txtAmountTendered.Text = "0.00"
txtChange.Text = "0.00"
End If
txtItemNumberRemove.Text = ""
End If
End Sub
Private Sub btnSubmitClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim strSoldItem As String
Dim strStoreSale As String
If lvwSelectedItems.Items.Count = 0 Then
MsgBox("There is no customer order to save.",
vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
Exit Sub
Else
strStoreSale = "INSERT INTO StoreSales(ReceiptNumber, EmployeeNumber, SaleDate, SaleTime, OrderTotal, AmountTendered, Change)" &
"VALUES(" & CInt(txtReceiptNumber.Text) & ", '" + txtEmployeeNumber.Text & "', #" &
dtpSaleDate.Value.ToShortDateString() & "#, #" & lblSaleTime.Text.ToString() & "#, " &
CDbl(txtOrderTotal.Text) & ", " & CDbl(txtAmountTendered.Text) & ", " & CDbl(txtChange.Text) & ");"
' Create a customer order using the information on the form except the items in the list view.
Using odcStoreSales As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath)
Dim cmdStoreSales As OleDbCommand = New OleDbCommand(strStoreSale, odcStoreSales)
odcStoreSales.Open()
cmdStoreSales.ExecuteNonQuery()
End Using
For Each lviStoreItem As ListViewItem In lvwSelectedItems.Items
If IsDBNull(lviStoreItem.SubItems(2).Text) Then
strSoldItem = "INSERT INTO SoldItems(ReceiptNumber, ItemNumber, ItemName, UnitPrice, DiscountRate, DiscountAmount, SalePrice) VALUES(" & _
CInt(txtReceiptNumber.Text) & ", " & lviStoreItem.SubItems(0).Text & ", '" & lviStoreItem.SubItems(1).Text & "', " & _
CDbl(lviStoreItem.SubItems(3).Text) & ", " & (CDbl(Replace(lviStoreItem.SubItems(4).Text, "%", "")) / 100) & ", " & CDbl(lviStoreItem.SubItems(5).Text) & _
", " & CDbl(lviStoreItem.SubItems(6).Text) & ");"
Else
strSoldItem = "INSERT INTO SoldItems(ReceiptNumber, ItemNumber, ItemName, ItemSize, UnitPrice, DiscountRate, DiscountAmount, SalePrice) VALUES(" & _
CInt(txtReceiptNumber.Text) & ", '" & lviStoreItem.SubItems(0).Text & "', '" & lviStoreItem.SubItems(1).Text & "', '" & _
lviStoreItem.SubItems(2).Text & "', " & CDbl(lviStoreItem.SubItems(3).Text) & ", " & (CDbl(Replace(lviStoreItem.SubItems(4).Text, "%", "")) / 100) & _
", " & CDbl(lviStoreItem.SubItems(5).Text) & ", " & CDbl(lviStoreItem.SubItems(6).Text) & ");"
End If
' Add the items of the list view in the SoldItems table
Using odcStoreSales As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath)
Dim cmdSoldItems As OleDbCommand = New OleDbCommand(strSoldItem, odcStoreSales)
odcStoreSales.Open()
cmdSoldItems.ExecuteNonQuery()
End Using
Next
' Remove the items of the list view from the StoreItems table
For Each lviStoreItem As ListViewItem In lvwSelectedItems.Items
Using odcStoreSales As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFilePath)
Dim cmdStoreItems As OleDbCommand = New OleDbCommand("DELETE FROM StoreItems " & _
"WHERE ItemNumber = '" & lviStoreItem.SubItems(0).Text & "';", odcStoreSales)
odcStoreSales.Open()
cmdStoreItems.ExecuteNonQuery()
End Using
Next
MsgBox("The customer's order has been saved.",
vbOkOnly Or vbInformation, "FunDS - Fun Department Store")
ResetForm()
End If
End Sub
Private Sub btnCloseClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnClose.Click
Close()
End Sub
<STAThread()>
Public Shared Sub Main()
Application.Run(New ShoppingSession())
End Sub
End Class
- Close the text editor (or Notepad)
- When asked whether you want to save, click Save
- Select the FunDS folder you had created and display it in the top combo
box
- Change the Save As Type (or files of type) combo box to All Files
- Set the file name to ShoppingSession.vb
- Click Save
- Display the Command Prompt (Start -> (All) Programs -> Accessories ->
Command Prompt)
- Type CD\ and press Enter to get to the root
- Type C: and press Enter to select the C: drive
- Type CD FunDS and press Enter to select the folder that contains the database
- Type vbc /t:winexe ShoppingSession.vb and press Enter to build the project
If you receive an error that vbc is not a valid program type:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc /t:winexe ShoppingSession.vb
and press Enter
- To close the Command Prompt, type exit and press Enter
- Return to Microsoft Access where the FunDS1 database is opened
- On the Ribbon, click Create and, in the Forms section, click Form Design
- Using the Properties window, set the following characteristics:
Caption: Fun Department Store - Shopping Session Review
Auto Center: Yes
Navigation Buttons: No
- In the Design section of the Ribbon, click Subform/Subreport and click
the form
- On the first page of the wizard, click Use An Existing Form
- In the list box, click sfSoldItems
- Click Next
- Click Finish
- In the Properties window, click Link Master Fields and type ReceiptNumber
- Click Link Child Fields and type ReceiptNumber
- Right-click the form and click Form Header/Footer
- Save the form as ShoppingSessionReview
- Design it as follows:
- Save the form
- Right-click the Find button and click Buils Event...
- In the Choose Builder dialog b ox, click Code Builder and click OK
- Implement the event as follows:
Private Sub cmdFind_Click()
If IsNull(ReceiptNumber) Then
MsgBox "You must enter a receipt number before viewing related records.", _
vbInformation Or vbOKOnly, "Ceil Inn"
Else
If Not IsNull(DLookup("ReceiptNumber", "StoreSales", "ReceiptNumber = " & ReceiptNumber)) Then
txtEmployeeNumber = DLookup("EmployeeNumber", "StoreSales", "ReceiptNumber = " & ReceiptNumber)
txtSaleDate = FormatDateTime(DLookup("SaleDate", "StoreSales", "ReceiptNumber = " & ReceiptNumber), vbLongDate)
txtSaleTime = DLookup("SaleTime", "StoreSales", "ReceiptNumber = " & ReceiptNumber)
txtOrderTotal = DLookup("OrderTotal", "StoreSales", "ReceiptNumber = " & ReceiptNumber)
txtAmountTendered = DLookup("AmountTendered", "StoreSales", "ReceiptNumber = " & ReceiptNumber)
txtChange = DLookup("Change", "StoreSales", "ReceiptNumber = " & ReceiptNumber)
txtEmployeeName = DLookup("LastName", "Employees", "EmployeeNumber = '" & txtEmployeeNumber & "'") & ", " & DLookup("FirstName", "Employees", "EmployeeNumber = '" & txtEmployeeNumber & "'")
End If
End If
End Sub
- In Object combo box, select cmdNewShoppingSession
- Implement its OnClick event as follows:
Private Sub cmdNewShoppingSession_Click()
Shell "C:\FunDS\ShoppingSession.exe", vbNormalFocus
End Sub
- In Object combo box, select cmdClose
- Implement its OnClick event as follows:
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
- Save and close the form
If you want to test the application, use the following
values.
|
|