Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The Fruit Stand program is a simplified data entry system for a small fruit vendor. It implements many chapter-based concepts such as variables, constants, and VBA statements.
To build the Fruit Stand program, you’ll need to create a form in Design view, as shown in Figure 3.11.
FIGURE 3.11 Building the Fruit Stand program in Design view.
Controls and properties of the Fruit Stand program are described in Table 3.5.
TABLE 3.5 CONTROLS AND PROPERTIES OF THE FRUIT STAND PROGRAM
All of the code required to build the Fruit Stand program is shown here.
Option Compare Database Option Explicit ‘ declare module level variable and constants Dim dRunningTotal As Currency Const TAXRATE As Currency = 0.07 Const PRICEPERAPPLE As Currency = 0.1 Const PRICEPERORANGE As Currency = 0.2 Const PRICEPERBANANA = 0.3
Private Sub cmdCalculateTotals_Click()
‘ declare procedure-level variables
Dim dSubTotal As Currency
Dim dTotal As Currency
Dim dTax As Currency
‘ calculate and apply sub total
dSubTotal = (PRICEPERAPPLE * Val(Me.txtApples.Value)) + _
(PRICEPERORANGE * Val(Me.txtOranges.Value)) + _
(PRICEPERBANANA * Val(txtBananas.Value))
Me.lblSubTotal.Caption = “$” & FormatNumber(dSubTotal, 2)
‘ calculate and apply tax
dTax = (TAXRATE * dSubTotal)
Me.lblTax.Caption = ”$” & FormatNumber(dTax, 2)
‘ calculate and apply total cost
dTotal = dTax + dSubTotal
Me.lblTotal.Caption = ”$” & FormatNumber(dTotal, 2)
‘ build and apply running total using module-level variable
dRunningTotal = dRunningTotal + dTotal
Me.lblRunningTotal.Caption = ”$” & FormatNumber(dRunningTotal, 2) End Sub
Private Sub cmdExit_Click() DoCmd.Quit ‘ terminates the application End Sub
Private Sub cmdResetFields_Click() ‘ reset application fields Me.txtApples.Value = “0” Me.txtOranges.Value = “0” Me.txtBananas.Value = “0” Me.lblSubTotal.Caption = “$0.00” Me.lblTax.Caption = “$0.00” Me.lblTotal.Caption = “$0.00” End Sub
Private Sub cmdResetRunningTotal_Click() ‘ reset running total variable and application field dRunningTotal = 0 Me.lblRunningTotal.Caption = “$0.00” End Sub
Privte Sub Form_Load()
‘ set focus to first text box
Me.txtApples.SetFocus
‘set default quantities when the form first loads
Me.txtApples.Value = 0
Me.txtBananas.Value = 0
Me.txtOranges.Value = 0
End Sub