Visual Basic 6.
0
TYBCA
Topic Name
Built-in Function of VB
1
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
String Function
(1) Format$()
Use : It returns a string in a specified format.
You can pass date, time, number, or string data
to this function, along with instruction on how to
format the string.
E.g. Format$(Now, “General Date”)
Format$(50,”Currency”) O/P : $50
Format$(0,”Yes/No”) O/P : No
(other than 0 value it returns Yes)
Format$(0,”True/False”) O/P : False
(other than 0 value it returns True)
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
String$( )
It returns a string made up of a specific
number of characters.
E.g. Me.Print String$(5, "*")
It prints ( * ) symbol up to five times on the
form.
other examples
Me.Print String$(10,”#”)
Me.Print String$(15,”%”)
3
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
InStr( )
InStr( ) returns the location where one string
resides in another string.
E.g. Msgbox InStr(“Have a nice day”, “day”)
It returns 13 number which is the position
number of day word.
The first argument you typically pass to the
InStr() function is the string you wish to
search within. The next parameter is the
string you wish to search for.
4
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
InStrRev( )
InStrRev() returns the location where one
string resides in another string, but from the
end of the string.
It search the given string from the back
side.
E.g. MsgBox InStrRev ("Microsoft Visual
Basic", "a")
It returns 19 number.
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Left$( )
Itreturns the leftmost number of
characters specified in a string.
E.g.
Me.Print Left$ (“Visual Basic”,6)
It returns first 6 characters ( “Visual” ) at the
left side from specified string.
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Right$( )
Itreturns the rightmost number of
characters specified in a string.
E.g.
Me.Print Right$ (“Visual Basic”,5)
It returns Last 5 characters ( “Basic” ) at the
Right side from specified string.
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Mid$ ( )
It returns a substring of a specified number of
characters within a string.
E.g.
Mid$ (String, Start Location, length)
Mid$ (“Visual Basic 6.0”,5,2)
It returns (al) character from the specified string.
The Mid$() function’s first parameter is the string
you wish to retrieve the some characters from.
The 2nd parameter is the character position you
wish to start with.
The 3rd parameter is how many of the next
characters you want to retrieve.
8
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Len ( )
Itreturns the number of characters in a
string.
E.g. Len ( String )
Msgbox Len (“TYBCA”)
It returns 5 number because TYBCA
string contains total number of characters
are five.
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Replace ( )
The Replace function allows you to
substitute one character (or string) in a
string with another character( or string)
E.g. Replace (String , Find string, Replace
string)
MsgBox Replace ("Visual Basic 5.0",
"5.0", "6.0")
10
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Trim$( )
It removes both leading and trailing spaces
from a string.
E.g.
Msgbox Trim$(“ TYBCA “)
It prints like (TYBCA)
11
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
LTrim$( )
It removes all the spaces at the left side
from the specified string.
E.g. LTrim$ (“ SSCCS”)
It removes all the spaces at the left side
from “ SSCCS”.
12
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
RTrim$( )
It removes all the spaces at the right side
from the specified string.
E.g. RTrim$ (“SSCCS ”)
It removes all the spaces at the right side
from “SSCCS ”.
13
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Str$ ( )
It returns a string from a numeric
expression.
E.g. Msgbox “ Value of Pie is : “ + 3.14
It display error of Type mismatch.
Msgbox “Value of Pie is : “ + Str(3.14)
It doesn’t give error because here now
3.14 number is converted into string and
concatenated with other string.
14
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
UCase$( )
It returns a string in all uppercase letters.
E.g. Msgbox UCase$ (“tybca”)
It display output like TYBCA
LCase$( )
It returns a string in all lowercase letters.
E.g. Msgbox LCase$ (“TYBCA”)
It display output like tybca
15
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Space$ ( )
It returns a specified amount of spaces in
a string.
E.g. Dim StdName As String
StdName = Space$(30)
Msgbox Len (StdName)
It display 30 length of StdName variable.
16
Prepared By : Ashwin R. Dobariya
Built-in Function of VB
Date Function
17
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Now
This function returns a variant that’s a date
/ Time.
It is based on the current system date and
time.
E.g. Dim temp As Date
temp = Now
Me.Print temp
Out Put is : 9/20/2005 12:38:59 PM
18
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Date ( ) / Date$( )
The Date() function returns a variant of type
date.
Date$( ) returns a ten characters string in the
format mm- dd - yyyy.
E.g. Me.Print Date
Out Put : 9/20/2005
It display from international settings in the control
panel.
Me.Print Date$
Out Put : 09-20-2005
19
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Time( ) / Time$( )
It
returns Current system time.
For example
(1) Msgbox Time()
Out put is : 12:47:16 PM
(2) Msgbox Time$()
Out Put is : 12:47:42
20
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
DatePart( )
This function returns the specified part of a data variable.
Use it to return the day, month, year.
Private Sub Command1_Click()
Dim DT As Date
DT = #9/21/2005#
MsgBox DatePart ("d", DT)
MsgBox DatePart ("m", DT)
MsgBox DatePart ("yyyy", DT)
MsgBox DatePart ("q", DT) 'Quarter of year
MsgBox DatePart ("w", DT) 'Current Week number
End Sub
21
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Different Date Format
'For Example Current Date is : 20/09/2005
MsgBox Format (Date, "dd") 'O/p : 20
MsgBox Format (Date, "ddd") 'O/p : Tue
MsgBox Format (Date, "dddd") 'O/p : Tuesday
MsgBox Format(Date, "mm") 'O/p : 09
MsgBox Format(Date, "mmm") 'O/p : Sep
MsgBox Format(Date, "mmmm") 'O/p :
September
MsgBox Format(Date, “yy") 'O/p : 05
MsgBox Format(Date, “yyyy”) 'O/p : 2005
22
Prepared By : Ashwin R. Dobariya
Built-in Function of VB
Date Arithmetic
Function
23
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
DateDiff ( )
This function returns the specified interval of time of time between two
dates.
E.g. Private Sub Command1_Click()
Dim dt1 As Date
Dim dt2 As Date
dt1 = #9/1/2005#
dt2 = #9/30/2005#
MsgBox "Days difference : " & DateDiff ("d", dt1, dt2) O/P : 29
MsgBox "Total Week :" & DateDiff ("w", dt1, dt2) O/P : 4
MsgBox "Total Months : " & DateDiff ("m", dt1, dt2) O/P : 0
End Sub
24
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
DateAdd( )
DateAdd( ) returns a date after adding a specified time
interval to another date.
For example
Private Sub Command1_Click()
Dim temp As Date
temp = #9/21/2005#
MsgBox DateAdd ("d", 2, temp) (Add date)
MsgBox DateAdd ("ww", 2, temp) (Add week)
MsgBox DateAdd ("m", 2, temp) (Add month)
MsgBox DateAdd ("m", -2, temp) (Subtract month)
End Sub
25
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
Conversion Functions
Val( ) returns a numeric value for string
variable.
For example
Msgbox val(Text1.Text)
Msgbox Val(“123”)
CCur()
Itconverts any numeric or string
expression into a currency value.
Msgbox CCur(“5.50”)
26
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
CDbl()
CDbl( ) converts any numeric or string expression into
a double value.
For example
Msgbox CDbl(“12.564545”)
CInt( )
It converts any numeric or string expression into
an integer value
E.g. Msgbox CInt(12.60) O/P : 13
Note : The CInt( ) function does round any
decimal portion.
27
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
CStr( )
It converts any numeric or string
expression into a string value.
E.g. Msgbox CStr(123)
CDate( )
It converts any numeric or string
expression into a date type.
Msgbox CDate (“09/22/2005”)
28
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
IsNumeric ( )
It will returns True if the value passed to it
can be converted to a number.
Dim test
test = 5
MsgBox IsNumeric(test)
It returns True because 5 is a number.
test = “BCA”
MsgBox IsNumeric(test)
It returns False because BCA is a String.
29
Prepared By : Ashwin R. Dobariya
Shree Swaminarayan College of computer Science, Sardarnagar - Bhavnagar
IsDate( )
Itreturns True if the value passed to it is a
valid date.
For example
Private Sub Command1_Click()
Dim test As Date
test = #9/22/2005#
MsgBox IsDate(test) O/P is True
End Sub
30
Prepared By : Ashwin R. Dobariya