A family of Microsoft relational database management systems designed for ease of use.
My objective is to pull the two, three, or four digits after the first hyphen and before the period.
I'd wrap the logic in a little function:
Public Function GetMaterialCode(varDescription)
Dim strDescription As String
If Not IsNull(varDescription) Then
If InStr(varDescription, "-") > 0 Then
strDescription = Mid(varDescription, InStr(varDescription, "-") + 1)
If InStr(varDescription, ".") > 0 Then
strDescription = Left(strDescription, InStr(strDescription, ".") - 1)
Else
strDescription = Left(strDescription, InStr(strDescription, "-") - 1)
End If
GetMaterialCode = strDescription
End If
End If
End Function
You can see how it works in the debug window by entering your sample values:
? GetMaterialCode("8100-248-160-160-36")
248
? GetMaterialCode("8177-234.1-160-80-24")
234
? GetMaterialCode("34 Short with No Chamfer")
? GetMaterialCode("21 Long with Chamfer")
In the last two Null is returned. You would still get an error, however, if a value contained only one hyphen, e.g.
? GetMaterialCode("42 Long with Half-Chamfer")
would return an 'invalid procedure call or argument' error on this line:
strDescription = Left(strDescription, InStr(strDescription, "-") - 1)
but it would be a simple task to trap this in the function if necessary.
However, I would take serious note of what John says. You are using the column as a data structure here. Each column position in a row in a table should contain one value, and one value only of the attribute type modelled by the column. To do otherwise violates a basic principle of the database relational model, Codd's Rule #2, The Guaranteed Access Rule:
Every datum (atomic value) in a relational database is guaranteed to be logically accessible by resorting to a combination of table name, primary key value and column name.
Even though the data is imported you should really avoid the iceberg rather than moving the deckchairs on the Titanic, by parsing it into its constituent elements before inserting rows into a correctly designed table. The non-conforming values like '34 Short with No Chamfer' would best go into a column of their own. The parsing of the data could be automated by writing a set of suitable functions into each of which the original value is passed.