Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    I'm struggling to follow this guide...

    I am very much an access novice. I am struggling to figure out how to follow this guide. I've been working on it all day. I'm not sure how to actually implement the function.

    I have a table full of phone numbers and the code cited (also below) is exactly what I need. I just don't know how to actually implement it (I'm an idiot). Do I put that function into module? After that, then what? Do I use a query to call it? How exactly is the query coded? My table name is just tblCustomers.

    If anyone could kindly walk me through it, I'd be incredibly grateful.



    Thanks
    Code:
    Function ExtractPhoneNum(s As String) As String   Dim strS As String
    
    
       strS = Replace(Replace(s, "(", ""), ")", "")
       If InStr(strS, "x") <> 0 Then strS = Left(strS, InStr(strS, "x") - 1)
       If InStr(strS, "+") <> 0 Then strS = Mid(strS, InStr(strS, " ") + 1)
       ExtractPhoneNum = Val(strS) 
    End Function
    Last edited by templeowls; 06-10-2022 at 06:55 AM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, you would copy that function into a standard module (not behind a form or report). You could call it from anywhere, but in a query:

    SELECT YourFieldName, ExtractPhoneNum(YourFieldName) As Test
    FROM tblCustomers
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    How about posting some samples of the phone numbers involved?
    Did you see the link to International calling codes from Gustav at StackOverflow?

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is the function from Post #1 with all of the HTML formatting removed (I hope)
    Code:
    Function ExtractPhoneNum(s As String) As String
       Dim strS As String
    
       strS = Replace(Replace(s, "(", ""), ")", "")
       If InStr(strS, "x") <> 0 Then strS = Left(strS, InStr(strS, "x") - 1)
       If InStr(strS, "+") <> 0 Then strS = Mid(strS, InStr(strS, " ") + 1)
       ExtractPhoneNum = Val(strS) 
    End Function

  5. #5
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by ssanfu View Post
    Here is the function from Post #1 with all of the HTML formatting removed (I hope)
    Code:
    Function ExtractPhoneNum(s As String) As String
       Dim strS As String
    
       strS = Replace(Replace(s, "(", ""), ")", "")
       If InStr(strS, "x") <> 0 Then strS = Left(strS, InStr(strS, "x") - 1)
       If InStr(strS, "+") <> 0 Then strS = Mid(strS, InStr(strS, " ") + 1)
       ExtractPhoneNum = Val(strS) 
    End Function
    Omg I didn't even realize I left the HTML in there. Sorry about that. I updated my post.

    @pbaldy that solution worked! One issue though with the code itself though. Hoping someone could help:

    It doesn't work with dashes. If a phone number is +1 (234) 567-8890, it is showing as 234567. It is correctly getting rid of the dash but then not including the 4 digits afterwards.

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Always better to show the actual code you are actually using as the code provided does not remove '-'

    Always recommend when you pick up a bit of code from the web, you spend a little bit of time to understand what it is actually doing by stepping though and checking values as you go

  7. #7
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by Ajax View Post
    Always better to show the actual code you are actually using as the code provided does not remove '-'

    Always recommend when you pick up a bit of code from the web, you spend a little bit of time to understand what it is actually doing by stepping though and checking values as you go
    That is the actual code I'm using. I did what pbadly suggested. I dropped that exact code into a module then created a select query. The module code and select query code are below. I've tested it multiple times; any time there's a dash, it removes it along with everything that comes after it.

    Code:
    Function ExtractPhoneNum(s As String) As String
       Dim strS As String
       strS = Replace(Replace(s, "(", ""), ")", "")
       If InStr(strS, "x") <> 0 Then strS = Left(strS, InStr(strS, "x") - 1)
       If InStr(strS, "+") <> 0 Then strS = Mid(strS, InStr(strS, " ") + 1)
       ExtractPhoneNum = Val(strS)
    End Function
    Code:
    SELECT PhoneNumber, ExtractPhoneNum([PhoneNumber]) AS Test FROM tblCustomers;

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Because Val stops at the first character that cannot be recognized as a number and the last - is not being removed first. Either nest another Replace function to remove the last - or create a loop that does it until Instr = 0.
    EDIT - Actually, I'm not sure if spaces count wrt Val.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    suggest check the before and after values as your comment 'any time there's a dash, it removes it along with everything that comes after it.' doesn't ring true. Think you will find it is not removing everything else but changing what comes before.

    if you step through the code, what is the value of strS at each stage?

    edit: - micron has given you a clue

  10. #10
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Code:
    Because Val stops at the first character that cannot be recognized as a number and the last - is not being removed first. Either nest another Replace function to remove the last - or create a loop that does it until Instr = 0.
    EDIT - Actually, I'm not sure if spaces count wrt Val.
    Thanks. I'll try to figure it out

  11. #11
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    I was able to figure it out using the below code. If you look at the guide I originally cited, the poster didn't have dashes in their numbers so the code never solved for that.

    Thanks for the help everyone!

    Code:
    Function ExtractPhoneNum(s As String) As String   Dim strS As String
       strS = Replace(Replace(Replace(s, "(", ""), ")", ""), "-", "")
       If InStr(strS, "+") <> 0 Then strS = Mid(strS, InStr(strS, " ") + 1)
       ExtractPhoneNum = Val(strS)
    End Function

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    the other problem you will have with val is it will remove initial 0's

    so

    val("0234 567 8890")

    will return
    2345678890

    edit: you could just use replace

    ExtractPhoneNum = val(replace(replace(Replace(Replace(Replace(s, "(", ""), ")", ""), "-", ""),"+","")," ",""))

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Or
    Code:
    Function ExtractPhoneNum(s As String) As String
    
    Do Until InStr(s, "-") = 0 And InStr(s, ")") = 0 And InStr(s, "(") = 0 And InStr(s, "+") = 0
         s = Replace(s, "-", " ")
         s = Replace(s, ")", "")
         s = Replace(s, "(", "")
         s = Replace(s, "+", "")
    Loop
    
    If InStr(s, "x") > 0 Then s = Left(s, InStr(s, "x") - 1)
    ExtractPhoneNum = s
    
    End Function
    Might be easier to add to that vs more nested Replace functions. That also eliminates using val.
    EDIT - As is so often the case, desired output not shown as far as I can see.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    why use a loop? - replace will replace all instances of a character in one go

    only time I would use a loop is where you want to replace 2 or more consecutive chars with a single one

    Code:
    Do Until InStr(s, "--") = 0
         s = Replace(s, "--", "-")
    Loop
    agree re val - it just removes the spaces in this case. Confusing since the function is a string and is being assigned a number

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Yes, good point. That's what happens when I rush and have had no coffee yet. I'm about to take care of that right now. So more like?
    Code:
    s = Replace(s, "-", " ")
    s = Replace(s, ")", "")
    s = Replace(s, "(", "")
    s = Replace(s, "+", "")
    
    If InStr(s, "x") > 0 Then s = Left(s, InStr(s, "x") - 1)
    ExtractPhoneNum = s
    Note that I dropped the string variable as it's not needed.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. User Guide
    By yagerlegi in forum Access
    Replies: 5
    Last Post: 10-14-2014, 02:36 PM
  2. Help or Guide to Particular Form Setup
    By Zephyx in forum Forms
    Replies: 2
    Last Post: 06-27-2013, 03:53 PM
  3. Someone to help me understand a guide
    By carrybag93 in forum Access
    Replies: 4
    Last Post: 06-04-2012, 12:20 AM
  4. VBA & VBA GUI Guide?
    By bginhb in forum Programming
    Replies: 2
    Last Post: 04-01-2012, 08:39 AM
  5. plz guide about access
    By ali zaib in forum Access
    Replies: 3
    Last Post: 01-10-2012, 11:25 AM

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