Lesson 20: Format Function in VB2019

Master data formatting for numbers, dates, currencies, and more

Key Takeaway

The Format function is essential for presenting data professionally in VB2019 applications. It allows you to customize the display of numbers, dates, currencies, and percentages.

In the previous lesson, we explored trigonometric functions for geometric calculations. Now we'll learn how to use the Format function to present data in a more readable and professional way. The Format function allows you to control how numbers, dates, times, and text are displayed in your applications.

Format Function Syntax

The basic syntax of the Format function is:

Format(expression, style)

Where expression is the value to format and style defines how the value should be displayed.

20.1 Formatting Numbers

Visual Basic 2019 provides both built-in and custom formatting options for numbers. These allow you to control decimal places, thousands separators, currency symbols, and percentage formats.

Built-in Formats

Predefined formats like Currency, Percent, and Standard

Custom Formats

Create your own formatting patterns

Currency

Format numbers as monetary values

Percentages

Display values as percentages

20.1(a) Built-in Number Formats

VB2019 includes several predefined number formats:

Format Description Example
General Number No thousand separators Format(8972.234, "General Number") → 8972.234
Fixed 2 decimal places, no separators Format(8972.2, "Fixed") → 8972.20
Standard 2 decimal places with separators Format(6648972.265, "Standard") → 6,648,972.27
Currency Currency symbol, separators, 2 decimals Format(6648972.265, "Currency") → $6,648,972.27
Percent Percentage with 2 decimal places Format(0.56324, "Percent") → 56.32%
Form1.vb
Private Sub BtnFormat_Click(sender As Object, e As EventArgs) Handles BtnFormat.Click
    ' Built-in number formatting examples
    LblGeneral.Text = Format(8972.234, "General Number")
    LblFixed.Text = Format(8972.2, "Fixed")
    LblStandard.Text = Format(6648972.265, "Standard")
    LblCurrency.Text = Format(6648972.265, "Currency")
    LblPercent.Text = Format(0.56324, "Percent")
End Sub

20.1(b) Custom Number Formats

You can create your own number formats using placeholders:

Format Description Example
0 Digit placeholder (shows zero if no digit) Format(1234.5, "00000.00") → 01234.50
# Digit placeholder (no leading/trailing zeros) Format(1234.5, "#####.##") → 1234.5
, Thousands separator Format(1234567, "#,##0") → 1,234,567
. Decimal point Format(1234.567, "#,##0.00") → 1,234.57
% Percentage placeholder Format(0.1234, "0.00%") → 12.34%
$ Currency symbol Format(1234.5, "$#,##0.00") → $1,234.50
Form1.vb
Private Sub BtnCustom_Click(sender As Object, e As EventArgs) Handles BtnCustom.Click
    ' Custom number formatting examples
    LblCustom1.Text = Format(1234.5, "00000.00")   ' 01234.50
    LblCustom2.Text = Format(1234.5, "#####.##")   ' 1234.5
    LblCustom3.Text = Format(1234567, "#,##0")     ' 1,234,567
    LblCustom4.Text = Format(1234.567, "#,##0.00") ' 1,234.57
    LblCustom5.Text = Format(0.1234, "0.00%")     ' 12.34%
    LblCustom6.Text = Format(1234.5, "$#,##0.00") ' $1,234.50
End Sub

20.2 Formatting Dates and Times

The Format function provides powerful options for displaying dates and times in various formats.

Built-in Date Formats

VB2019 includes predefined date and time formats:

Format Description Example
General Date Date and time Format(Now, "General Date") → 6/16/2023 2:30:15 PM
Long Date Full date Format(Now, "Long Date") → Friday, June 16, 2023
Short Date Numeric date Format(Now, "Short Date") → 6/16/2023
Long Time Time with seconds Format(Now, "Long Time") → 2:30:15 PM
Short Time Time without seconds Format(Now, "Short Time") → 14:30
Form1.vb
Private Sub BtnDateFormats_Click(sender As Object, e As EventArgs) Handles BtnDateFormats.Click
    ' Built-in date/time formatting
    LblGeneralDate.Text = Format(Now, "General Date")
    LblLongDate.Text = Format(Now, "Long Date")
    LblShortDate.Text = Format(Now, "Short Date")
    LblLongTime.Text = Format(Now, "Long Time")
    LblShortTime.Text = Format(Now, "Short Time")
End Sub
Custom Date Formats

Create your own date and time formats using these placeholders:

Format Description Example
d Day of month (1-31) Format(Now, "d") → 16
dd Day with leading zero (01-31) Format(Now, "dd") → 16
ddd Abbreviated day name (Mon, Tue) Format(Now, "ddd") → Fri
dddd Full day name (Monday) Format(Now, "dddd") → Friday
M Month number (1-12) Format(Now, "M") → 6
MM Month with leading zero (01-12) Format(Now, "MM") → 06
MMM Abbreviated month name (Jan, Feb) Format(Now, "MMM") → Jun
MMMM Full month name Format(Now, "MMMM") → June
yy Two-digit year Format(Now, "yy") → 23
yyyy Four-digit year Format(Now, "yyyy") → 2023
h Hour (1-12) Format(Now, "h") → 2
hh Hour with leading zero (01-12) Format(Now, "hh") → 02
H Hour (0-23) Format(Now, "H") → 14
HH Hour with leading zero (00-23) Format(Now, "HH") → 14
m Minute (0-59) Format(Now, "m") → 30
mm Minute with leading zero (00-59) Format(Now, "mm") → 30
s Second (0-59) Format(Now, "s") → 15
ss Second with leading zero (00-59) Format(Now, "ss") → 15
tt AM/PM designator Format(Now, "tt") → PM
Form1.vb
Private Sub BtnCustomDates_Click(sender As Object, e As EventArgs) Handles BtnCustomDates.Click
    ' Custom date/time formatting
    LblDate1.Text = Format(Now, "MM/dd/yyyy")          ' 06/16/2023
    LblDate2.Text = Format(Now, "dd-MMM-yyyy")        ' 16-Jun-2023
    LblDate3.Text = Format(Now, "MMMM d, yyyy")      ' June 16, 2023
    LblTime1.Text = Format(Now, "hh:mm:ss tt")       ' 02:30:15 PM
    LblTime2.Text = Format(Now, "HH:mm")             ' 14:30
    LblDateTime.Text = Format(Now, "yyyy-MM-dd HH:mm:ss") ' 2023-06-16 14:30:15
End Sub
Real-time Clock

Create a real-time clock using a Timer control:

Form1.vb
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ' Update time displays
    LblClock1.Text = Format(Now, "hh:mm:ss tt")
    LblClock2.Text = Format(Now, "HH:mm")
    LblClock3.Text = Format(Now, "MMM dd, yyyy")
    LblClock4.Text = Format(Now, "dddd, MMMM d, yyyy")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Configure timer
    Timer1.Interval = 1000 ' 1 second
    Timer1.Enabled = True
End Sub

Lesson Summary

In this lesson, you've learned how to use VB2019's Format function to present data professionally:

Number Formatting

Mastered built-in and custom formats for numbers, currencies, and percentages

Date & Time Formatting

Learned to display dates and times in various formats using placeholders

Real-time Display

Created a real-time clock using the Timer control and Format function

Practical Applications

Applied formatting to solve real-world presentation challenges

The Format function is essential for creating professional-looking applications with well-presented data. In the next lesson, we'll learn how to work with Checkbox controls in VB2019.

Next Lesson

Ready to learn about user interface controls? Continue to Lesson 21: Checkbox Controls.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon