Management Information Systems (33:623:370)Fall 2004, Professor EcksteinCode from December 2 Class

To help you with assignment 9, here is some code similar to what we did in class on December 2. It implements all the buttons on the “products” form of the computer store database. I added some “rem” statements, which are just comments that try to explain what is going on, and are not actually executed by the computer. You don’t need to put “rem” statements in your answer to homework 9.

Option Compare Database

Rem this subroutine is invoked by the "log order from supplier" button

Private Sub Command13_Click()

Dim AddAmount As Integer

Rem call our own special function to get the number of units

Rem if the function returns a negative value, that means things

Rem didn't work out (the user cancelled or entered something bad)

AddAmount = PositiveNumberFromBox("Units ordered from supplier:")

If AddAmount > 0 Then

Rem if the user entered a good number, add it to number on order

UnitsOnOrder = UnitsOnOrder + AddAmount

End If

End Sub

Rem this subroutine is called by the "log arrival from supplier" button

Private Sub Command14_Click()

Dim ArriveAmount As Integer

Rem get a number the same as the last subroutine,

Rem except with a different message

ArriveAmount = PositiveNumberFromBox("Units arrived from supplier:")

If ArriveAmount > UnitsOnOrder Then

Rem Complain if the amout seems to high

Call MsgBox(ArriveAmount & " is more units than you have on order.")

Rem otherwise, if our function thought it was a valid entry,

Rem transfer it from UnitsOnOrder to UnitsInStock

ElseIf ArriveAmount > 0 Then

UnitsOnOrder = UnitsOnOrder - ArriveAmount

UnitsInStock = UnitsInStock + ArriveAmount

End If

End Sub

Rem function to display a message in a input box, get a number back,

Rem and make sure it's numeric and positive. If anything goes wrong

Rem or the user cancels, return -1. Otherwise return the user's number

Private Function PositiveNumberFromBox(message As String) As Integer

Rem set the number to be returned to -1; change it later if all seems well

PositiveNumberFromBox = -1

Dim FromInputBox As String

FromInputBox = InputBox(message)

If Not IsNumeric(FromInputBox) Then

Rem not a number -- complain

Call MsgBox("'" & FromInputBox & "' is not a valid amount.")

ElseIf FromInputBox <= 0 Then

Rem a number, but not positive -- complain

Call MsgBox("You must enter a positive amount.")

Else

Rem otherwise, things look OK, so return the user's number instead

Rem of -1

PositiveNumberFromBox = FromInputBox

End If

End Function

Rem this is called by the 10% markdown button

Private Sub Command15_Click()

Call Markdown(10)

End Sub

Rem this is called by the 5% markdown button

Private Sub Command16_Click()

Call Markdown(20)

End Sub

Rem common code for all markdowns

Private Sub Markdown(percent As Double)

Dim newprice As Double

Rem reduce the price by "percent" percent, round to the nearest

Rem dollar, and subtract 5 cents.

newprice = UnitPrice * (1 - percent / 100)

newprice = Round(newprice, 0) - 0.05

Rem if the new price is under $50, complain and don't change the price.

Rem I didn't do exactly this in class

If (newprice < 50) Then

Call MsgBox("Markdowns to prices under $50 must be done manually.")

Else

Rem if still over $50, change the price in the table

UnitPrice = newprice

End If

End Sub

-- 2 --