Split text - Run-time error '9'

Leo_Polla_Psemata

Registered User.
Local time
Today, 03:26
Joined
Mar 24, 2014
Messages
364
I have a field (say which stores emails addresses.
Emails are separated with semicolon ";"
I use this field in a docmd.send object.

Now
I need to make a query and split this field into separate emails.

I found the below code in here
https://www.access-programmers.co.uk/forums/showthread.php?t=291823&highlight=InStr
[quote]
[code]
Function Splitstr(s as string, element as integer) as string
Splitstr=split(s,",")(element)
end function[/code]

then in your query

Dept:splitstr(myfield,0)
Unit:splitstr(myfield,1)
[/quote]

It works just fine, however, I get this error if the field [email] contain only one email and therefore the second field in the query gives #Error
If I fix field [email] to contain for example three emails (three ;) and the query has three columns then it works just perfect.

How can I fix the error ?

Run-time error '9'
Subscript out of range
 
Consider these amendments to the function, one which tests the length of the array, and the other which handles unexpected errors...
Code:
Function Splitstr(s as string, element as integer) as string
on error goto handler
   dim var
   var = split(s,",")
   if element <= ubound(var) then Splitstr=var(element)
   exit function
handler:
   Splitstr=err.description
end function
 

Users who are viewing this thread

Back
Top Bottom