473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do you automagically insert dashes in different phone number lengths?

anoble1
245 128KB
I am trying to make a phone number lookup.
Is there a way to automatically put a dash in a number. We have 2 types of numbers.

1. x-xxx-xxxx
2. xxx-xxx-xxxx

Not sure if there is a way to do both of them, but would be cool.
May 24 '13 #1
2 3459
Seth Schrock
2,965 Expert 2GB
To do this, it has to be a text field. You could then use the Len() function to get the length of the data. Then you can use a select case statement to see if there are 8 characters (the first example), 10 characters (the second example) or "else" (invalid entry). You would then need to split up your string into three parts and then concatenate them back together with dashes in between. Here is some air code:
Expand|Select|Wrap|Line Numbers
  1. Dim s1 as string
  2. Dim s2 as string
  3. Dim s3 as string
  4.  
  5. Select Case Len(Me.MyField)
  6.     Case 8
  7.         s1 = Left(Me.MyField, 1)
  8.         s2 = Mid(Me.MyField, 2, 3)
  9.         s3 = Mid(Me.MyField, 5)
  10.  
  11.     Case 10
  12.         s1 = Left(Me.MyField, 3)
  13.         s2 = Mid(Me.MyField, 4, 3)
  14.         s3 = Mid(Me.MyField, 7)
  15.  
  16.     Case Else
  17.         MsgBox "Invalid Entry"
  18.         Exit Sub
  19.  
  20. End Select
  21.  
  22. Me.MyField = s1 & "-" & s2 & "-" & s3
The actual numbers of which characters are being grabbed might be off as I seldom get it right the first time, but the idea will work.
May 24 '13 #2
ADezii
8,834 Expert 8TB
Assuming the Phone Number Field is TEXT in nature and consists of either 8 or 9 characters, it can easily be Formatted as you requested:
  1. SQL:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblEmployees.[Phone#], Format$([Phone#],"&&@-@@@-@@@") AS FPhone
    2. FROM tblEmployees;
  2. Results
    Expand|Select|Wrap|Line Numbers
    1. Phone#       FPhone
    2. 12345678     12-345-678
    3. 123456789    123-456-789
    4. 456789875    456-789-875
    5. 998076412    998-076-412
    6. 12346677     12-346-677
    7. 222113145    222-113-145
    8. 878878787    878-878-787
    9. 99999999     99-999-999
    10. 23326526     23-326-526
    11.  
May 24 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Shay Hurley | last post by:
this is probably a stupid question so apologies in advance. I am trying to format a number to look like a phone number with "-"'s between the numbers etc e.g. 15554256987 should be formatted as...
5
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is...
5
by: Kamaluokeakua | last post by:
I have to write an application that deals with clients in multiple countries. The addresses, phone numbers, country id and currency information has to be stored into a database that allows for the...
6
by: Poewood | last post by:
Is there a way to parse the contents of a textbox into a phone number format? Actually I would like to save the input as a number but display it as a phone number in the typical US format (000)...
3
by: Mike | last post by:
I need to update a table with a phone number and have the phone number like (800)555-1212 how can I put ( ) around the area code in the update process?
3
by: venu | last post by:
Hi, I have a different requirement and it is : I need to validate a phone number field. It may or may not be a US phone number. The constraints are : *********************** # It should...
2
by: carrot | last post by:
The question is : Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9...
2
by: Joell | last post by:
Hi Guys, How can I format a phone number in SQL that has the dashes back to no dashes? i.e. 444-44-4444 needs to now be 444444444 ? I tried just pulling it into Crystal and using the picture...
1
by: Seth Williams | last post by:
I've got a strange problem - - I've got an input form (members), which has standard data, name, address, phone, etc I've changed the table now, so that the phone number accepts a varchar(25) - I...
4
by: luke noob | last post by:
This is my HTML... <head> <script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script> <script type="text/javascript" src="js/script.js"></script> </head> <body>
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.