Results 1 to 5 of 5
  1. #1
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39

    Default area code in Phone field with input mask

    I am using this input mask in a phone number field: \(999") "000\-0000;;_
    The field could only take 10 digits.
    I would like to have a default area code on Gotfocus or on click using vba.
    I tried several methods with no success.
    Does anyone know how this could be accomplished.

    This method for example puts the area code at the end. Can anyone determine why this does not work.

    Private Sub YourPhoneNumberField_GotFocus()


    Dim defaultAreaCode As String
    Dim currentPosition As Integer
    defaultAreaCode = "800"
    ' Remove spaces from the current value of the field
    Dim phoneNumber As String
    phoneNumber = Replace(Me.PhoneNumberField, " ", "")
    ' Insert the area code at the beginning
    Me.PhoneNumberField = defaultAreaCode & Mid(phoneNumber, 4)
    currentPosition = Len(defaultAreaCode) ' Set cursor position after the first 3 digits
    Me.PhoneNumberField.SetFocus
    Me.PhoneNumberField.SelStart = currentPosition
    End Sub





  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Could have area code in its own field.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    If you get this working, what happens when your local area codes expand? In Colorado, area codes have expanded from one to four!

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    What does "does not work" mean? Nothing happens? Cursor in wrong position? Cursor not in any position? In that vein, consider that character position in a string is to the left of the character. So the periods are at position 1, 2 and 3 respectively:
    .800
    8.00
    80.0
    Since Len(defaultAreaCode) is 3, your cursor position would be between the two zeros.

    If you use that as the starting point for this you should be in the wrong position.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    ijo68 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2021
    Posts
    39
    this works..
    In the input mask property of the field use : \(999") "000\-0000;;_

    In the GotFocus use this code:

    If IsNull(Me.yourPhoneField) Then
    yourPhoneField.Value = "(555)"
    yourPhoneField.SelStart = 5
    End If
    yourPhoneField.InputMask = "(000)000-0000"

    this also allows you to change the area code if it is not 555.


Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-28-2019, 04:30 PM
  2. Replies: 4
    Last Post: 05-02-2016, 04:23 PM
  3. Using an input mask with a default value
    By tc197 in forum Access
    Replies: 4
    Last Post: 07-03-2014, 01:18 PM
  4. How to exclude an area code in a phone number?
    By indians207 in forum Queries
    Replies: 11
    Last Post: 11-21-2013, 05:59 PM
  5. Phone Input Mask issue
    By Huddle in forum Forms
    Replies: 4
    Last Post: 08-13-2010, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums