Lesson 12: Mastering Formatting Functions in VB6
Learn to use Tab, Space, and Format functions for precise text and number formatting
Key Takeaway
Formatting functions in VB6 provide powerful tools to control how text and numbers are displayed, making your applications more professional and user-friendly.
Welcome to Lesson 12 of our Visual Basic 6 Tutorial! In this lesson, you'll master VB6's essential formatting functions. These functions are crucial for creating applications that display data in a clear, organized, and visually appealing way.
12.1 The Tab Function
The Tab function controls the horizontal positioning of output in your applications. It positions text at a specific column from the left border of the output form.
Important Note
Always include a semicolon between Tab and the items you want to display. VB6 will automatically handle the spacing based on the specified column position.
Example 12.1: Using the Tab Function
Private Sub Form_Activate() Print "I"; Tab(5); "like"; Tab(10); "to"; Tab(15); "learn"; Tab(20); "VB" Print Print Tab(10); "I"; Tab(15); "like"; Tab(20); "to"; Tab(25); "learn"; Tab(30); "VB" Print Print Tab(15); "I"; Tab(20); "like"; Tab(25); "to"; Tab(30); "learn"; Tab(35); "VB" End Sub
Tab Function Output:
Figure 12.1: Output of the Tab function demonstration
12.2 The Space Function
The Space function is similar to Tab but specifies the exact number of spaces between items rather than absolute positions.
Example 12.2: Using the Space Function
Private Sub Form_Activate() Print "Visual"; Space(10); "Basic" End Sub
Space Function Output:
12.3 The Format Function
The Format function is a powerful tool for formatting numbers, dates, and strings in VB6. It has both predefined and custom formatting options.
1 Predefined Formats
VB6 provides several built-in format styles for numbers:
| Style | Description | Example |
|---|---|---|
| General Number | No thousands separators | 8972.234 → 8972.234 |
| Fixed | Two decimal places | 8972.2 → 8972.23 |
| Standard | Thousands separators | 6648972.265 → 6,648,972.27 |
| Currency | With currency symbol | 6648972.265 → $6,648,972.27 |
| Percent | Percentage format | 0.56324 → 56.32% |
2 Custom Formats
Create your own formats with special characters:
| Format | Description | Example |
|---|---|---|
| 0 | Digit placeholder | Format(123.45, "000.00") → 123.45 |
| # | Digit placeholder (optional) | Format(123.45, "###.##") → 123.45 |
| . | Decimal point | Format(1234, "#,##0.00") → 1,234.00 |
| , | Thousands separator | Format(1234567, "#,##0") → 1,234,567 |
| % | Percentage placeholder | Format(0.123, "0.00%") → 12.30% |
Example 12.3: Predefined Format Functions
Private Sub Form_Activate() Print Format(8972.234, "General Number") Print Format(8972.2, "Fixed") Print Format(6648972.265, "Standard") Print Format(6648972.265, "Currency") Print Format(0.56324, "Percent") End Sub
Figure 12.2: Output of predefined format functions
Example 12.4: User-Defined Format Functions
Private Sub Form_Activate() Print Format(781234.57, "0") Print Format(781234.57, "0.0") Print Format(781234.576, "0.00") Print Format(781234.576, "#,##0.00") Print Format(781234.576, "$#,##0.00") Print Format(0.576, "0%") Print Format(0.5768, "0.00%") End Sub
Format Function Output:
Format Function Simulator
Test different formatting options with custom inputs
Lesson Summary
In this lesson, you've mastered VB6's essential formatting functions:
Tab Function
Controls horizontal positioning of output
Space Function
Inserts a specified number of spaces between items
Format Function
Powerful tool for formatting numbers, dates, and strings
Predefined Formats
Built-in styles like Currency, Percent, and Fixed
Custom Formats
Create your own formatting patterns using special characters
Best Practice
Use formatting functions to make your application's output more readable and professional. Formatting is especially important for financial applications where currency and percentage formats are essential.
Practice Exercises
Test your understanding of VB6 formatting functions with these exercises:
Exercise 1: Product List Formatter
Create a program that displays a product list with aligned columns using the Tab function. The list should include product names, prices, and quantities.
Exercise 2: Financial Report Generator
Generate a financial report with proper currency formatting, percentages, and thousands separators. Include columns for revenue, expenses, and profit margin.
Exercise 3: Temperature Converter
Create a temperature converter that converts between Celsius and Fahrenheit. Format the output with one decimal place and include the degree symbol.
Exercise 4: Custom Invoice Generator
Design an invoice generator that formats prices with currency symbols, aligns columns with Tab, and includes properly formatted totals with thousands separators.
Exercise 5: Progress Bar Display
Create a progress bar using the Space function to display progress visually. Show percentage completion with proper formatting.
Next Lesson
Continue your VB6 journey with Lesson 13: String Functions.
Related Resources
Visual Basic 6 Made Easy
The ultimate beginner-friendly guide for mastering Windows-based application development using Visual Basic 6. Used as a textbook by universities worldwide.
What You'll Learn:
- Comprehensive coverage of VB6 coding techniques
- Event-driven programming best practices
- Practical examples and projects
- Debugging and error handling
- Database integration
- Advanced UI development
Visual Basic 2022 Made Easy
The ultimate guide to VB.NET programming in Visual Studio 2022. Master modern VB development with this comprehensive resource.
What You'll Learn:
- Modern VB.NET coding techniques
- Visual Studio 2022 features
- Advanced UI development
- Database programming with ADO.NET
- Web API integration
- Deployment strategies