CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Format function: Format$(Expression, formatting string) or Format(Expression, FS) Examples Results Format$(3000, “Currency”) $3,000.00 Format$(3000.1, “Fixed”) 3000.10 Format$(.25, “Percent”) 25.00% Format$(1234, “Scientific”) 1.234E+03 Format$(3000, “Standard”) 3,000

Evaluate the following and determine the results. Remember to look at prefixes to determine the argument type. Cash = 3250.75 Function Results lblNetCash = format( intCash, “Standard”) lblNetCash = format( intCash, “Currency”) lblNetCash = format( intCash, “Percent”) lblNetCash = format( intCash, “Scientific”) lblNetCash = format( intCash, “Fixed”)

Mathematical Functions: Function Returns Example Result Abs(x) Absolute value of x Abs(-3.2) 3.2 Cos(x) Cosine value of x where x is an Cos(3.14159265359) -1 angle in radians Exp(x) ex Exp(1) 2.71828182845905 Fix(x) Integer portion of x by Fix(3.6) 3 truncation Int(x) Integer portion of x by Int(3.6) 3 truncation Log(x) Natural logarithm of x Log(2.71828182845905) 1 Rnd() A random number in the range Rnd() Any random number of (0 to 1) (but less than 1) between 0 and 1 Sgn(x) Sign (1,0 or –1) of x Sgn(-9) -1 Sin(x) x, where x is an angle in Sin(0) 0 radians Sqr(x) Square root of x Sqr(100) 10 Tan(x) Tangent of x where x is a Tan(100) -0.58721 radiant

Evaluate the following and determine the results. Remember to look at prefixes to determine the argument type.

Function Results Abs(202.50 – 1525.456) Log(3.14159265359) Sqr(5000) Sgn(-25) CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Int(-12535.9999) Fix(-12535.9999)

Conversion Functions –x is an expression that can be evaluated to a numeric value Function Returns Example Results CBool(x) Boolean type value CBool(1) True all non-zero values convert to true CByte(x) Byte (character) CByte(“12”) 12 CCur(x) Currency type CCur(“12.34”) 12.34 CDate(x) Date/Time type CDate(“31-Dec-02”) 12/31/02 CDbl(x) Double type CDbl(“12.34”) 12.34 CDec(x) Decimal type CDec(“12.34”) 12.34 CInt(x) Integer type CInt(“12.34”) 12 CLng(x) Long type CLng(“12.34”) 12 CSng(x) Single type CSng(“12.34”) 12.34 EOF(ChannelNo) Checks for end of If EOF(1) then When the end of file markers file is reached it will be true

Evaluate the following and determine the results. Remember to look at prefixes to determine the argument type. TxtHourRate = 16.75 txtHours = 40.25 Function Results dblSalary = Cdbl(txtHourRate) * Cdbl(txtHours) intSalary = CInt(txtHourRate) * CInt(txtHours) curSalary = CCur(txtHourRate) * CInt(txtHours) lngSalary = CLng(txtHourRate) * CCur(txtHours) DtmReportDate = CDate(Now)

Date and Time Functions

Function Returns Example Result Date Current date of Date Today computer Now Current date and Now Current date time of computer & Time Time Current time of Time Current time computer DateSerial(Date) Date given y m d DateSerial(00,5,31) 05/31/00 DateValue(Date) Date given a date in DateValue(“May 31,00”) 05/31/00 string DateAdd(Date) Date and time after DateAdd(“m”,2,”03/31/00”) 05/31/00 adding the specified date by the specified number of intervals. Year(date) The year of a date Year(Now) 2002 CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Month(date) The month of a date Month(Now) 11 Day(Date) The day of a date Day(Now) 12

Evaluate the following and determine the results. Remember to look at prefixes to determine the argument type. Assume Now is equal to the system date below. TxtDate = “11/12/02” system date = 11/12/02 1:12:20 PM Function Results Month(txtDate) DateSerial(Now) Now DateAdd(“m”, 5, txtDate) DateAdd(“d”, 20, Now) PicDisplay.Print “Month = “ ; Month(txtdate)

