Formatting Data and Calling Event Procedures in VB6.0
VISUAL BASIC 6.0
FORMATTING DATA
To Format data
for display, either on the printer or on the screen, use the formatting
functions.
i.The FormatCurrency Function
The FormatCurrency function returns a
string of characters formatted as dollars and cents. By default, the currency value displays a
dollar sign, commas and two positions to the right of the decimal point.
General Format
FormatCurrency(NumericExpressionToFormat)
Example
lblBalance.Caption
= FormatCurrency(curBalance)
Variable |
Value |
Function |
Output |
curBalance |
1275.675 |
Formatcurrency (curBalance) |
$1,275.68 |
sngAmount |
.9 |
Formatcurrency (sngAmount) |
$0.09 |
ii.The FormatNumber Function
The
FormatNumber function is similar to the FormatCurrency function. The default format is determined by your
computer’s regional setting; it will generally display commas and two digits to
the right of the decimal point.
General Format
FormatNumber(Expression-To-Format)
Example
lblsum.Caption
= FormatNumber(curSum)
lblCount.Caption
= FormatNumber(intCount)
Variable |
Value |
Function |
Output |
mcurTotal |
1125.67 |
FormatNumber (mcurTotal, 0) |
1,126 |
curBalance |
1234.567 |
FormatNumber (curBalance, 2) |
1,234.57 |
iii.The FormatPercent Function
To
display numeric values as a percent, use the FormatPercent function. This function multiplies the argument by 100,
adds a percent sign, and rounds to two decimal places.
General Format
FormatPercent
(ExpressionToFunction)
Example
lblPercentComplete.Caption
= FormatPercent(sngComplete)
lblInterestRate.Caption
= FormatPrecent(curRate)
Variable |
Value |
Function |
Output |
curCorrect |
.75 |
FormatPercent (curCorrect) |
75% |
curCorrect |
.75 |
FormatPercent (curCorrect, 1) |
75.0% |
curate |
.734 |
FormatPercent (curRate) |
73% |
curate |
.734 |
FormatPercent (curRate,1) |
73.4% |
curate |
.734 |
Formatpercent (curRate, 2) |
73.40% |
iv.The FormatDateTime Function
To
format an expression as a date and/or time.
The expression may be a string that holds a date or time value, a data
type variable, or a function that returns a date. The named formats use your computer’s
regional settings. If you omit the
optional named format, the function returns the date using vbGeneralDate.
General Format
FormatDateTime(ExpressionToFormat
[ , NamedFormat ] )
Example
lblStartDate.Caption
= FormatDateTime (dtmStartDate, vbShortDate)
lblStartTime.Caption
= FormatDateTime (“1/100”, vbLongDate)
lblDateAndTime.Caption
= FormatDateTime (dtmSomeDate)
The actual values returned by the FormatDateTime
function depend on the regional settings on your computer. These are the return formats based on the USA
defaults.
Named Format |
Returns |
Example |
vbGeneralDate |
A date and/or time.
If the expression holds a date, returns a short date. If it holds a time, returns a long
time. If it holds both, returns both a
short date and long time. |
2/28/99
6:01:24 PM |
vbLongDate |
Day of week, Month Day, Year |
Sunday, February 28,1999 |
vbShortDate |
MM/DD/YY |
2/28/99 |
vbLongTime |
HH:MM:SS AM/PM |
6:01:24 PM |
vbShortTime |
HH:MM (24 hour clock) |
18:01 |
CALLING EVENT PROCEDURES
If you wish to perform a set of instructions in more
than one location, you don’t have to duplicate the code. Write the instructions once, in an event
procedure, and “call” the procedure from another procedure. When you call an event procedure, the
entire procedure is executed and then execution returns to the statement
following the call.
General Format
[Call]
ProcedureName
Example
Text1.text=””
Text2.text=””
Text3.text=””
Text4.text=””
Text5.text=””
End Sub
Whenever you want to clear the content of the form, to
call the procedure clear
Call Clear ( ) or
Clear ( )
Comments
Post a Comment