Brad Dean

Use VB's Powerful Format Function Through ASP

This article originally appeared on 4GuysFromRolla.com at https://www.4guysfromrolla.com/webtech/051601-1.shtml.

When creating ASP pages you can choose to use a number of scripting languages, such as VBScript, JScript, PerlScript, and even Python. While this may seem all fine and good, realize that VBScript, the most popular server-side scripting languages, is a watered-down version of VisualBasic. Think of VBScript as VB-Lite - certain things are missing.

VB’s Format() function is such a missing feature from VBScript. Yeah, VBScript contains a number of formatting functions - FormatDateTime(), FormatNumber(), FormatCurrency(), etc. - but these format functions pale in comparison to the more generic Format() function. These VBScript formatting functions offer just a select few formatting “templates” and are also reliant on the locale settings of the Web server. The VB Format() function, however, is much more generic, allowing for a much more custom format.

The Format() function takes two parameters and returns a string. The first parameter is the string to format, while the second parameter is the format pattern string. (The returned string is the first parameter that is formatted according to the format pattern.)

strFormattedString = Format(StringToFormat, FormatPattern)

The FormatPattern string can contain a wide-array of predefined format patterns. You can see a long list of these patterns here. Below you will see some common format string examples and their associated outputs. You can also view the live demo and try out your own formatting patterns and inputs and view the formatted output!

Due to the wide array of potential format patterns, the Format() function can be used to format a number of string types. Often, the Format() function is used for formatting dates. So, if today is May 1st, 2001:

Format(Now, "MM/DD/YYYY") 'will return '05/01/2001'
Format(Now, "DDDD") 'will return 'Tuesday'.

The Format() function will also format any number:

Format(12, "Fixed") 'will return "12.00"

Or string:

Format("5625551234", "(###)###-####") 'will return "(562)555-1234"

Check out these links for more about the Format() function and formatting patterns:

Let’s take this last Format() function example, formatting a string of numbers into a telephone number. Since the Format() function is a VB function (and not a VBScript function), you can’t use the Format() function in an ASP page. So, if you wanted the same output results for the telephone number formatting using VBScript you would have to do:

Str = "5625551234"
Str = "(" & Mid(Str, 1, 3) & ")" & Mid(Str, 4, 3) & "-" & Mid(Str, 7, 4)

Not very pretty, but it works. Recently I found myself creating a week-view calendar for viewing scheduled DIRECTV installations for < shameless plug >InfoDish.com< /shameless plug >. Since there were a number of areas on the Web site that I needed to have very custom control over the date formatting, I decided that I wasn’t interested in using VBScript’s FormatDateTime() function. I considered using a custom date formatting script, such as the one presented in the 4Guys article A Customizable Date Formatting Routine, but decided that it would be nice to be able to also have more control over other formatting facets (such as complete control over formatting numbers). What I really wanted was to be able to easily use VB’s Format() function via an ASP page…

Then a light bulb went off. Since ASP supports the use of ActiveX components (COM components) I could simply create an ActiveX component for any function available in Visual Basic! (If you want a good article on creating an ActiveX DLLs in Visual Basic for use on an ASP web site, read Writing a COM Object with VisualBasic 6.0.)

In simple terms, open up Visual Basic, click New, and select ‘ActiveX DLL’. Click Project -> Add New Class Module. Then add this code to that module.

Public Function FormatString(ByVal Str As String, ByVal Formatting as String) As String FormatString = Format(Str, Formatting) End Function Compile the project, write it to a DLL, save it to the server, and register it. (You can download the DLL file and complete VisualBasic Project files at the end of this article.) The code to use the VB Format() function via an ASP page is now only three lines long:

Dim objDLL
Set objDLL = Server.CreateObject("ProjectName.ClassModuleName")
DayName = objDLL.FormatString(DateRequested, "DDDD")

Now, you are free to use VB’s powerful Format() function using VBScript! Be sure to view the live demo! I hope this helps you in your projects.

God bless and Happy Programming,