String Functions Function Returns Example Result Asc(char) The ASCII value of the character Asc(“A”) 65 Chr(n) The ASCII character represented by Chr(65) A n Replace(s, fs, rs, p,c) A string that has the findstring (fs) Replace(“My “Your replaced with the replacementstring Name”,”MY”,”Your”,1,1) Name” (rs) given a starting position p, and replaced c number of times. Val(s) Converts s to a number Val(“$21.00”) 21.00 Str(n) Converts n into a string Str(65) “65” StrReverse(s) The mirror image of s StrReverse(“AB”) “BA” Lcase(s) All lower case of s Lcase(“DEBY”) “deby” Ucase(s) All upper case of s Ucase(“deby”) “DEBY” Space(n) Generates a string of n blank spaces Space(5) “ “ String(n,char) Creates a string with n repetitive String(3,”A”) “AAA” characters Left(s,n) A subset of s consisting of n Left(“My Name”,2) “MY” characters beginning with the left most character Right(s) A subset of s consisting of n Right(“My Name”,2) “me” characters beginning with the right most character Mid(s,b,n) A string of characters consisting of n mid(“My Name”,2,3) “y N” characters beginning at b position. If n is omitted it returns the remainder InStr(sp,s,m) The position in s where string m InStr(1,“My Name”, ”me”) 6 starts, the comparison begin at sp. 1 is the default of sp InStrRev(s,m,sp) The position in s where string m InStrRev(“My Name” , “me”) 6 starts, the comparison goes backwards from position sp. if omitted it begins at the end of s Len(s) The number of characters in s Len(“My Name”) 7 Ltrim(s) Removes all leading blanks in s LTrim(“ My “) “My “ Rtrim(s) Removes all trailing blanks in s Rtrim(“ My “) “ My” Trim(s) Removes leading and trailing blanks Trim(“ My “) “My” in s CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Evaluate the following and determine the results. Remember to look at prefixes to determine the argument type. Assume Now is equal to the system date below. s = “ This is my favorite class this semester. ” Function Value of strAnswer of intAnswer IntAnswer = Asc(“z”) StrAnswer = Chr(101) StrAnswer = Right(s,8) StrAnswer = Mid(s, 3, 4) StrAnswer = Mid(s,33) StrAnswer = lTrim(s) IntAnswer = Instr(s, “my”) IntAnswer = Instr(1, s, “is”) IntAnswer = Instr(5, s, “is”) IntAnswer = Instr(5, s, “Is”) IntAnswer = RevInstr(s, “is”) IntAnswer = Len(s) PicDisplay.Print String(10, “@”) PicDisplay.Print String(10, “AB”) Ucase(mid(s, 9, 6)) Chr(int(101.33333))

Both the “+” and the “&” can be use for concatenation operations but have different results. Evaluate the following. Dim strAnswer as string Value of strAnswer StrAnswer = “123” + “123” StrAnswer = “123” + 123 StrAnswer = “ABC” + 123 StrAnswer = “123” &”123” StrAnswer = “123” & 123 StrAnswer = “ABC” & 123

The Functions Val(s) and Str(n) Remember Val(s) converts s to a number and Str(n) converts n to a string, However Str(n) will add a leading space to the string. Function Result Val(“03-01-02”) Str(576) Val(“$50.00”) Str(50.00)

What numeric values do the following MsgBox functions display? True is internally coded as a two byte integer with all its bits on &HFFFF which = -1 False is internally coded as a two byte integer will all its bits off &H0000 which = 0 Function Value MsgBox "10 times True is " & 10 * True MsgBox "10 Plus True is " & 10 + True MsgBox "10 times False is " & 10 * False MsgBox "10 Plus False is " & 10 + False CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Functions for determining data types Function Purpose Example Returns IsArray(v) Verify Array IsArray(strName()) True or False IsNumeric(v/o) Verifies content of IsNumeric(txtValue) True or False variable or object IsDate(v/o) Verifies content of IsDate(dtmStartDate) True or False variable or object IsObject(v) Verifies content of IsObject(Customer) True of False variable or object Is Nothing Verifies content has If Customer Is True or False been set to nothing Nothing then TypeOf Checks the type of an If TypeOf txtName is True of false object variable. This TextBox then can be used only in a logical expression TypeName(v) Returns the data type PicDisplay.Print Returns the type name of a variable TypeName(strSalary) String, Integer, etc.

Some interesting VB Constants Constant Value Description

vbBlack &h00 Black

vbRed &hFF Red

vbGreen &hFF00 Green

vbYellow &hFFFF Yellow

vbBlue &hFF0000 Blue

vbMagenta &hFF00FF Magenta

vbCyan &hFFFF00 Cyan

vbWhite &hFFFFFF White

Constant Value Description

vbBinaryCompare 0 Perform a binary comparison.

vbTextCompare 1 Perform a textual comparison

Constant Value Description

vbSunday 1 Sunday

vbMonday 2 Monday

vbTuesday 3 Tuesday CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

vbWednesday 4 Wednesday

vbThursday 5 Thursday

vbFriday 6 Friday

vbSaturday 7 Saturday

vbFirstJan1 1 Use the week in which January 1 occurs (default).

vbFirstFourDays 2 Use the first week that has at least four days in the new year.

vbFirstFullWeek 3 Use the first full week of the year.

vbUseSystem 0 Use the date format contained in the regional settings for your computer.

vbUseSystemDayOfWeek 0 Use the day of the week specified in your system settings for the first day of the week.

Constant Value Description

vbGeneralDate 0 Display a date and/or time. For real numbers, display a date and time. If there is no fractional part, display only a date. If there is no integer part, display time only. Date and time display is determined by your system settings.

vbLongDate 1 Display a date using the long date format specified in your computer's regional settings.

