built in functions of vb

30
1 Visual Basic 6.0 Visual Basic 6.0 TYBCA TYBCA Topic Name Topic Name Built-in Function Built-in Function of VB of VB

Upload: stavan-desai

Post on 22-Nov-2014

405 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Built in Functions of VB

11

Visual Basic 6.0Visual Basic 6.0TYBCATYBCA

Topic NameTopic Name Built-in Function of VBBuilt-in Function of VB

Page 2: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 22

String FunctionString Function(1) Format$()(1) Format$() Use : It returns a string in a specified format.Use : It returns a string in a specified format. You can pass date, time, number, or string data You can pass date, time, number, or string data

to this function, along with instruction on how to to this function, along with instruction on how to format the string.format the string.

E.g. Format$(Now, “General Date”)E.g. Format$(Now, “General Date”) Format$(50,”Currency”) Format$(50,”Currency”) O/P : $50 Format$(0,”Yes/No”) Format$(0,”Yes/No”) O/P : No

(other than 0 value it returns Yes) Format$(0,”True/False”) Format$(0,”True/False”) O/P : False

(other than 0 value it returns True)

Page 3: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 33

String$( )String$( ) It returns a string made up of a specific It returns a string made up of a specific

number of characters.number of characters. E.g. Me.Print String$(5, "*")E.g. Me.Print String$(5, "*")

It prints ( * ) symbol up to five times on the It prints ( * ) symbol up to five times on the form.form.

