Results 1 to 7 of 7
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237

    split a field

    Hi,
    I have an access database with a file that is alpha numeric where number and text forms the data.
    How can I spit this field into two different fields where the first field contains the numbers only and the second field contains the rest of the original one.

    sample:
    Original ========== field 1 ===========field 2
    240DIZ ===========240 ==============DIZ
    268004SAMR 4-2 =====268004 =========SAMR 4-2
    109090034abcd 55 ====109090034 =======abcd 55
    089011CTX =========289011 ==========CTX

    Note: All data in the original field starts with numeric values.



    The ====== is used to separate the three columns for clearance only in the post

    Khalil

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I would probably create some User Defined Functions (UDFs), like done here: http://www.utteraccess.com/forum/fin...-t1766615.html

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Last record shows 089011 becomes 289011 - must be a typo.

    Val([original]) will return the numbers at beginning but will drop leading zeros. So if leading zeroes must be considered, second part can be returned with:

    Mid([original], Len(CStr(Val([original])) + IIf(Left([original],1)=0,2,1))

    Consistency in structure is critical when parsing text.
    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.

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Last record in the sample is a mistake, it should be 089011.
    I have 36 records with leading zeros.
    I used the formula and was able to get the second part but with a leading number at the begining (sample: 9TAC 4-2), I was able to delete the first digit.

    Is there a way to keep a leading zero in a numeric field othat than the Format?

    Thanks to all



  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    The only way to keep leading 0 is to treat data as string/text.

    Here's a sample routine using your data as test data.
    Code:
    Sub testMay19()
    10  On Error GoTo testmay19_Error
        Dim i As Integer
        Dim numpart As String, otherpart As String
        Dim str(3) As String
        'test data
    20  str(0) = "240DIZ"
    30  str(1) = "268004SAMR 4-2"
    40  str(2) = "109090034abcd 55"
    50  str(3) = "089011CTX"
        'process test data
    60  For i = 0 To UBound(str)
    70      If Left(str(i), 1) = "0" Then
    80          numpart = "0" & val(str(i))
    90      Else
    100         numpart = val(str(i))
    120     End If
            'process remainder of test data record
    110     otherpart = Right(str(i), Len(str(i)) - Len(numpart))
    130     Debug.Print str(i) & Space(20 - Len(str(i))) & " ---> " & "numpart: " & numpart & vbTab & "otherpart: " & otherpart
    140 Next i
    
    
    150 On Error GoTo 0
    160 Exit Sub
    
    testmay19_Error:
    
    170 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure testmay19, line " & Erl & "."
    
    End Sub
    And the results:
    240DIZ ---> numpart: 240 otherpart: DIZ
    268004SAMR 4-2 ---> numpart: 268004 otherpart: SAMR 4-2
    109090034abcd 55 ---> numpart: 109090034 otherpart: abcd 55
    089011CTX ---> numpart: 089011 otherpart: CTX

  6. #6
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Thank you all for the help.
    Solved

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    What was your solution? It may help someone else.

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

Similar Threads

  1. Split alphanumeric field
    By aswin34 in forum Queries
    Replies: 3
    Last Post: 09-11-2015, 08:37 AM
  2. Split Currency Field
    By RayMilhon in forum Reports
    Replies: 2
    Last Post: 02-06-2015, 10:21 AM
  3. Read only field on Split form
    By casinc815 in forum Forms
    Replies: 1
    Last Post: 04-26-2014, 02:51 PM
  4. split and reverse a string in a field
    By mzeltman in forum Queries
    Replies: 1
    Last Post: 04-04-2014, 02:32 PM
  5. split text field
    By Zukster in forum Queries
    Replies: 4
    Last Post: 01-11-2011, 10:01 PM

Tags for this Thread

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