Hi TJ,
Sorry for the delayed response, this has been a hectic
week at work.
Listed at the end of this message is the code for the
function that I use (watch the line wrapping). You can
paste this code (repair the wrapping though) and then
call it in a query such as ParseArray
([FieldToBeParsed],3) - which would return the 3rd field.
The function also has two optional input parameters, one
specifying the ASCII code for the delimeter (mine is a
pipe which has the code 10), and another to specify
whether to add a prefix with the field number and a dash.
If you used the function, you would want to change the
default delimiter value in the function definition line
to match your case. If you don't use the code, you can
use the Asc() function or look it up in the character map
in help.
I put together this function as a test sometime back and
never really went back over it to see if I could have
made it more efficient. In looking at it now I think it
is likely it could be tweaked to improve performance,
such as possibly using the replace function to get rid of
all of the delimiters before the one that you are
interested in, then using the instr() functions. But, it
did work fine when I tested it.
I didn't have the errorhandler in testing, but just added
it. I tried a new approach with the error handler here
which is just to return the error information to the
function call if an error is encountered. I did this
because otherwise if you test a function with a lot of
records and the function encounters an error, it keeps
notifying you for each record. In the past to avoid this
I would set the top values property of the query to 5
during testing, but I think this error handler should
prevent this.
Anyway, following is the function code. To use it just
create a new module and past the function in. Then, you
can type the function in queries, or you can insert it
using the builder if you go to functions and then click
on the name of your database. Post back if you have any
questions.
Public Function ParseArray(InputArrayField, OutputFieldNo
As Integer, Optional DelimiterCharCode As Integer = 10,
Optional InclFieldNo_Y_N As String = "N")
Dim ArrayBreakPos() As Integer, strPrefix As String
On Error GoTo ErrorHandler
If IsNull(InputArrayField) = True Then
ParseArray = Null
Exit Function
End If
If UCase(Left(InclFieldNo_Y_N, 1)) = "Y" Then
strPrefix = OutputFieldNo & " - "
Else
strPrefix = ""
End If
ReDim ArrayBreakPos(OutputFieldNo) As Integer
'Set the Break Position 0 (which is not a real break) as
0 to start searching the string initially
'At the start point of the string (1 character after the
previous break pos)
ArrayBreakPos(0) = 0
For i = 1 To OutputFieldNo
ArrayBreakPos(i) = InStr(ArrayBreakPos(i - 1) + 1,
InputArrayField, Chr(DelimiterCharCode), vbTextCompare)
If ArrayBreakPos(i) = 0 Then
If i < OutputFieldNo Then
ParseArray = Null
Else
ParseArray = strPrefix & Right
(InputArrayField, Len(InputArrayField) - ArrayBreakPos
(OutputFieldNo - 1))
End If
Exit Function
End If
Next
ParseArray = strPrefix & Mid(InputArrayField,
ArrayBreakPos(OutputFieldNo - 1) + 1, ArrayBreakPos
(OutputFieldNo) - ArrayBreakPos(OutputFieldNo - 1) - 1)
Exit Function
ErrorHandler:
ParseArray = "Error - " & Err.Number & " " &
Err.Description
End Function
Hope that helps.
-Ted Allen
-----Original Message-----
Thanks again, Ted. I don't know why my post didn't appear
in the newsgroup. I found a somewhat complicated set of
functions that will locate the nth delimeter, but I am
VERY interested in your function if it is at all less
cumbersome than what I've found.
.