Substr Function | Community
Skip to main content
Level 9
Jun 23, 2015 1:56 PM UTC
Question

Substr Function

I have a script that "walks" through all the records in an Access database table to find the largest file ID number. This script has been working correctly. Now I need to add a prefix to all the ID numbers. This requires me to modify my script so it ignores the one character prefix (prefix is either a "T" or a "Y"). I was trying to use the Substr function to accomplish this.

This is the original script that works:


while(!oDB.isEOF()){


if(xfa.record.DataConnection1.FileName.value > nMaxID)


  nMaxID = Number(xfa.record.DataConnection1.FileName.value);


oDB.next();


}


Below is how I tried to modify the script above to ignore the prefix but I get an error that says Substr is not defined. What have I got wrong?


while(!oDB.isEOF()){


if(xfa.record.DataConnection1.FileName.value > nMaxID)


  nMaxID = Number(Substr(xfa.record.DataConnection1.FileName.value,2,12);


oDB.next();


}


This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

16 replies

Magus069
Level 10
Jun 23, 2015 7:05 PM UTC

Yes it can be used in JavaScript... Just one last thing, make sure that your value is a String before using substr.. so try using value.toString().substr(2,5)

Translate
jasotastic81
Level 8
Jun 23, 2015 7:05 PM UTC

Here's the method working using JavaScript.

Maybe the problem is with how the method works. The first parameter is the starting location of the substring, and the second is the number of characters to take. So, from your example, you start with character 2 and take 12 characters. If you only want to ignore the first character, try substr(1,12). That starts at the first character in the string. (Keep in mind that characters in the string are numbered starting from 0.)

Translate
DKinsleyAuthor
Level 9
Jun 23, 2015 6:24 PM UTC

substr appears to be a Formcalc method only - not JavaScript. Your suggestion stopped the error but it does not appear to be doing what i expected. Are you sure it can alos be used in JavaScript?

Translate
jasotastic81
Level 8
Jun 23, 2015 3:22 PM UTC

substr is a method in JavaScript. Should you use xfa.record.DataConnection.FileName.value.substr(2,12) ?

Translate
DKinsleyAuthor
Level 9
Jun 23, 2015 3:06 PM UTC

Unfortunately, with the lower case "s" I am still getting the substr undefined error.

Translate
Magus069
Level 10
Jun 23, 2015 2:57 PM UTC

Hi there,

If I'm right, the substr function does not have a capital S...

while(!oDB.isEOF()){ 

     if(xfa.record.DataConnection1.FileName.value > nMaxID) 

          nMaxID = Number(substr(xfa.record.DataConnection1.FileName.value,2,12); 

     oDB.next(); 

} 

Translate