Question

I have a field called 'Specimen' that has entries formatted like 'CM-Z-01', 'TOR-XY-03', etc. I want to populate 2 additional fields in the same table, called 'TestType' and 'Axis', with the first two sections of the 'Specimen' entry, respectively. So 'TestType' would have 'CM' and 'TOR'; Axis would have 'Z' and 'XY'

I can do the first one just fine, with

UPDATE MechanicalData
    SET MechanicalData.TestType = Left(Specimen,InStr(Specimen,"-")-1);

Is there an easy way to grab the middle portion?

Was it helpful?

Solution

You can use the Split function which returns an array with the string parts:

firstPart = Split("TOR-XY-03", "-")(0)
secondPart = Split("TOR-XY-03", "-")(1)

Make a function that you can call in your query based on this function. You cannot use Split directly as it is not supported in SQL.

Public Function SpecimenPart(ByVal s As Variant, ByVal partNo As Long) As Variant
    If Nz(s) <> "" Then
        Dim parts As Variant
        parts = Split(s, "-")
        If partNo - 1 <= UBound(parts) Then
            SpecimenPart = parts(partNo - 1)
        End If
    End If
End Function

The parameter partNo is one-based (1 for first part).

UPDATE MechanicalData SET
    TestType = SpecimenPart(Specimen, 1),
    Axis =  SpecimenPart(Specimen, 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top