Lab_1 MPG

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 TravelExpenses()
    'Declare variables
    Dim firstName, lastName, nMiles, milesPerGallon, response, output As String 'Always declare string types for data to be entered from keyboard
    Dim avgPrice, tripCost As Currency
    Dim isValid As Boolean        'Flag to terminate the DO LOOP
 
    'INPUT SECTION: ***** FIRST NAME *****************************************************************
    'Capture First Name from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        firstName = InputBox("Please enter your First Name (e.g., John); ", "FIRST NAME")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If firstName = "" 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 value (e.g., John)" ' vbExclamation, "NO DATA ENTRY"  'Header reflects no Data Entry scenario
            End If
 
        'Input not blank; validate input for other requirements
 
        'Check if input is non-numeric
        ElseIf IsNumeric(firstName) Then
            MsgBox "First Name must be a non-numeric value (e.g., John)", vbExclamation, "NUMERIC ENTRY"  'Header reflects non-numeric 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: ***** LAST NAME *****************************************************************
    'Capture Last Name from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        lastName = InputBox("Please enter your Last Name (e.g., Smith); ", "LAST NAME")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If lastName = "" 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 value (e.g., Smith)" ' vbExclamation, "NO DATA ENTRY"  'Header reflects no Data Entry scenario
            End If
 
        'Input not blank; validate input for other requirements
 
        'Check if input is non-numeric
        ElseIf IsNumeric(lastName) Then
            MsgBox "Last Name must be a non-numeric value (e.g., Smith)", vbExclamation, "NUMERIC ENTRY"  'Header reflects non-numeric 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: ***** MILES TRAVELED *****************************************************************
    'Capture Miles Traveled from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        nMiles = InputBox("Please enter Miles Traveled (e.g., 200); ", "MILES TRAVELED")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If nMiles = "" 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., 200)" ' 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(nMiles) Then
            MsgBox "Miles Traveled must be a numeric value (e.g., 200)", vbExclamation, "NON-NUMERIC ENTRY"  'Header reflects non-numeric scenario
 
        'Input is numeric but check if it is positive
        ElseIf nMiles < 0 Then
            MsgBox "Miles Traveled cannot be a negative value", vbExclamation, "NEGATIVE ENTRY"  'Header reflects negative entry scenario
 
        'Input is positive numeric; check if it is a DECIMAL number
        ElseIf nMiles < 0 Then
            MsgBox "Miles Traveled must be a whole number (e.g., 200)", 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
 
 
    'INPUT SECTION: ***** MILES PER GALLON ***********************************************
    'Capture Miles Per Gallon from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        milesPerGallon = InputBox("Please enter Miles Per Gallon (e.g., 29.5); ", "MILES PER GALLON")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If milesPerGallon = "" 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., 29.5)" ' 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(milesPerGallon) Then
            MsgBox "Miles Per Gallon must be a numeric value (e.g., 29.5)", vbExclamation, "NON-NUMERIC ENTRY"  'Header reflects non-numeric scenario
 
        'Input is numeric but check if it is positive
        ElseIf milesPerGallon < 0 Then
            MsgBox "Miles Per Gallon 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: ***** AVERAGE PRICE PER GALLON ***********************************************
    'Capture Average Price Per Gallon from keyboard inside a DO LOOP
    isValid = False                    'Initialize the flag to false and convert it to TRUE if conditions are met
    Do
        avgPrice = InputBox("Please enter Average Price Per Gallon for Fuel (e.g., 3.25); ", "AVERAGE PRICE PER GALLON")  'MsgBox Header must reflect Data asked for
 
        'Check if the input is NULL
        If avgPrice = "" 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., 3.25)" ' 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(avgPrice) Then
            MsgBox "Average Price Per Gallon must be a numeric value (e.g., 3.25)", vbExclamation, "NON-NUMERIC ENTRY"  'Header reflects non-numeric scenario
 
        'Input is numeric but check if it is positive
        ElseIf avgPrice < 0 Then
            MsgBox "Average Price Per Gallon 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
 
 
    'PROCESSING SECTION: ***** CALCULATE TOTAL COST ******************************************************
    'NOTE: All string values must be converted to corresponding NUMERIC before you do the calculation.
 
    tripCost = (CInt(nMiles) / CDbl(milesPerGallon)) * CDbl(avgPrice)
 
    '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 = WorksheetFunction.Rept("=", 30) & vbCrLf    'Repeat the char "=" 30 times on screen
    output = output & Format(firstName) & " " & Format(lastName) & " traveled " & Format(nMiles, "##,###") & " miles, " & vbCrLf
    output = output & "got " & Format(milesPerGallon, "##.#") & " miles per gallon on average, " & vbCrLf
    output = output & "paid " & Format(avgPrice, "$##.##") & " per gallon on average, " & vbCrLf
    output = output & "and paid a total of " & Format(tripCost, "$#,###.##") & " for gas." & vbCrLf
    output = output & WorksheetFunction.Rept("=", 30)              'Repeat the char "=" 30 times on screen
 
    'Display output string
    MsgBox output, vbInformation, "TRIP SUMMARY"
 
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: 9
Date Added: 2017-09-06 16:42:03
Last Modified: 2017-09-09 14:05:31

Web Analytics