Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17

    Remove eveything (including text) to the right. What's wrong with this code?

    This is only removing the text characters.. I need it to remove all the text and everything after it



    Public Function ExtractNumeric(TextString As String) As String
    Dim x As Long
    Dim sDigit As String



    ExtractNumeric = vbNullString
    For x = 1 To Len(TextString)
    sDigit = Mid(TextString, x, 1)
    If sDigit >= "0" And sDigit <= "9" Then
    ExtractNumeric = ExtractNumeric & sDigit

    End If
    Next x
    End Function

  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,640
    Does adding the bit in red work for you?

    Code:
    If sDigit >= "0" And sDigit <= "9" Then
      ExtractNumeric = ExtractNumeric & sDigit
    Else
      Exit Function
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17
    Hey Paul...
    Thanks but it that didn't help..

    When I pull it into a query to test, The value comes up blank

  4. #4
    DaveT is offline Access Developer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    May 2010
    Location
    Texas
    Posts
    69
    Your function works for me as shown. Are you expecting something else?

  5. #5
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17
    Hey Dave..
    Right,.. this is what I get too.. the Text is striped. I need to strip the texts as well as everything behind it..
    This function is being used for splitting a "Phone Number" column into "Phone" and "Ext" columns..

    In your example.. Row 2 it is possible that a text character is entered at the beginning of the field "ext 217 (312)555-5555" in which the function would completely wipe out everything after and including the first text character "e"...

    I'm trying to get to...

    (312)555-5555 Ext 217 = tblcontact.[Phone] 3125555555 tblContact.[ex] 217

    my current code gets me 3125555555217

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I based mine on this:

    Quote Originally Posted by DetrieZ View Post
    I need it to remove all the text and everything after it
    Can we clarify the goal, including what you might start with and what you want to end up with?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17
    Sorry I wasn't more clear.


    I'm trying to get from
    tblContact.[PhoneNumber] = (312)555-5555 Ext 217
    To
    tblcontact.[Phone] 3125555555



    my current code gets me from
    tblContact.[PhoneNumber] = (312)555-5555 Ext 217
    to
    tblcontact.[Phone] 3125555555217

    notice the 217 still exists and only the "Ext" has been removed

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Presuming the parentheses are actually in the data, the code modification I posted earlier can be tweaked to pass over them:

    Code:
    If sDigit >= "0" And sDigit <= "9" Then 
      ExtractNumeric = ExtractNumeric & sDigit 
    ElseIf sDigit = "(" OR sDigit = ")" Then 
      'don't do anything
    Else
      Exit Function 
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    Quote Originally Posted by pbaldy View Post
    Presuming the parentheses are actually in the data, the code modification I posted earlier can be tweaked to pass over them:

    Code:
    If sDigit >= "0" And sDigit <= "9" Then 
      ExtractNumeric = ExtractNumeric & sDigit 
    ElseIf sDigit = "(" OR sDigit = ")" Then 
      'don't do anything
    Else
      Exit Function 
    End If
    you forgot the "-"

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Quote Originally Posted by weekend00 View Post
    you forgot the "-"
    I did indeed.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    DaveT is offline Access Developer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    May 2010
    Location
    Texas
    Posts
    69
    This works if Ext occurs after number or if Ext is missing.
    If you have the case of Ext 123 (999)-555-1234. let me know and I can tweak the code some more.

    <code>
    Public Function ExtractNumeric(TextString As String, EP As String) As String

    'version 1.0 DLT
    'pass string such as 999-555-1111 Ext 123 (with or without Ext present)
    'pass "E" or "P"
    'if E then return extension: 123
    'if P then return phone: 9995551111

    Dim x As Long
    Dim sDigit As String
    Const EXT As String = "Ext"
    Const DIGITS As String = "0123456789"
    Dim strNumber As String
    Dim strExt As String
    Dim zPos As Long
    Dim zLEN As Long
    Dim zMAX As Long
    Dim blnFound As Boolean

    ExtractNumeric = vbNullString

    zLEN = Len(Nz(TextString))
    If zLEN = 0 Then
    Exit Function
    End If

    zPos = InStr(TextString, EXT)

    Select Case EP
    Case "E"

    If (zPos > 0) And (zPos < zLEN) Then
    blnFound = False

    For x = zPos To zLEN
    sDigit = Mid(TextString, x, 1)
    If IsNumeric(sDigit) Then
    blnFound = True
    ExtractNumeric = ExtractNumeric & sDigit

    ElseIf (blnFound) And Not IsNumeric(sDigit) Then
    Exit Function

    End If
    Next
    End If

    Case "P"

    If zPos > 0 Then
    zMAX = zPos
    Else
    zMAX = zLEN
    End If

    For x = 1 To zMAX
    sDigit = Mid(TextString, x, 1)

    If IsNumeric(sDigit) Then
    ExtractNumeric = ExtractNumeric & sDigit
    End If
    Next
    End Select
    End Function
    </code>

  12. #12
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17

    Resolved

    If you have the case of Ext 123 (999)-555-1234. let me know and I can tweak the code some more.
    This "could" happen but I don't see the need to tweak...

    This runs perfectly. I'm sorry to have taken so much of your time and not being clear in my first post for help. I've been working this for days.

    I will now go back to understand what is actually going on in your code.

    Dave and Paul.. thank you guys

  13. #13
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17
    Ugh.. I posted too quickly...

    It seems only to work if "Ext" appears in the initial field.

    Because the point of entry for this data is free text, an extension can be entered with any number of text characters "ext", "x", "ex" etc.

    sorry

  14. #14
    DaveT is offline Access Developer
    Windows 7 Access 2010 (version 14.0)
    Join Date
    May 2010
    Location
    Texas
    Posts
    69
    It's getting late. I can look at this more this weekend.
    MEANWHILE, a quick flyby and you might try modifying the code as follows:

    Const EXT As String = "x"
    'Const DIGITS As String = "0123456789"

    just search for x

    DIGITS can be rem'd out since I didn't use it in the procedure

  15. #15
    DetrieZ is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2010
    Posts
    17
    I tried "Const EXT As String = "x" but the problem is it could any letter or any combination of letters.
    I think the code you sent will strip all text characters, I just need to find the first on in the Const EXT part .. <Const EXT As String = "the first text character in a string"> .. RIght?

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

Similar Threads

  1. Saving Module Code to Text Files
    By ioMatt in forum Modules
    Replies: 2
    Last Post: 07-02-2011, 08:18 AM
  2. Including two queries in one report
    By kulanga in forum Reports
    Replies: 1
    Last Post: 03-23-2010, 10:21 PM
  3. What is wrong with this code?
    By nkenney in forum Forms
    Replies: 2
    Last Post: 11-16-2009, 03:04 PM
  4. Query including Null relationship?
    By David Criniti in forum Database Design
    Replies: 0
    Last Post: 08-14-2009, 09:10 PM
  5. Anything wrong with this one line of code?
    By alsoto in forum Reports
    Replies: 3
    Last Post: 07-01-2009, 09:23 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