How to create a temp table from concatenated fields that vary in length

BenMason

Member
Local time
Today, 06:53
Joined
May 14, 2021
Messages
42
I have a parts table. In it there are two fields that have string info (Customer and CPN). For instance:

Parts Table Example:

PartNoCustomerCPN
1678Yen/Stick/PearsonYen: 2321*Stick: 52344J/45*Pearson: Hy45
1679CarsonCy8911
1680
1681Harry's


In the example above, there are three customers that use the partNo 1678 separated by “/”, and the CPN is separated by an asterisk “*”

I want to create a temp table that looks like this:

PartNoCustomerCPN
1678Yen2321
1678Stick52344J/45
1678PearsonHy45




I tried strVarCust1 = InStr(1, [strPartCustomer1], "/") to count the position before the first slash in vba with not much luck because the Parts table can have a customer field that has one customer, or three or more customers or it can be blank. The same with the CPN field.

I think I need a way to determine how many “/” or customers are in the Customer field and how many CPN’s are in the CPN field. Then write it to a string variable and then write the string variable to the temp table. But not sure how to do this or if this would be the correct solution.

There must be an easy way to do this.

Please help.

Thanks,

Ben
 
Hi Ben. You should try the Split() function.
 
Code:
Public Function fnSplit(ByVal pString As String, ByVal pDelim As String, ByVal pItemNumber As Integer) As String
    Dim v As Variant
    If pItemNumber < 1 Then
        Exit Function
    End If
    v = Split(pString, pDelim)
    If UBound(v) + 1 >= pItemNumber Then
        fnSplit = Trim$(v(pItemNumber - 1))
    End If
End Function
 
The table Parts Table Example is not normalized and should be the temporary table.
 
Code:
Public Function fnSplit(ByVal pString As String, ByVal pDelim As String, ByVal pItemNumber As Integer) As String
    Dim v As Variant
    If pItemNumber < 1 Then
        Exit Function
    End If
    v = Split(pString, pDelim)
    If UBound(v) + 1 >= pItemNumber Then
        fnSplit = Trim$(v(pItemNumber - 1))
    End If
End Function
Thanks for the function. I copied it to my functions module. But then I am lost. I tried to create a query using it and received #error in the field. Here's the query: MyField1: fnSplit(1,[Customer],"/")

Ben
 
Thanks. I'm looking up the split() function.

Ben
Hi Ben. I'm sure Arnel will be able to help you with using it, as he showed in his sample function. Good luck!
 
The table Parts Table Example is not normalized and should be the temporary table.
You are right. The Parts table is not normalized. I'm trying to create a temp table but need to split the customer and PN and then write to the temp table. The splitting is where I am stopped. In the meantime I am creating a normalized CPN table that has the splits but it's very manual. Then I will have to find a way for the users to enter their updates directly in the new CPN table instead of concatenated as they are doing now.
 
Thanks for the function. I copied it to my functions module. But then I am lost. I tried to create a query using it and received #error in the field. Here's the query: MyField1: fnSplit(1,[Customer],"/")

Ben
You need to pay attention to the parameters that arnelgp's function expects?
 
MyField1: fnSplit([Customer],"/",1)
 

Users who are viewing this thread

Back
Top Bottom