vbShortDate 2 Display a date using the short date format specified in your computer's regional settings.

vbLongTime 3 Display a time using the long time format specified in your computer's regional settings.

vbShortTime 4 Display a time using the short time format specified in your computer's regional settings.

Constant Value Description

vbOKOnly 0 Display OK button only.

vbOKCancel 1 Display OK and Cancel buttons.

vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.

vbYesNoCancel 3 Display Yes, No, and Cancel buttons.

vbYesNo 4 Display Yes and No buttons.

vbRetryCancel 5 Display Retry and Cancel buttons.

vbCritical 16 Display Critical Message icon.

vbQuestion 32 Display Warning Query icon. CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

vbExclamation 48 Display Warning Message icon.

vbInformation 64 Display Information Message icon.

vbDefaultButton1 0 First button is the default.

vbDefaultButton2 256 Second button is the default.

vbDefaultButton3 512 Third button is the default.

vbDefaultButton4 768 Fourth button is the default.

vbApplicationModal 0 Application modal. The user must respond to the message box before continuing work in the current application.

vbSystemModal 4096 System modal. All applications are suspended until the user responds to the message box.

The following constants are used with the MsgBox function to identify which button a user has selected. These constants are only available when your project has an explicit reference to the appropriate type library containing these constant definitions. For VBScript, you must explicitly declare these constants in your code.

Constant Value Description

vbOK 1 OK button was clicked.

vbCancel 2 Cancel button was clicked.

vbAbort 3 Abort button was clicked.

vbRetry 4 Retry button was clicked.

vbIgnore 5 Ignore button was clicked.

vbYes 6 Yes button was clicked.

vbNo 7 No button was clicked.

Constant Value Description

vbCr Chr(13) Carriage return

vbCrLf Chr(13) & Carriage return–linefeed combination Chr(10)

vbFormFeed Chr(12) Form feed; not useful in Microsoft Windows

vbLf Chr(10) Line feed

vbNewLine Chr(13) & Platform-specific newline character; whatever is appropriate for Chr(10) or the platform Chr(10) CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

vbNullChar Chr(0) Character having the value 0

vbNullString String having Not the same as a zero-length string (""); used for calling value 0 external procedures

vbTab Chr(9) Horizontal tab

vbVerticalTab Chr(11) Vertical tab; not useful in Microsoft Windows

Constant Value Description

vbEmpty 0 Uninitialized (default)

vbNull 1 Contains no valid data

vbInteger 2 Integer subtype

vbLong 3 Long subtype

vbSingle 4 Single subtype

vbSingle 5 Double subtype

vbCurrency 6 Currency subtype

vbDate 7 Date subtype

vbString 8 String subtype

vbObject 9 Object

vbError 10 Error subtype

vbBoolean 11 Boolean subtype

vbVariant 12 Variant (used only for arrays of variants)

vbDataObject 13 Data access object

vbDecimal 14 Decimal subtype

vbByte 17 Byte subtype

vbArray 8192 Array

Key Words Write the correct VB statement using the following Key Words. Use strFileName as the file variable KeyWord VB Statement Open (mode output) Explain what the mode does Rewrite the open statement using App.Path Explain what App.Path does Close (both examples) Explain the difference CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Write (strName, curSalary) Use Harry Potter and 500000 as data and show how the data will be stored on the disk. Input (use arguments from the write statement) What do we use the Input statement for? Dim (strName, curSalary) What does the Dim key word do? Const (dtmDate = today) What is the difference between a Constant and a regular variable? What is the difference between an argument and an attribute ByVal write a sub procedure statement for GetSalary and pass the arguments from the Dim statement. Pass the Name ByVal. What does the ByVal keyword do? Static declare strCompany as static How is a static argument different from a regular local argument? Rem or (‘) What is a Rem statement? Where should they be used? Call Write a call statement to call a procedure GetName include the two arguments in the Dim statement Write a user defined Function call statement to GetName and store the results in strCompanyName. Use the arguments from the call statement above.

Logic Structures Structure Statement Simple (If Then) Write an if the condition statement to add 1 to intCounter if sngGrade > 90 If Then Else Write an If then else block that adds 1 to intCounter if sngGrade > 90 or displays a message “You did not receive an A” Select Case selector Write a select case block which checks the value of Day(dtmToday) and displays the name of the day of the week. Only do the first two cases. Do While | Until Condition CSC 110 BUILT IN FUNCTIONS WORK SHEET 4/8/2018

Name ______

Create a loop structure that reads until the EOF(1) and inputs the data into string arrays strName() and strAddress(). What is the conditional difference between the Do Until and the Do While loops? Do loop Until | While condition Create a do loop while structure that checks the user input into txtPassword checking for “Master” For Next [step] Write a for next structure that reads the data from a sequential file and loads it into arrays strName() and strAddress() for 100 records. Use intCustNo as the index. Write a for next structure to read the above data but only store every other record in the arrays.