I have a table that has the columns below. I'm trying to separate the Additional Fig and Additional Item in new records but have old ID still associated with them.
So that I end up with a table like the one below. On ID 89 there are two additional fig but only one additional item, on records like these the missing additional item will be left blank or 9999 needs to put into the record.
The code below is from Split Field Into Multiple Records in Access DB. it gets me close to what I want to do. The code splits one column into many but I need to do two into many and find a way to deal with record where there is a additional fig but no additional item or vice versa.
Option Explicit
Public Sub ReformatTable()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsADD As DAO.Recordset
Dim strSQL As String
Dim strField1 As String
Dim strField2 As String
Dim varData As Variant
Dim strAppCode As String
Dim i As Integer
Set db = CurrentDb
' Select all eligible fields (have a comma) and unprocessed (Field2 is Null)
strSQL = "SELECT AppCode, Field1, Field2 FROM Table1 WHERE ([Field1] Like ""*,*"") AND ([Field2] Is Null)"
' This recordset is only used to Append New Records
Set rsADD = db.OpenRecordset("Table1", dbOpenDynaset, dbAppendOnly)
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
With rs
While Not .EOF
' Do we need this for newly appended records?
strAppCode = !AppCode
strField1 = !Field1
varData = Split(strField1, ",") ' Get all comma delimited fields
' Update First Field
.Edit
!Field2 = Trim(varData(0)) ' remove spaces before writing new fields
.Update
' Add new fields for remaining data at end of string
For i = 1 To UBound(varData)
With rsADD
.AddNew
' ***If you need a NEW Primary Key based on current AppCode
!AppCode = strAppCode & "-" & i
' ***If you remove the Unique/PrimaryKey and just want the same code copied
!AppCode = strAppCode
' Copy previous Field 1
!Field1 = strField1
' Insert Field 2 based on extracted data from Field 1
!Field2 = Trim(varData(i)) ' remove spaces before writing new fields
.Update
End With
Next
.MoveNext
Wend
.Close
rsADD.Close
End With
Set rsADD = Nothing
Set rs = Nothing
db.Close
Set db = Nothing
End Sub