Results 1 to 10 of 10
  1. #1
    mlrucci is offline Competent Performer
    Windows 7 64bit Office 365
    Join Date
    Apr 2018
    Posts
    202

    Angry Removing special characters and numbers from text field

    Good afternoon, I have imported a spreadsheet from excel to access. I have a filed called Document ID. I would like to remove special characters from this to create a new number. This is essential in creating a 1:1 relationship on another table.

    Original value:
    0871900602-601116-7-1



    New Value: Need to delete all preceding 0s, delete anthing after the second "-" and removed the "-" in the middle. End result should be:
    871900602601116

    Suggestions?
    Actually completely lost as to how to even start.

    Any help would be appreciated.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    Use split() function? And instrrev()
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    put this function in a module ,
    then run an update query to post the correction in a NEW FIELD. (don't overwrite the original value)
    or
    run the correction at import if using a query.

    usage:
    FixNum([FIELD])

    Code:
    Public Function FixNum(ByVal pvNum)
    Dim vRet, vChr
    Dim iDashCt As Integer, i As Integer
    On Error Resume Next
    
      'remove leading zeros
    vChr = Left(pvNum, 1)
    While vChr = "0"
      pvNum = Mid(pvNum, 2)
      vChr = Left(pvNum, 1)
    Wend
    
    'now keep nums
    For i = 1 To Len(pvNum)
       vChr = Left(pvNum, 1)
       Select Case True
          Case vChr = "-"
            iDashCt = iDashCt + 1
            pvNum = Mid(pvNum, 2)
            If iDashCt = 2 Then GoTo endit
            
          Case Asc(vChr) > 47 And Asc(vChr) < 58
            vRet = vRet & vChr
            pvNum = Mid(pvNum, 2)
       End Select
       
    Next
    
    endit:
    FixNum = vRet
    End Function
    


  4. #4
    mlrucci is offline Competent Performer
    Windows 7 64bit Office 365
    Join Date
    Apr 2018
    Posts
    202
    Ranman256,
    Thank you for your response. I am fairly new to coding and am self-taught. I can create the module and I copied/pasted the code provided. I created a new field in the table. Not sure how to run the module to then run the update query (I'm familiar with update qrys, just not using a module). Can you please provide me with some additional information?

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Original value:
    0871900602-601116-7-1
    Are the original numbers consistent? If yes you can use something as simple as this in a calculated field in a query:
    FixNumber=Replace(Mid(OriginalNumber,2,16),"-","")

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    mlrucci is offline Competent Performer
    Windows 7 64bit Office 365
    Join Date
    Apr 2018
    Posts
    202
    Thank you all for your help. I wound up using Gicu's suggestion and did an update qry onto the table. Thanks again! Works great

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,776
    So you will never have something like 0071900602-601116-7-1 or 0871900602-601-7-1 or some other deviation from what you posted?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Can you tell us more about this relationship?
    This is essential in creating a 1:1 relationship on another table.

  9. #9
    mlrucci is offline Competent Performer
    Windows 7 64bit Office 365
    Join Date
    Apr 2018
    Posts
    202
    the 1:1 relationship number is embedded in the Original value:0871900602-601116-7-1. After removing the necessary characters and numbers, it will become a 1:1 relationship with another field in a different table. The end result is 871900602601116. In other words, by removing preceding 0, all-, and anything after the second -, I get what I need. All the numbers are in the same format. This number is created by a different platform and cannot be adjusted. Hope that is clear as mudd.

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2018, 03:45 PM
  2. Replies: 8
    Last Post: 10-09-2014, 01:31 AM
  3. Replies: 6
    Last Post: 05-08-2014, 01:32 PM
  4. Removing special characters
    By crowegreg in forum Queries
    Replies: 3
    Last Post: 02-26-2014, 11:56 AM
  5. Setting a field to only accept text characters, not numbers
    By USAFA2012 in forum Database Design
    Replies: 2
    Last Post: 03-09-2010, 12:37 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