Hello,
I have 5 simialr fields, each field is either null, has 1 value or multiple values separted by commas.
The system which is producing this excel extract had a character limit, when reached, the string would proceed in the next column.
Goal: to concatenate all the unique container numbers into the 'Containers' field, keeping a comma between each value.
Challenges:
some fields are null then the field after has data
some values end in comma, some do not
a 128 char limit in the source system caused the values to split across multiple fields, so these needs to be concatenated without a separating comma (see red highlight)
The long text field size with rich text format still seems to be truncating or not having enough space for more than 256 chars (see blue highlight)
Here is my code: Desparately hoping for some help.
[BEGIN CODE]
Sub Process_Containers()
Dim rst As Recordset
Set rst = currentdb.OpenRecordset(Name:="tblImport")
Dim C23 As String
Dim C34 As String
Dim C45 As String
If Len(rst![container no2]) = 128 And IsNull(rst![Container No3]) = True Then
C23 = ""
ElseIf Len(rst![container no2]) = 128 And IsNull(rst![Container No3]) = False Then
C23 = ""
Else
C23 = ","
End If
If Len(rst![Container No3]) = 128 And IsNull(rst![Container No4]) = True Then
C34 = ""
ElseIf Len(rst![Container No3]) = 128 And IsNull(rst![Container No4]) = False Then
C34 = ""
Else
C34 = ","
End If
If Len(rst![Container No4]) = 128 And IsNull(rst![Container No5]) = True Then
C45 = ""
ElseIf Len(rst![Container No4]) = 128 And IsNull(rst![Container No5]) = False Then
C45 = ""
Else
C45 = ","
End If
rst.MoveFirst
Do Until rst.EOF
rst.Edit
If Right(rst![container no2], 1) = "," Then
rst![Containers] = rst![container no2] & C23 & rst![Container No3] & C34 & rst![Container No4] & C45 & rst![Container No5]
Else
rst![Containers] = rst![container no1] & "," & rst![container no2] & C23 & rst![Container No3] & C34 & rst![Container No4] & C45 & rst![Container No5]
End If
rst.Update
rst.MoveNext
Loop
End Sub
[END CODE]
Then I run this code 4 times to remove trailing commas
[BEGIN CODE]
Sub TrimCommas()
Dim rst As Recordset
Set rst = currentdb.OpenRecordset(Name:="tblImport")
rst.MoveFirst
Do Until rst.EOF
' If IsNull(rst![container no1]) = True Then
' rst.MoveNext
' Else
If Right(rst![Containers], 1) = "," Then
rst.Edit
rst![Containers] = Left(rst![Containers], Len(rst![Containers]) - 1)
rst.Update
Else
' Exit Sub
' End If
End If
rst.MoveNext
Loop
End Sub
[END CODE]
I am hopeful there is a better way to get rid of the trailing commas as well.
Thanks in advance your assistance!
I have 5 simialr fields, each field is either null, has 1 value or multiple values separted by commas.
The system which is producing this excel extract had a character limit, when reached, the string would proceed in the next column.
Goal: to concatenate all the unique container numbers into the 'Containers' field, keeping a comma between each value.
Challenges:
some fields are null then the field after has data
some values end in comma, some do not
a 128 char limit in the source system caused the values to split across multiple fields, so these needs to be concatenated without a separating comma (see red highlight)
The long text field size with rich text format still seems to be truncating or not having enough space for more than 256 chars (see blue highlight)
Here is my code: Desparately hoping for some help.
[BEGIN CODE]
Sub Process_Containers()
Dim rst As Recordset
Set rst = currentdb.OpenRecordset(Name:="tblImport")
Dim C23 As String
Dim C34 As String
Dim C45 As String
If Len(rst![container no2]) = 128 And IsNull(rst![Container No3]) = True Then
C23 = ""
ElseIf Len(rst![container no2]) = 128 And IsNull(rst![Container No3]) = False Then
C23 = ""
Else
C23 = ","
End If
If Len(rst![Container No3]) = 128 And IsNull(rst![Container No4]) = True Then
C34 = ""
ElseIf Len(rst![Container No3]) = 128 And IsNull(rst![Container No4]) = False Then
C34 = ""
Else
C34 = ","
End If
If Len(rst![Container No4]) = 128 And IsNull(rst![Container No5]) = True Then
C45 = ""
ElseIf Len(rst![Container No4]) = 128 And IsNull(rst![Container No5]) = False Then
C45 = ""
Else
C45 = ","
End If
rst.MoveFirst
Do Until rst.EOF
rst.Edit
If Right(rst![container no2], 1) = "," Then
rst![Containers] = rst![container no2] & C23 & rst![Container No3] & C34 & rst![Container No4] & C45 & rst![Container No5]
Else
rst![Containers] = rst![container no1] & "," & rst![container no2] & C23 & rst![Container No3] & C34 & rst![Container No4] & C45 & rst![Container No5]
End If
rst.Update
rst.MoveNext
Loop
End Sub
[END CODE]
Then I run this code 4 times to remove trailing commas
[BEGIN CODE]
Sub TrimCommas()
Dim rst As Recordset
Set rst = currentdb.OpenRecordset(Name:="tblImport")
rst.MoveFirst
Do Until rst.EOF
' If IsNull(rst![container no1]) = True Then
' rst.MoveNext
' Else
If Right(rst![Containers], 1) = "," Then
rst.Edit
rst![Containers] = Left(rst![Containers], Len(rst![Containers]) - 1)
rst.Update
Else
' Exit Sub
' End If
End If
rst.MoveNext
Loop
End Sub
[END CODE]
I am hopeful there is a better way to get rid of the trailing commas as well.
Thanks in advance your assistance!