Results 1 to 4 of 4
  1. #1
    mfmuc is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2017
    Location
    Germany
    Posts
    3

    Splitt field in new table and new records

    Hi,
    I would need a Access program that is doing following.

    I have a table with following field:



    Table: Input
    ID Comment
    4711 abc,bbc,xyz,bmw
    4712 akw,ibm,

    These records needs to be splitt into a new table

    Table: Output
    ID Value
    4711 abc
    4711 bbc
    4711 xyz
    4711 bmw
    4712 akw
    4712 ibm

    Can someone please help me?

    Best,
    Martin

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Here is some code which uses the Split function (change names as applicable):

    Code:
     
    
    Public Function SplitTheField()
        Dim LString As String, LArray() As String, rst As Recordset, rstOut As Recordset, x As Integer
        On Error Resume Next
        Set rst = CurrentDb.OpenRecordset("Input_tbl", dbOpenDynaset)
        Set rstOut = CurrentDb.OpenRecordset("Output_tbl", dbOpenDynaset)
        DoCmd.RunSQL "DELETE * FROM Output_tbl;"
        rst.MoveFirst
        If Err <> 0 Then
            MsgBox "No input records found"
            Exit Function
        End If
        
        Do Until rst.EOF
            If Right(rst!InComment, 1) = "," Or Right(rst!InComment, 1) = " " Then
                LArray = Split(Left(rst!InComment, Len(rst!InComment) - 1), ",")
            Else
                LArray = Split(rst!InComment, ",")
            End If
            If Err <> 0 Then
                MsgBox "Invalid string format found, cannot process - " & rst!InId & " " & Err.Number & " " & Err.Description
                Err = 0
            Else
                For x = 0 To UBound(LArray)
                    rstOut.AddNew
                    rstOut!InId = rst!InId
                    rstOut!OutComment = LArray(x)
                    rstOut.Update
                Next
            End If
            rst.MoveNext
            Err = 0
        Loop
        Set rst = Nothing
        Set rstOut = Nothing
        
    End Function

  3. #3
    mfmuc is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2017
    Location
    Germany
    Posts
    3
    Thank you so much. This saves me more than one hour of work! Best, Martin

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You're welcome. I should probably have provided the tables!
    Input_tbl:
    InID number
    InComment text

    Output_tbl
    OutID Autonumber
    InId number
    OutComment text

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

Similar Threads

  1. Modifying the Table Field into Records
    By mkrylataya in forum Access
    Replies: 2
    Last Post: 11-04-2016, 11:45 PM
  2. Replies: 6
    Last Post: 03-23-2015, 08:16 AM
  3. Replies: 2
    Last Post: 02-26-2015, 09:33 AM
  4. Replies: 5
    Last Post: 05-02-2013, 12:04 PM
  5. Replies: 2
    Last Post: 01-22-2010, 03:53 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