other examplesother examples Me.Print String$(10,”#”)Me.Print String$(10,”#”) Me.Print String$(15,”%”)Me.Print String$(15,”%”)

Page 4: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 44

InStr( )InStr( ) InStr( ) returns the location where one string InStr( ) returns the location where one string

resides in another string.resides in another string. E.g. Msgbox InStr(“Have a nice E.g. Msgbox InStr(“Have a nice day”, “day”)”, “day”)

It returns 13 number which is the position It returns 13 number which is the position number of day word.number of day word.

The first argument you typically pass to the The first argument you typically pass to the InStr() function is the string you wish to InStr() function is the string you wish to search within. The next parameter is the search within. The next parameter is the string you wish to search for.string you wish to search for.

Page 5: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 55

InStrRev( )InStrRev( ) InStrRev() returns the location where one InStrRev() returns the location where one

string resides in another string, but from the string resides in another string, but from the end of the string.end of the string.

It search the given string from the back It search the given string from the back side.side.

E.g. MsgBox InStrRev ("Microsoft Visual E.g. MsgBox InStrRev ("Microsoft Visual BBasic", "a")sic", "a")

It returns 19 number.It returns 19 number.

Page 6: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 66

Left$( )Left$( ) It returns the leftmost number of It returns the leftmost number of

characters specified in a string.characters specified in a string. E.g.E.g.

Me.Print Left$ (“Visual Basic”,6)Me.Print Left$ (“Visual Basic”,6)

It returns first 6 characters ( “Visual” ) at the It returns first 6 characters ( “Visual” ) at the left side from specified string.left side from specified string.

Page 7: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 77

Right$( )Right$( ) It returns the rightmost number of It returns the rightmost number of

characters specified in a string.characters specified in a string. E.g.E.g.

Me.Print Right$ (“Visual Basic”,5)Me.Print Right$ (“Visual Basic”,5)

It returns Last 5 characters ( “Basic” ) at the It returns Last 5 characters ( “Basic” ) at the Right side from specified string.Right side from specified string.

Page 8: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 88

Mid$ ( )Mid$ ( ) It returns a substring of a specified number of It returns a substring of a specified number of

characters within a string.characters within a string. E.g.E.g.

Mid$ (String, Start Location, length)Mid$ (String, Start Location, length)Mid$ (“Visual Basic 6.0”,5,2)Mid$ (“Visual Basic 6.0”,5,2)

It returns (al) character from the specified string.It returns (al) character from the specified string. The Mid$() function’s first parameter is the string The Mid$() function’s first parameter is the string

you wish to retrieve the some characters from. you wish to retrieve the some characters from. The 2The 2ndnd parameter is the character position you parameter is the character position you

wish to start with.wish to start with. The 3The 3rdrd parameter is how many of the next parameter is how many of the next

characters you want to retrieve.characters you want to retrieve.

Page 9: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 99

Len ( )Len ( ) It returns the number of characters in a It returns the number of characters in a

string.string. E.g. Len ( String )E.g. Len ( String ) Msgbox Len (“TYBCA”)Msgbox Len (“TYBCA”) It returns 5 number because TYBCA It returns 5 number because TYBCA

string contains total number of characters string contains total number of characters are five.are five.

Page 10: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1010

Replace ( )Replace ( ) The Replace function allows you to The Replace function allows you to

substitute one character (or string) in a substitute one character (or string) in a string with another character( or string)string with another character( or string)

E.g. Replace (String , Find string, Replace E.g. Replace (String , Find string, Replace string)string)

MsgBox Replace ("Visual Basic 5.0", MsgBox Replace ("Visual Basic 5.0", "5.0", "6.0")"5.0", "6.0")

Page 11: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1111

Trim$( )Trim$( ) It removes both leading and trailing spaces It removes both leading and trailing spaces

from a string.from a string. E.g.E.g. Msgbox Trim$(“ TYBCA “)Msgbox Trim$(“ TYBCA “) It prints like (TYBCA)It prints like (TYBCA)

Page 12: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1212

LTrim$( )LTrim$( ) It removes all the spaces at the left side It removes all the spaces at the left side

from the specified string.from the specified string. E.g. LTrim$ (“ SSCCS”)E.g. LTrim$ (“ SSCCS”) It removes all the spaces at the left side It removes all the spaces at the left side

from “ SSCCS”.from “ SSCCS”.

Page 13: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1313

RTrim$( )RTrim$( ) It removes all the spaces at the right side It removes all the spaces at the right side

from the specified string.from the specified string. E.g. RTrim$ (“SSCCS ”)E.g. RTrim$ (“SSCCS ”) It removes all the spaces at the right side It removes all the spaces at the right side

from “SSCCS ”.from “SSCCS ”.

Page 14: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1414

Str$ ( )Str$ ( ) It returns a string from a numeric It returns a string from a numeric

expression.expression. E.g. Msgbox “ Value of Pie is : “ + 3.14E.g. Msgbox “ Value of Pie is : “ + 3.14

It display error of Type mismatch.It display error of Type mismatch. Msgbox “Value of Pie is : “ + Str(3.14)Msgbox “Value of Pie is : “ + Str(3.14) It doesn’t give error because here now It doesn’t give error because here now

3.14 number is converted into string and 3.14 number is converted into string and concatenated with other string.concatenated with other string.

Page 15: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1515

UCase$( )UCase$( ) It returns a string in all uppercase letters.It returns a string in all uppercase letters. E.g. Msgbox UCase$ (“tybca”)E.g. Msgbox UCase$ (“tybca”) It display output like TYBCAIt display output like TYBCA

LCase$( )LCase$( ) It returns a string in all lowercase letters.It returns a string in all lowercase letters. E.g. Msgbox LCase$ (“TYBCA”)E.g. Msgbox LCase$ (“TYBCA”) It display output like tybcaIt display output like tybca

Page 16: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1616

Space$ ( )Space$ ( )

It returns a specified amount of spaces in It returns a specified amount of spaces in a string.a string.

E.g. Dim StdName As StringE.g. Dim StdName As StringStdName = Space$(30)StdName = Space$(30)Msgbox Len (StdName)Msgbox Len (StdName)

It display 30 length of StdName variable.It display 30 length of StdName variable.

Page 17: Built in Functions of VB

1717

Built-in Function of VBBuilt-in Function of VB

Date FunctionDate Function

Page 18: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1818

NowNow This function returns a variant that’s a date This function returns a variant that’s a date

/ Time./ Time. It is based on the current system date and It is based on the current system date and

time.time.E.g. Dim temp As DateE.g. Dim temp As Date temp = Nowtemp = Now Me.Print tempMe.Print tempOut Put is : 9/20/2005 12:38:59 PM Out Put is : 9/20/2005 12:38:59 PM

Page 19: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 1919

Date ( ) / Date$( )Date ( ) / Date$( ) The Date() function returns a variant of type The Date() function returns a variant of type

date.date. Date$( ) returns a ten characters string in the Date$( ) returns a ten characters string in the

format mm- dd - yyyy.format mm- dd - yyyy. E.g. Me.Print Date E.g. Me.Print Date

Out Put : 9/20/2005 Out Put : 9/20/2005 It display from international settings in the control It display from international settings in the control

panel.panel. Me.Print Date$Me.Print Date$

Out Put : 09-20-2005Out Put : 09-20-2005

Page 20: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2020

Time( ) / Time$( )Time( ) / Time$( ) It returns Current system time.It returns Current system time. For exampleFor example

(1) Msgbox Time()(1) Msgbox Time() Out put is : 12:47:16 PM Out put is : 12:47:16 PM

(2) Msgbox Time$()(2) Msgbox Time$() Out Put is : 12:47:42Out Put is : 12:47:42

Page 21: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2121

DatePart( )DatePart( ) This function returns the specified part of a data variable. This function returns the specified part of a data variable.

Use it to return the day, month, year.Use it to return the day, month, year. Private Sub Command1_Click()Private Sub Command1_Click() Dim DT As DateDim DT As Date DT = #9/21/2005# DT = #9/21/2005# MsgBox DatePart ("d", DT)MsgBox DatePart ("d", DT) MsgBox DatePart ("m", DT)MsgBox DatePart ("m", DT) MsgBox DatePart ("yyyy", DT)MsgBox DatePart ("yyyy", DT) MsgBox DatePart ("q", DT) 'Quarter of yearMsgBox DatePart ("q", DT) 'Quarter of year MsgBox DatePart ("w", DT) 'Current Week numberMsgBox DatePart ("w", DT) 'Current Week number End SubEnd Sub

Page 22: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2222

Different Date FormatDifferent Date Format 'For Example Current Date is : 20/09/2005'For Example Current Date is : 20/09/2005 MsgBox Format (Date, "dd") 'O/p : 20MsgBox Format (Date, "dd") 'O/p : 20 MsgBox Format (Date, "ddd") 'O/p : TueMsgBox Format (Date, "ddd") 'O/p : Tue MsgBox Format (Date, "dddd") 'O/p : TuesdayMsgBox Format (Date, "dddd") 'O/p : Tuesday

MsgBox Format(Date, "mm") 'O/p : 09MsgBox Format(Date, "mm") 'O/p : 09 MsgBox Format(Date, "mmm") 'O/p : SepMsgBox Format(Date, "mmm") 'O/p : Sep MsgBox Format(Date, "mmmm") 'O/p : MsgBox Format(Date, "mmmm") 'O/p :

SeptemberSeptember MsgBox Format(Date, “yy") 'O/p : 05MsgBox Format(Date, “yy") 'O/p : 05 MsgBox Format(Date, “yyyy”) 'O/p : 2005MsgBox Format(Date, “yyyy”) 'O/p : 2005

Page 23: Built in Functions of VB

2323

Built-in Function of VBBuilt-in Function of VB

Date Arithmetic Date Arithmetic FunctionFunction

Page 24: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2424

DateDiff ( )DateDiff ( ) This function returns the specified interval of time of time between two This function returns the specified interval of time of time between two

dates.dates. E.g. Private Sub Command1_Click()E.g. Private Sub Command1_Click() Dim dt1 As DateDim dt1 As Date Dim dt2 As DateDim dt2 As Date dt1 = #9/1/2005#dt1 = #9/1/2005# dt2 = #9/30/2005#dt2 = #9/30/2005# MsgBox "Days difference : " & DateDiff ("d", dt1, dt2) MsgBox "Days difference : " & DateDiff ("d", dt1, dt2) O/P : 29 MsgBox "Total Week :" & DateDiff ("w", dt1, dt2) MsgBox "Total Week :" & DateDiff ("w", dt1, dt2) O/P : 4 MsgBox "Total Months : " & DateDiff ("m", dt1, dt2) MsgBox "Total Months : " & DateDiff ("m", dt1, dt2) O/P : 0 End SubEnd Sub

Page 25: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2525

DateAdd( )DateAdd( ) DateAdd( ) returns a date after adding a specified time DateAdd( ) returns a date after adding a specified time

interval to another date.interval to another date. For exampleFor example Private Sub Command1_Click()Private Sub Command1_Click() Dim temp As DateDim temp As Date temp = #9/21/2005#temp = #9/21/2005# MsgBox DateAdd ("d", 2, temp) (Add date)MsgBox DateAdd ("d", 2, temp) (Add date) MsgBox DateAdd ("ww", 2, temp) (Add week)MsgBox DateAdd ("ww", 2, temp) (Add week) MsgBox DateAdd ("m", 2, temp) (Add month)MsgBox DateAdd ("m", 2, temp) (Add month) MsgBox DateAdd ("m", -2, temp) (Subtract month)MsgBox DateAdd ("m", -2, temp) (Subtract month) End SubEnd Sub

Page 26: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2626

Conversion FunctionsConversion Functions Val( ) returns a numeric value for string returns a numeric value for string

variable.variable. For exampleFor example Msgbox val(Text1.Text)Msgbox val(Text1.Text) Msgbox Val(“123”)Msgbox Val(“123”)

CCur() It converts any numeric or string It converts any numeric or string

expression into a currency value.expression into a currency value. Msgbox CCur(“5.50”)Msgbox CCur(“5.50”)

Page 27: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2727

CDbl() CDbl( ) converts any numeric or string expression into CDbl( ) converts any numeric or string expression into

a double value.a double value. For exampleFor example

Msgbox CDbl(“12.564545”)Msgbox CDbl(“12.564545”) CInt( ) It converts any numeric or string expression into It converts any numeric or string expression into

an integer valuean integer value E.g. Msgbox CInt(12.60) O/P : 13E.g. Msgbox CInt(12.60) O/P : 13 Note : The CInt( ) function does round any Note : The CInt( ) function does round any

decimal portion.decimal portion.

Page 28: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2828

CStr( )CStr( ) It converts any numeric or string It converts any numeric or string

expression into a string value.expression into a string value. E.g. Msgbox CStr(123)E.g. Msgbox CStr(123) CDate( )CDate( ) It converts any numeric or string It converts any numeric or string

expression into a date type.expression into a date type. Msgbox CDate (“09/22/2005”)Msgbox CDate (“09/22/2005”)

Page 29: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 2929

IsNumeric ( )IsNumeric ( ) It will returns True if the value passed to it It will returns True if the value passed to it

can be converted to a number.can be converted to a number. Dim testDim test test = 5test = 5 MsgBox IsNumeric(test)MsgBox IsNumeric(test) It returns True because 5 is a number.It returns True because 5 is a number. test = “BCA”test = “BCA” MsgBox IsNumeric(test)MsgBox IsNumeric(test) It returns False because BCA is a String.It returns False because BCA is a String.

Page 30: Built in Functions of VB

Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar

Prepared By : Ashwin R. Dobariya 3030

IsDate( )IsDate( ) It returns True if the value passed to it is a It returns True if the value passed to it is a

valid date.valid date. For exampleFor examplePrivate Sub Command1_Click()Private Sub Command1_Click() Dim test As DateDim test As Date test = #9/22/2005#test = #9/22/2005# MsgBox IsDate(test) MsgBox IsDate(test) O/P is TrueO/P is TrueEnd SubEnd Sub