Total Product Expense Example Lab1

Send Snippet To: Save this snippet to Code Collector Pro -- view all surfrgeek's snippets
language: Other
license: Other

Code for Snippet:

                
Option Explicit
 
Sub TotalProductExpense()
    'Declare variables
    Dim unitCost, totalNum, response, output As String 'Always declare string types for data to be entered from keyboard
    Dim totalCost As Currency
    Dim isValid As Boolean        'Flag to terminate the DO LOOP
 
 
    'INPUT SECTION: ***** UNIT COST *************************************************************
    'Capture Unit Cost from keyboard inside a DO LOOP
    isValid = False                    'Initialize the falg to false and convert it to TRUE if conditions are met
    Do
        unitCost = InputBox("Please enter Unit Cost (e.g., 2.99); ", "UNIT COST")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If unitCost = "" Then
            'Check if user wants to quit
            response = MsgBox("Empty Input. Do you want to quit?", vbYesNo, "QUIT?")  'Header reflects QUIT scenario
            If response = vbYes Then
                Exit Sub                'Exit the entire Sub; no point in continuing with other stuff at bottom
 
 
            Else
                'Not quitting; display proper error message and continue validation
                MsgBox "You must enter a valid numeric value (e.g., 2.99)" ' vbExclamation, "NO DATA ENTRY"  'Header reflects no Data Entry scenario
            End If
 
        'Input not blank; validate input for other requirements
 
        'Check if input is numeric
        ElseIf Not IsNumeric(unitCost) Then
            MsgBox "Unit Cost must be a numeric value (e.g., 2.99)", vbExclamation, "NON-NUMERIC ENTRY"  'Header reflects non-numeric scenario
 
        'Input is numeric but check if it is positive
        ElseIf unitCost < 0 Then
            MsgBox "Unit Cost cannot be a negative value", vbExclamation, "NEGATIVE ENTRY"  'Header reflects negative entry scenario
 
        'If the control reaches here, it means the input is valid; change the Flag to "True" to exit DO LOOP
        Else
            isValid = True
 
        End If
 
    Loop Until isValid = True
 
 
    'INPUT SECTION: ***** TOTAL NUMBER OF UNITS ***********************************************
    'Capture Total Number of Items from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        totalNum = InputBox("Please enter Total Number of Units (e.g., 15); ", "TOTAL NUMBER OF UNITS")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If totalNum = "" Then
            'Check if user wants to quit
            response = MsgBox("Empty Input. Do you want to quit?", vbYesNo, "QUIT?")  'Header reflects QUIT scenario
            If response = vbYes Then
                Exit Sub                'Exit the entire Sub; no point in continuing with other stuff at bottom
 
 
            Else
                'Not quitting; display proper error message and continue validation
                MsgBox "You must enter a valid numeric value (e.g., 15)" ' vbExclamation, "NO DATA ENTRY"  'Header reflects no Data Entry scenario
            End If
 
        'Input not blank; validate input for other requirements
 
        'Check if input is numeric
        ElseIf Not IsNumeric(totalNum) Then
            MsgBox "Total Number of Units must be a numeric value (e.g., 15)", vbExclamation, "NON-NUMERIC ENTRY"  'Header reflects non-numeric scenario
 
        'Input is positive numeric; check if it is a DECIMAL number
        ElseIf InStr(totalNum, ".") <> 0 Then
            MsgBox "Total Number of Units cannot be a negative value", vbExclamation, "NEGATIVE ENTRY"  'Header reflects negative entry scenario
 
        'Input is numeric but check if it is positive
        ElseIf totalNum < 0 Then
            MsgBox "Total Number of Units must be a whole number (e.g., 15)", vbExclamation, "DECIMAL ENTRY"  'Header reflects decimal entry scenario
 
        'If the control reaches here, it means the input is valid; change the Flag to "True" to exit DO LOOP
        Else
            isValid = True
 
        End If
 
    Loop Until isValid = True
 
 
    'PROCESSING SECTION: ***** CALCULATE TOTAL COST ******************************************************
    'NOTE: All string values must be converted to corresponding NUMERIC before you do the calculation.
 
    totalCost = CDbl(unitCost) * CDbl(totalNum)
 
    'OUTPUT SECTION: ***** DISPLAY SCREEN OUTPUT *********************************************************
    'Formulate an output string with nice formatting
    '"vbTab" is a built-in constant that pushes the disply one TAB to the right
    '"vbCrLf" is a built-in constant that pushes display to the start of the next line
 
    output = "Unit Cost:" & vbTab & vbTab & vbTab & Format(unitCost, "$##,##0.00") & vbCrLf
    output = output & "Total Number of Items:" & vbTab & Format(totalNum, "##,##0") & vbCrLf
    output = output & WorksheetFunction.Rept("=", 30) & vbCrLf    'Repeat the char "=" 30 times on screen
    output = output & "Total Cost:" & vbTab & vbTab & Format(totalCost, "$##,#00.00") & vbCrLf
    output = output & WorksheetFunction.Rept("=", 30)              'Repeat the char "=" 30 times on screen
 
    'Display output string
    MsgBox output, vbInformation, "INVOICE"
 
End Sub
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 8
Date Added: 2017-09-06 16:35:31
Last Modified: 2017-09-09 14:05:31

Web Analytics