String processing at runtime has its own special stumbling stones. If the programmer correctly anticipates them the result is a code that takes in account all possibilities considering the string.
The problem is this: You create a string buffer that has 12 characters and it is created from some sArray(counter) ... now it is not certain (and as programmer you should never assume it to be 100% certain) what exactly the string contents are.
Say that the 'buf' is " some funny bunny " .. then your  line makes I = 1 . (Because of the leading white space)
after this 'a' becomes - 1 . In your code line  makes a runtime error because your code attempts to take substring from 'buf' using negative length. Computers will never try to understand paradoxes and so you get the errors.
Now you should probably anticipate what you want to do if your buffer 'buf' contents are something else from what you expected.
Some General Ideas:
- firstly trim
all your strings. Trimming means that you cut away all leading as well as all tailing white space
characters from your string. I think VB has a function for this purpose
-secondly check the values of critical variables using if-statements. Like before the line  you should check in if-statement what is the value of 'a'. Then you make your code handle the situation when the value is out of accepted range.
PS. would be helpful if you mentioned the language in your post like stu_design mentioned. I guessed that you use VB and checked your functions with google but still helpful if you mention.
Edited by scicatur, 03 June 2005 - 11:42 AM.