Built in Functions of VB
Built in Functions of VB
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)
String$( )
It returns a string made up of a specific
number of characters.
E.g. Me.Print String$(5, "*")
InStr( )
InStr( ) returns the location where one string
resides in another string.
E.g. Msgbox InStr(“Have a nice day”, “day”)
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.
Left$( )
Itreturns the leftmost number of
characters specified in a string.
E.g.
Right$( )
Itreturns the rightmost number of
characters specified in a string.
E.g.
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
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.
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
Trim$( )
It removes both leading and trailing spaces
from a string.
E.g.
Msgbox Trim$(“ TYBCA “)
It prints like (TYBCA)
11
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
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
Str$ ( )
It returns a string from a numeric
expression.
E.g. Msgbox “ Value of Pie is : “ + 3.14
14
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
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
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
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
Time( ) / Time$( )
It
returns Current system time.
For example
20
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
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
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
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
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
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
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
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