6week chapter6 function.ppt

Upload: einstein2d

Post on 14-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 6week Chapter6 Function.ppt

    1/54

    Chapter 6 Functions

    Week: 6

  • 7/27/2019 6week Chapter6 Function.ppt

    2/54

    Objectives

    In this lesson you will learn: MsgBox Function

    InputBox Function

    Math Functions

    Common String-Manipulation Functions

    Time Functions

    RGB Function QBColorFunction

    LoadPicture Function

    Format Function

  • 7/27/2019 6week Chapter6 Function.ppt

    3/54

    MsgBox FunctionDisplays a message in a dialog box, waits for the

    user to click a button, and returns an Integer indicating

    which button the user clicked.Syntax

    MsgBox(prompt[, buttons] [, title])

    PromptRequired. String expression displayed as the

    message in the dialog box.

    If prompt consists of more than one line, you canseparate the lines using a carriage return character

    (Chr(13)), a linefeed character (Chr(10)), or carriage

    return linefeed character combination (Chr(13) &

    Chr(10)) between each line.

  • 7/27/2019 6week Chapter6 Function.ppt

    4/54

    But tonsOptional.

    Numeric expression that is the sum of values

    specifying the number and type of buttons to

    display, the icon style to use, the identity of thedefault button, and the modality of the message

    box.

    If omitted, the default value for buttons is 0.

  • 7/27/2019 6week Chapter6 Function.ppt

    5/54

    The but tonsargument settings are:

    Constant Value DescriptionvbOKOnly 0 OK

    vbOKCancel 1 OK Cancel

    vbAbortRetryIgnore 2 Abort Retry Ignore

    vbYesNoCancel 3 Yes No Cancel

    vbYesNo 4 Yes NovbRetryCancel 5 Retry Cancel

    vbCritical 16 X Critical Message icon

    vbQuestion 32 ? Warning Query iconvbExclamation 48 ! Warning Message icon

    vbInformation 64 i Information Message

    icon

  • 7/27/2019 6week Chapter6 Function.ppt

    6/54

    t i t le

    Optional.String expression displayed in

    the title bar of the dialog box.If you omit title, the application

    name is placed in the title bar.

  • 7/27/2019 6week Chapter6 Function.ppt

    7/54

    Return Values

    Constant Value Description

    vbOK 1 OK

    vbCancel 2 Cancel

    vbAbort 3 Abort

    vbRetry 4 Retry

    vbIgnore 5 Ignore

    vbYes 6 Yes

    vbNo 7 No

  • 7/27/2019 6week Chapter6 Function.ppt

    8/54

    InputBox FunctionDisplays a prompt in a dialog box, waits for

    the user to input text or click a button, andreturns a String containing the contents of

    the text box.

    SyntaxInputBox (prompt[, title] [, default] )

    Prompt

    Required. String expression displayed as the

    message in the dialog box.

  • 7/27/2019 6week Chapter6 Function.ppt

    9/54

    t i t le

    Optional.String expression displayed in

    the title bar of the dialog box.If you omit title, the application

    name is placed in the title bar.

  • 7/27/2019 6week Chapter6 Function.ppt

    10/54

    Default

    Optional.

    String expression displayed in the

    text box as the default response if no

    other input is provided.

    If you omit default, the text box is

    displayed empty.

  • 7/27/2019 6week Chapter6 Function.ppt

    11/54

    Math Functions

    Abs Function

    Returns a value of the same type that ispassed to it specifying the absolute value of

    a number.

    Syntax

    Abs(number)

    To compute the absolute value of a number.The required number argument can be any

    valid numeric expression. If number is an

    un-initialized variable, zero is returned.

  • 7/27/2019 6week Chapter6 Function.ppt

    12/54

    Sgn Function

    Returns a Variant (Integer) indicating the sign of a

    number.

    Syntax:

    Sgn(number)

    The required number argument can be any validnumeric expression.

    Return Values

    If number is Sgn returns

    Greater than zero 1

    Equal to zero 0

    Less than zero -1

  • 7/27/2019 6week Chapter6 Function.ppt

    13/54

    Sin Function

    Returns a Double specifying the sine of an angle.

    Syntax

    Sin(number)

    The required number argument is a Double or any

    valid numeric expression that expresses an angle

    in radians.

    To convert degrees to radians, multiply degrees by

    pi/180. (pi=3.1415926)

    To convert radians to degrees, multiply radians by

    180/pi.

  • 7/27/2019 6week Chapter6 Function.ppt

    14/54

    Cos Function

    Returns a Double specifying the cosine of

    an angle.

    Syntax

    Cos(number)

    The required number argument is a Double

    or any valid numeric expression that

    expresses an angle in radians.

  • 7/27/2019 6week Chapter6 Function.ppt

    15/54

    Atn Function

    Returns a Double specifying the arctangentof a number.

    Syntax

    Atn(number)

    The required number argument is a Doubleor any valid numeric expression.

    The Atn function takes the ratio of two sidesof a right triangle (number) and returns the

    corresponding angle in radians.The ratio is the length of the side oppositethe angle divided by the length of the sideadjacent to the angle.

  • 7/27/2019 6week Chapter6 Function.ppt

    16/54

    Sqr Function

    Returns a Double specifying thesquare root of a number.

    SyntaxSqr(number)

    The required number argument is aDouble or any valid numeric

    expression greater than or equal to

    zero.

  • 7/27/2019 6week Chapter6 Function.ppt

    17/54

    Example:

    MySqr = Sqr(4) ' Returns 2.MySqr = Sqr(23)

    ' Returns 4.79583152331272.MySqr = Sqr(0) ' Returns 0.MySqr = Sqr(-4)

    ' Generates a error.

  • 7/27/2019 6week Chapter6 Function.ppt

    18/54

    Int, Fix Functions

    Returns the integer portion of a number.

    Syntax

    Int(number)

    Fix(number)

    The required number argument is a Double or

    any valid numeric expression.

    MyNumber = Int(99.8) ' Returns 99MyNumber = Fix(99.2) ' Returns 99

    MyNumber = Int(-99.8) ' Returns -100

    MyNumber = Fix(-99.8) ' Returns -99

  • 7/27/2019 6week Chapter6 Function.ppt

    19/54

    Exp Function

    Returns a Double specifying e (the baseof natural logarithms) raised to a power.

    Syntax

    Exp(number)

    The required number argument is a

    Double or any valid numeric expression.

  • 7/27/2019 6week Chapter6 Function.ppt

    20/54

    Log Function

    Returns a Double specifying the natural

    logarithm of a number.

    Syntax

    Log(number)

    The required number argument is a

    Double or any valid numeric expression

    greater than zero.

    Ln(x) Log(x)

  • 7/27/2019 6week Chapter6 Function.ppt

    21/54

    The natural logarithm is the logarithm to the base e.

    The constant e is approximately 2.718282.

    You can calculate base-n logarithms for anynumber x by dividing the natural logarithm of x by

    the natural logarithm of n as follows:

    Logn(x) = Log(x) / Log(n)

    Lg(x) Log(x)/Log(10)

  • 7/27/2019 6week Chapter6 Function.ppt

    22/54

    Rnd Function

    Returns a Single containing a random

    number.

    Syntax

    Rnd[(number)]

    The optional number argument is a Single

    or any valid numeric expression.

  • 7/27/2019 6week Chapter6 Function.ppt

    23/54

    Return Values

    If number is Rnd generates

    Less than zero The same number every time,

    using number as the seed.

    Greater than zero The next random number inthe sequence.

    Equal to zero The most recently generated

    number.

    Not supplied The next random number in

    the sequence

  • 7/27/2019 6week Chapter6 Function.ppt

    24/54

    The Rnd function returns a value less

    than 1 but greater than or equal to zero.0

  • 7/27/2019 6week Chapter6 Function.ppt

    25/54

    To produce random integers in a

    given range, use this formula:

    Int((upperbound - lowerbound + 1) * Rnd +

    lowerbound)

    Here, upperbound is the highestnumber in the range, and

    lowerbound is the lowest number in

    the range.Generate random value between

    1 and 6.

    MyValue = Int((6 * Rnd) + 1)

  • 7/27/2019 6week Chapter6 Function.ppt

    26/54

    Common String-Manipulation Functions

    Left(string, length)

    Righ t(str ing , leng th)

    Mid(s tr ing , start [ , leng th ])

    Left(abcdef, 3) abc

    Right(abcdef, 3) defMid(abcdef, 3,2) cd

    Mid(abcdef, 3) cdef

  • 7/27/2019 6week Chapter6 Function.ppt

    27/54

    Instr Function

    Returns a Variant (Long) specifying the

    position of the first occurrence of one string

    within another.

    SyntaxInStr([start, ]string1, string2)

  • 7/27/2019 6week Chapter6 Function.ppt

    28/54

    start

    Optional. Numeric expression that sets the

    starting position for each search. If omitted, searchbegins at the first character position.

    string1

    Required. String expression being searched.

    string2

    Required. String expression sought.

  • 7/27/2019 6week Chapter6 Function.ppt

    29/54

    Return Values

    If InStr returns

    string1 is zero-length 0

    string2 is zero-length startstring2 is not found 0

    string2 is found within string1 Position

    at which match is found

    start > string2 0

  • 7/27/2019 6week Chapter6 Function.ppt

    30/54

    LTrim, RTrim, and Trim Functions

    Returns a Variant (String) containing a copy of a

    specified string without leading spaces (LTrim),trailing spaces (RTrim), or both leading and

    trailing spaces (Trim).

    Syntax

    LTrim(string)

    RTrim(string)

    Trim(string)

    The required string argument is any valid string

    expression.

    If string contains Null, Null is returned.

  • 7/27/2019 6week Chapter6 Function.ppt

    31/54

    Len Function

    The Len function tells you how many characters

    a String contains.

    Syntax

    Len(string)

    The Len function syntax has this argument:

    string

    Any valid string expression. If string containsNull, Null is returned.

    Asc Function

  • 7/27/2019 6week Chapter6 Function.ppt

    32/54

    Asc Function

    Returns an Integer representing the character

    code corresponding to the first letter in a string.

    Syntax

    Asc(string)

    The required string argument is any valid stringexpression.

    If the string contains no characters, a run-time

    error occurs.MyNumber = Asc("A") ' Returns 65

    MyNumber = Asc("a") ' Returns 97

    M Number = Asc "A le" ' Returns 65

  • 7/27/2019 6week Chapter6 Function.ppt

    33/54

    Chr Function

    Returns a String containing the character associated with

    the specified character code.

    Syntax

    Chr(charcode)

    The required charcode argument is a Long that identifies acharacter.

    Remarks

    Numbers from 0-31 are the same as standard, nonprintableASCII codes.

    For example, Chr(10) returns a linefeed character.

    The normal range for charcode is 0-255.

  • 7/27/2019 6week Chapter6 Function.ppt

    34/54

    MyChar = Chr(65) ' Returns AMyChar = Chr(97) ' Returns aMyChar = Chr(48) ' Returns 0

  • 7/27/2019 6week Chapter6 Function.ppt

    35/54

    LCase Function

    Returns a String that has been converted to

    lowercase.

    Syntax

    LCase(string)

    The required string argument is any valid string

    expression. If string contains Null, Null is returned.

    RemarksOnly uppercase letters are converted to

    lowercase; all lowercase letters and nonletter

    characters remain unchanged.

  • 7/27/2019 6week Chapter6 Function.ppt

    36/54

    UCase Function

    Returns a Variant (String) containing the specifiedstring, converted to uppercase.

    Syntax

    UCase(string)

    The required string argument is any valid string

    expression.

    Remarks

    Only lowercase letters are converted to

    uppercase; all uppercase letters and nonletter

    characters remain unchanged.

  • 7/27/2019 6week Chapter6 Function.ppt

    37/54

    x$ = "Hello World 1234"Y$ = Lcase(x$)' Returns "hello world 1234"

    Z$ = UCase(x$)

    ' Returns "HELLO WORLD 1234"

  • 7/27/2019 6week Chapter6 Function.ppt

    38/54

    Val Function

    Returns the numbers contained in astring as a numeric value of appropriate

    type.

    Syntax

    Val(string)

    The required string argument is any validstring expression.

  • 7/27/2019 6week Chapter6 Function.ppt

    39/54

    The Val function stops reading the

    string at the first character it can'trecognize as part of a number.

    MyValue = Val("2457") ' Returns 2457

    MyValue = Val(" 2 45 7") ' Returns 2457

    MyValue = Val("24 and 57") ' Returns 24

  • 7/27/2019 6week Chapter6 Function.ppt

    40/54

    Str Function

    Returns a Variant (String) representation

    of a number.

    Syntax

    Str(number)

    The required number argument is a Long

    containing any valid numeric expression.

    MyString = Str(459) ' Returns " 459"

    MyString = Str(-459.65) ' Returns "-459.65"

    MyString = Str(459.001) ' Returns " 459.001"

  • 7/27/2019 6week Chapter6 Function.ppt

    41/54

    String Function

    Returns a Variant (String) containing a repeating character

    string of the length specified.

    Syntax

    String(number, character)

    The String function syntax has these named arguments:

    number

    Required; Long. Length of the returned string.

    character

    Required; Variant. Character code specifying the character

    or string expression whose first character is used to build

    the return string.

  • 7/27/2019 6week Chapter6 Function.ppt

    42/54

    MyString = String(5, "*")

    ' Returns "*****"MyString = String(5, 42)

    ' Returns "*****"

    MyString = String(10, "ABC")' Returns AAAAAAAAAA"

  • 7/27/2019 6week Chapter6 Function.ppt

    43/54

    StrReverse Function

    Returns a string in which the character order

    of a specified string is reversed.

    Syntax

    StrReverse(expression)The expression argument is the string whose

    characters are to be reversed.

    If expression is a zero-length string (""), azero-length string is returned.

    StrReverse(abcdefGH) HGfedcba

  • 7/27/2019 6week Chapter6 Function.ppt

    44/54

    Time Functions

    Now Function

    Returns a Variant (Date) specifying the currentdate and time according your computer's system

    date and time.

    Syntax

    Now

    Example:

    Dim Today

    Today = Now ' Assign current system date and

    time.

    Time Function

  • 7/27/2019 6week Chapter6 Function.ppt

    45/54

    Time Function

    Returns a Variant (Date) indicating the current

    system time.

    Syntax

    Time

    Time Function Example

    This example uses the Time function to return

    the current system time.

    Dim MyTime

    MyTime = Time

    ' Return current system time.

  • 7/27/2019 6week Chapter6 Function.ppt

    46/54

    RGB Function

    Returns a Long whole number

    representing an RGB color value.

    Syntax

    RGB(red, green, blue)

  • 7/27/2019 6week Chapter6 Function.ppt

    47/54

    red

    Required; Variant (Integer). Number in the

    range 0-255, inclusive, that represents thered component of the color.

    Green

    Required; Variant (Integer). Number in the

    range 0-255, inclusive, that represents the

    green component of the color.

    Blue

    Required; Variant (Integer). Number in therange 0-255, inclusive, that represents the

    blue component of the color.

  • 7/27/2019 6week Chapter6 Function.ppt

    48/54

    LoadPicture Function

    Loads a graphic into a forms Picture property, a

    PictureBox control, or an Image control.Syntax

    LoadPicture([filename])

    Filename

    Optional. String expression specifying a filename.

    Can include folder and drive.

    If no filename is specified LoadPicture clears the

    Image or PictureBox control.

  • 7/27/2019 6week Chapter6 Function.ppt

    49/54

    Format Function

    Returns a Variant (String)containing an expression formatted

    according to instructions contained

    in a format expression.

    Syntax

    Format(expression[, format])

  • 7/27/2019 6week Chapter6 Function.ppt

    50/54

    Expression

    Required. Any valid expression.

    Format

    Optional. A valid named or user-defined format expression.

  • 7/27/2019 6week Chapter6 Function.ppt

    51/54

    MyTime = #17:04:23#

    MyDate = #January 27, 1993#

    MyStr = Format(MyTime, "h:m:s")

    ' Returns "17:4:23".

    MyStr = Format(MyTime, "hh:mm:ss AMPM")

    ' Returns "05:04:23 PM".

    MyStr = Format(MyDate, "dddd, mmm d yyyy")' Returns "Wednesday, Jan 27 1993".

  • 7/27/2019 6week Chapter6 Function.ppt

    52/54

    ' If format is not supplied, a string is returned.

    MyStr = Format(23) ' Returns "23".

    ' User-defined formats.

    MyStr = Format(5459.4, "##,##0.00")

    ' Returns "5,459.40".

    MyStr = Format(334.9, "###0.00")' Returns "334.90".

    MyStr = Format(5, "0.00%")

    ' Returns "500.00%".

    MyStr = Format("HELLO", "")

    ' Returns "THIS IS IT".

    S

  • 7/27/2019 6week Chapter6 Function.ppt

    53/54

    Summary MsgBox Function InputBox Function

    Math Functions Abs , Sgn, Sin ,Cos ,Atn ,Sqr ,Int, Fix,Exp ,Log ,

    Rnd

    Common String-Manipulation Functions

    Left ,Right, Mid , Instr , LTrim, RTrim, Trim ,Len , Asc ,Chr

    LCase, UCase, Val, Str, String,StrReverse

    Time Functions Now ,Time

    Other Functions

    RGB ,LoadPicture,Format

  • 7/27/2019 6week Chapter6 Function.ppt

    54/54

    Exercises in the practice lessonPage 148-149:

    Exercise:1,2,4,5,6

    Homework

    Page 147-148:

    Question: 1 to 9