melgra2017
06-28-2017, 05:44 AM
Hi, I have a file that I've imported into Access 2016 that contains 3 columns. One of the columns contains many values separated by a space. These can be varied in length. Ideally, I would like to create rows for each of these values including the Item Number field so things are unique. The following code I've found on another site but it isn't running and I don't get any errors so I'm not sure what's wrong. Thanks for the help in advance.
Sample
Item Numbers Profile SP_WorkTicket
ABC1235 ABCD17 Issue1 Issue2 Issue3 Issue4
Public Function BreakToWords()
Dim rsOrig As DAO.Recordset
Dim rsNew As DAO.Recordset
Dim vArr As Variant 'array to hold the split phrase
Dim i As Integer 'counter
Set rsOrig = CurrentDb.OpenRecordset("SELECTs.[Item Numbers], Slips.SP_WorkTicket FROM Slips where [Item Numbers] is not null;")
Set rsNew = CurrentDb.OpenRecordset("ItemNumberWorkTickets")
If rsOrig.RecordCount <> 0 Then
'loop the rsOrig records
rsOrig.MoveFirst
While Not rsOrig.EOF
'split the phrase on a space delimiter
vArr = split(rsOrig("[Item Numbers]"), ",")
'loop the array (words) and add to rsNew
For i = 0 To UBound(vArr)
With rsNew
Debug.Print vArr(i)
.AddNew
.Fields("SP_WorkTicket") = rsOrig("SP_WorkTicket")
.Fields("[ItemNumber]") = Trim(vArr(i))
.Update
End With
Next
rsOrig.MoveNext
Wend
End If
rsOrig.Close
rsNew.Close
Set rsOrig = Nothing
Set rsNew = Nothing
MsgBox "Complete"
End Function
Sample
Item Numbers Profile SP_WorkTicket
ABC1235 ABCD17 Issue1 Issue2 Issue3 Issue4
Public Function BreakToWords()
Dim rsOrig As DAO.Recordset
Dim rsNew As DAO.Recordset
Dim vArr As Variant 'array to hold the split phrase
Dim i As Integer 'counter
Set rsOrig = CurrentDb.OpenRecordset("SELECTs.[Item Numbers], Slips.SP_WorkTicket FROM Slips where [Item Numbers] is not null;")
Set rsNew = CurrentDb.OpenRecordset("ItemNumberWorkTickets")
If rsOrig.RecordCount <> 0 Then
'loop the rsOrig records
rsOrig.MoveFirst
While Not rsOrig.EOF
'split the phrase on a space delimiter
vArr = split(rsOrig("[Item Numbers]"), ",")
'loop the array (words) and add to rsNew
For i = 0 To UBound(vArr)
With rsNew
Debug.Print vArr(i)
.AddNew
.Fields("SP_WorkTicket") = rsOrig("SP_WorkTicket")
.Fields("[ItemNumber]") = Trim(vArr(i))
.Update
End With
Next
rsOrig.MoveNext
Wend
End If
rsOrig.Close
rsNew.Close
Set rsOrig = Nothing
Set rsNew = Nothing
MsgBox "Complete"
End Function