Let me be sure I understand. The initial query to initially select
records would look something like this (searching for the year/date
fragment followed by a letter):
SELECT T.*
FROM TableName AS T
WHERE T.FieldWithCommaValues Like "*" &
Format(Date(), "yym") & "[a-z]*";
However, the trick now is to extract the appropriate "year/date" and
"trailing letter" fragment from the FieldWIthCommaValues. The problem is
that 071 will "match" both a 071 and a 0711 string. It's not really
possible to do this with the built-in VBA functions (InStr, etc.), so
we'll need to use a user-defined function to extract the desired
fragment, convert the "region" letter to a number, and return it to the
query.
Put the following function in a regular module in your database file:
Public Function GetRegionValue(ByVal strFieldValue As String, _
ByVal strYearMonth As String) As String
Dim lngLoc As Long, lngLong As Long
Dim strTemp As String, strLetter As String
lngLoc = 1
lngLong = Len(strFieldValue)
Do
lngLoc = InStr(lngLoc, strFieldValue, strYearMonth, vbTextCompare)
strTemp = Mid(strFieldValue, lngLoc, Len(strYearMonth) + 1)
strLetter = Right(strTemp, 1)
If strLetter Like "[a-z]" Then Exit Do
lngLoc = lngLoc + 1
If lngLoc > lngLong Then
GetRegionValue = "invalid Region"
Exit Function
End If
Loop
GetRegionValue = "Region " & CStr(Asc(UCase(strLetter)) - _
Asc("A") + 1)
Exit Function
End Function
Then we need to call this function in your query:
SELECT T.*,
GetRegionValue(T.FieldWithCommaValues, Format(Date(), "yym"))
AS RegionNumber
FROM TableName AS T
WHERE T.FieldWithCommaValues Like "*" &
Format(Date(), "yym") & "[a-z]*";
I've not tested/debugged this function, so you may find it needs a bit of
tweaking.
I assume that you know that this field's contents violate normalization
rules for a relational database because you are not storing a single
value in a single field in a single record. If you were using a
normalized structure, the query to do what you seek would be so very much
easier and less complex, and wouldn't require the use of the user-defined
function.
--
Ken Snell
<MS ACCESS MVP>
ted said:
Can you provide us with some examples of the data and what you want to
do with the data?
The field looks like this: ,071a,075b,0711e, ...
where the 07 is the year 2007, the number folowing the year is the month
& the alpha represents a region of the usa (1 thru 6)
The logic in basic: we would get the date() & extract the year & month.
then search the field for (ie ',075?,' for 2007 & may)
finding that we would extract the ? (alpha) and turn the a=1,b=2 ...
plan on printing on the label 'Region1' for the a=1 among the fields we
already print. TIA
--
Ken Snell
<MS ACCESS MVP>
We are printing labels from a table. However, we have a field with
info separated with comma's. Would like to search for the appropriate
info & transform it to different text. Suggestions for office xp or
perhaps there is something in office 2007 & we could upgrade. TIA
--
_______________________________
In Christ's matchless name
ted & colleen
n6trf kc6rue