DiamondGeezer
New member
- Local time
- Today, 23:53
- Joined
- Dec 23, 2021
- Messages
- 10
Hi all, I'm looking for some help please on a create table macro that, after the initial run, can hopefully change to an append query. I need a macro that will trim & remove commas from the information in one field that existed in CSV format in an excel spreadsheet prior to import. I need to create individual records for each of the items (serial numbers) between the commas.
I am trying to use a table named Source as the source to create a table named SerialSplit and this is what I tried to use which seems to be creating a new table, but the data remains the same.
' Procedure : SpliSerials
' Purpose : One field has comma delimited data - split them to multiple records
' Copy the primary table (Source) and delete all records - the copy becomes SerialSplit
' Row 80 replace the field with the comma delimited data after the rsSource!
' Row 150 - on the rsout - add the name for the split
' in the Immediate window type then enter : SplitToRows
10 On Error GoTo PROC_Error
Dim db As DAO.Database
Dim rsSource As DAO.Recordset
Dim rsOut As DAO.Recordset
Dim SplitToRows() As String
Dim i As Integer
20 Set db = CurrentDb
30 Set rsSource = db.OpenRecordset("Source") ' source table
40 Set rsOut = db.OpenRecordset("SerialSplit") ' copy of empty source table
50 If (Not rsSource.BOF And Not rsSource.EOF) Then
60 rsSource.MoveFirst
70 Do Until rsSource.EOF
80 SerialSplit = Split(rsSource!Serial_Source, ",", -1) ' comma - change to other delimiter here.
90 For i = LBound(SplitToRows()) To UBound(SplitToRows())
100 rsOut.AddNew
110 rsOut("Order Nbr") = rsSource("Order Nbr") ' Keep these the same
120 rsOut("Shipped to City") = rsSource("Shipped to City")
130 rsOut("Shipped to State") = rsSource("Shipped to State")
140 rsOut("Lease_Type") = rsSource("Lease_Type")
' Data was in one field - split by a comma
150 rsOut("Serial_Source") = SerialSplit(i) ' The field name that gets split based on a comma
160 rsOut.Update
170 Next i
180 rsSource.MoveNext
190 Loop
200 Else
210 MsgBox "No Records in Input"
220 End If
230 rsSource.Close
240 Set rsSource = Nothing
250 rsOut.Close
260 Set rsOut = Nothing
270 Set db = Nothing
PROC_Exit:
280 Exit Sub
PROC_Error:
290 'On Error GoTo 0 ' comment out 300 if no message box is needed
300 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in SplitSerials procedure"
310 On Error GoTo 0 ' clear error
Resume PROC_Exit:
End Sub
I am trying to use a table named Source as the source to create a table named SerialSplit and this is what I tried to use which seems to be creating a new table, but the data remains the same.
' Procedure : SpliSerials
' Purpose : One field has comma delimited data - split them to multiple records
' Copy the primary table (Source) and delete all records - the copy becomes SerialSplit
' Row 80 replace the field with the comma delimited data after the rsSource!
' Row 150 - on the rsout - add the name for the split
' in the Immediate window type then enter : SplitToRows
10 On Error GoTo PROC_Error
Dim db As DAO.Database
Dim rsSource As DAO.Recordset
Dim rsOut As DAO.Recordset
Dim SplitToRows() As String
Dim i As Integer
20 Set db = CurrentDb
30 Set rsSource = db.OpenRecordset("Source") ' source table
40 Set rsOut = db.OpenRecordset("SerialSplit") ' copy of empty source table
50 If (Not rsSource.BOF And Not rsSource.EOF) Then
60 rsSource.MoveFirst
70 Do Until rsSource.EOF
80 SerialSplit = Split(rsSource!Serial_Source, ",", -1) ' comma - change to other delimiter here.
90 For i = LBound(SplitToRows()) To UBound(SplitToRows())
100 rsOut.AddNew
110 rsOut("Order Nbr") = rsSource("Order Nbr") ' Keep these the same
120 rsOut("Shipped to City") = rsSource("Shipped to City")
130 rsOut("Shipped to State") = rsSource("Shipped to State")
140 rsOut("Lease_Type") = rsSource("Lease_Type")
' Data was in one field - split by a comma
150 rsOut("Serial_Source") = SerialSplit(i) ' The field name that gets split based on a comma
160 rsOut.Update
170 Next i
180 rsSource.MoveNext
190 Loop
200 Else
210 MsgBox "No Records in Input"
220 End If
230 rsSource.Close
240 Set rsSource = Nothing
250 rsOut.Close
260 Set rsOut = Nothing
270 Set db = Nothing
PROC_Exit:
280 Exit Sub
PROC_Error:
290 'On Error GoTo 0 ' comment out 300 if no message box is needed
300 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in SplitSerials procedure"
310 On Error GoTo 0 ' clear error
Resume PROC_Exit:
End Sub