Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
132 views

Syntax Format (Expression (, Format (, Firstdayofweek (, Firstweekofyear) ) ) )

The document describes various string and date handling functions in Visual Basic. It provides the syntax and examples of functions like Format(), Left(), Right(), Len(), Mid(), InStr(), DateDiff(), DateAdd() and more. These functions allow manipulating, extracting, comparing parts of strings, as well as performing calculations on dates.

Uploaded by

abhinavgupta2010
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Syntax Format (Expression (, Format (, Firstdayofweek (, Firstweekofyear) ) ) )

The document describes various string and date handling functions in Visual Basic. It provides the syntax and examples of functions like Format(), Left(), Right(), Len(), Mid(), InStr(), DateDiff(), DateAdd() and more. These functions allow manipulating, extracting, comparing parts of strings, as well as performing calculations on dates.

Uploaded by

abhinavgupta2010
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Format() “Returns a Variant (String) containing an expression formatted

according to instructions contained in a format expression.

Syntax

Format(expression[, format[, firstdayofweek[, firstweekofyear]]])

Examples:
Dim MyTime, MyDate, MyStr
MyTime = #17:04:23#
MyDate = #January 27, 1993#

' Returns current system time in the system-defined long time format.
MyStr = Format(Time, "Long Time")

' Returns current system date in the system-defined long date format.
MyStr = Format(Date, "Long Date")

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".
' 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 "hello".
MyStr = Format("This is it", ">") ' Returns "THIS IS IT".
Date Handling Functions

Str() : Returns a Variant (String) representation of a number.

Syntax

Str(number)

Trim(0, Ltrim(), Rtrim()

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)

Left() : Returns a Variant (String) containing a specified number of characters


from the left side of a string.

Syntax

Left(string, length)

Right() : Returns a Variant (String) containing a specified number of characters


from the right side of a string.

Syntax

Right(string, length)
Len() : Returns a Long containing the number of characters in a string or the
number of bytes required to store a variable.

Syntax

Len(string | varname)

Mid() : Returns a Variant (String) containing a specified number of characters


from a string.

Syntax

Mid(string, start[, length])

Example:
Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo" ' Create text string.
FirstWord = Mid(MyString, 1, 3) ' Returns "Mid".
LastWord = Mid(MyString, 14, 4) ' Returns "Demo".
MidWords = Mid(MyString, 5) ' Returns "Function Demo".

InStr() : Returns a Variant (Long) specifying the position of the first occurrence
of one string within another.

Syntax

InStr([start, ]string1, string2[, compare])

Examples:

Dim SearchString, SearchChar, MyPos


SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".

' A textual comparison starting at position 4. Returns 6. Fourth parameter


indicates comparison option. 1 means Text and 0 means Binary.
MyPos = Instr(4, SearchString, SearchChar, 1)
' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)

' Comparison is binary by default (last argument is omitted).


MyPos = Instr(SearchString, SearchChar) ' Returns 9.

MyPos = Instr(1, SearchString, "W") ' Returns 0.

Option Compare Binary :Comparison is Case Sensitive (Default)


Option Compare Text : Comparison is NOT Case Sensitive

StrComp() : Returns a Variant (Integer) indicating the result of a string


comparison.

Syntax

StrComp(string1, string2[, compare])

Examples :
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1) ' Returns 1.

Option Compare {Binary | Text }


Binary : Case Sensitive
Text : Not Case Sensitive

StrConv() : Used to convert a string

Syntax
StrConv(String1, Case)

Examples
MsgBox StrConv("SIcsr", vbLowerCase)
MsgBox StrConv("SIcsr", vbUpperCase)
MsgBox StrConv("SyMBiosis comPUTER insTITUTE", vbProperCase)

Mathematical Functions

Val() : Returns the numbers contained in a string as a numeric value of


appropriate type.

Syntax

Val(string)

Round() : Returns a number rounded to a specified number of decimal places.

Syntax

Round(expression [,numdecimalplaces])

Sqr() : Returns a Double specifying the square root of a number.

Syntax

Sqr(number)

Abs() : Returns a value of the same type that is passed to it specifying the
absolute value of a number.
Syntax

Abs(number)

Sgn() : Returns a Variant (Integer) indicating the sign of a number.

Syntax

Sgn(number)

Date Handling Functions

Date : Returns a Variant (Date) containing the current system date.

Syntax

Date

Now () : Returns a Variant (Date) specifying the current date and time according
your computer's system date and time.

Syntax

Now
Day() : Returns a Variant (Integer) specifying a whole number between 1 and
31, inclusive, representing the day of the month.

Syntax

Day(date)

Month() : Returns a Variant (Integer) specifying a whole number between 1 and


12, inclusive, representing the month of the year.

Syntax

Month(date)

Year() : Returns a Variant (Integer) containing a whole number representing the


year.

Syntax

Year(date)

Hour() : Returns a Variant (Integer) specifying a whole number between 0 and


23, inclusive, representing the hour of the day.

Syntax

Hour(time)

Minute() : Returns a Variant (Integer) specifying a whole number between 0 and


59, inclusive, representing the minute of the hour.

Syntax

Minute(time)
Second() : Returns a Variant (Integer) specifying a whole number between 0
and 59, inclusive, representing the second of the minute.

Syntax

Second(time)

Time() : Returns a Variant (Date) indicating the current system time.

Syntax

Time

DateDiff() : Returns a Variant (Long) specifying the number of time intervals


between two specified dates.

Syntax

DateDiff(interval, date1, date2[)

Examples :
Dim TheDate As Date ' Declare variables.
Dim Msg
TheDate = InputBox("Enter a date")
Msg = "Days from today: " & DateDiff("d", Now, TheDate)
MsgBox Msg

Msg = "Months from today: " & DateDiff("m", Now, TheDate)


MsgBox Msg

DateAdd() : s a Variant (Date) containing a date to which a specified time


interval has been added.

Syntax
DateAdd(interval, number, date)

Examples : Dim FirstDate As Date ' Declare variables.


Dim IntervalType As String
Dim Number As Integer
Dim Msg
IntervalType = "m" ' "m" specifies months as interval.
FirstDate = InputBox("Enter a date")
Number = InputBox("Enter number of months to add")
Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)
MsgBox Msg

IntervalType = "d" ' "d" specifies days as interval.


Number = InputBox("Enter number of days to add")
Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)
MsgBox Msg

You might also like