We are going to create a new VB Tips newsletter. There will be no fee for this newsletter. It will contain short (10-50 lines of text) code snippets, tips, and other useful information for Visual Basic. If you have any tips to contribute, email us at newtips@vbdaily.com. Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $50 US Dollars depending on length and complexity.
If you are interested in subscribing to this newsletter just send an email to tips-on@vbdaily.com and you will be added to our subscriber list.
This issue presents one method to format a United States phone number. It will also work in locations where the phone numbers are 7 digits or 10 digits long. It can easily be modified to fit your local phone number requirements.
If you have any questions about using this module, let us know at questions@codeoftheweek.com
This module shows a good model for formatting phone numbers (or other numeric values). It will format a 7 digit or 10 digit phone number in the format 999-9999 or (999)999-9999.
Public Function FormatUSPhoneNumber(ByVal sPhone As String) As String
Call this routine with a phone number as the specified parameter. It will raise an error if the phone number is not 7 or 10 digits after all non-numerics are removed. It will return a string in the format of 999-9999 or (999)999-9999. If you want to modify the formatted output update the Format$ line in the routine.
This sample shows how to use the FormatUSPhoneNumber function.
Debug.Print FormatUSPhoneNumber("2125047945") ' the output will be: (212)504-7945 Debug.Print FormatUSPhoneNumber("212-504-7945") ' the output will be: (212)504-7945
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0130.html