Ch. 035 vbScript Field Scripting Reference

EDOCBUILDER > eDocBuilder Documentation Manual & Videos
Syntax  Function  Usage 
 ""  Mark text as a string  
"FullName" would output the text "FullName".
In contrast, FullName would be a variable, and
output the value of the variable.
 &  Append text and variable values together
 "Mr. " & "John Doe" would output "Mr.
John Doe"
"Mr. " & FullName would output "Mr. John
Doe" if FullName had a value of "John Doe"
 "\n"  Line feed  
Tells the document rendering to use a line feed. For example, "John\nSmith" will render as:
John
Smith
 ifs(ByVal condition As
Boolean, ByVal TrueValue
As String, ByVal
FalseValue As String)

If-then-else logic
for strings 

The condition can be a function that will return true or false. For example, 1=2 will return false, whereas 2-2 will return true. Or, if the variable FullName is "John Doe", the function FullName = "John Doe" will return true.

If the condition is true, then the TrueValue string
will be used. This can be another function, a string
value, or a variable reference. Logically, if the
condition is false, then the FalseValue will be used.

Example:
ifs(Telephone<>"", "Tel: " & Telephone
,"") & ifs(Extension<>"", "\nExt " &
Extension, "") & ifs(Mobile<>"",
"\nMobile: " & Mobile, "") &
ifs(Fax<>"", "\nFax: " & Fax, "")

If all the variables present (Telephone,
Extension, Mobile, and Fax) contain values,
the resulting output would look like this:

Tel: 123-456-7890\nExt 123\nMobile:
459-789-0123\nFax:321-654-9874

The composition module interprets the \n to be a
hard return, so the resulting text would be displayed
as such:
Tel: 123-456-7890
Ext 123
Mobile: 459-789-0123
Fax:321-654-9874

Now let's consider what will happen if Extension and Fax do not contain a value. The resulting text
will be:

Tel: 123-456-7890\nMobile: 459-789-
0123    
 ifn(ByVal condition As
Boolean, ByVal TrueValue
As Double, ByVal
FalseValue As Double)

If-then-else logic for numeric values
 Similar to ifs command.
Ifd(condition as boolean, trueValue as date, falseValue as date) If-then-else logic for date values Similar to ifs command.
 Upper(ByVal value As
String)
Convert to upper case  
Upper("john doe") will return "JOHN DOE"

If fullName equals "John Doe", Upper(fullName) will return "JOHN DOE"
 Lower(ByVal value As
String)
Convert to lower case   
Lower("JOHN DOE") will return "john doe". If fullName equals "John Doe", Lower(fullName)
will return "john doe"
 trim(ByVal str As
String)
 
Remove "empty" characters from either end of a string.
 trim(" John Doe") will return "John Doe"
 len(ByVal str As String)  
Returns the length of a string.
 len("John Doe") will return 8
 Bar128(ByVal str As String)  
Encodes a string for use with the Code 128 barcode font.
 
OBSOLETE: Please use the Barcode field type instead, which natively supports many more types of barcode.
BarPostNet("Item #1257")
will add the start character, the checksum character and the stop character, as well as convert spaces and any other conversion necessary. 

In addition to using this function, you must also upload the Code 128 font to your template and apply it to the field. Please contact technical support if you have any difficulty in locating a Code128 font.
 left(ByVal value As String, ByVal n As Integer)  
Returns the lefttmost 'n' characters of a string.

left("John Doe",3) will return "Joh"

If fullName equals "John Doe", left(fullName,3) will return "Joh"    
 right(ByVal value As String, ByVal n As Integer)  
Returns the rightmost 'n' characters of a string.
right("John Doe",5) will return "n Doe"

If fullName equals "John Doe", right(fullName,5) will return "n Doe"
 substr(ByVal value As String, ByVal start as Integer, ByVal n As Integer)  
Returns the 'n' characters of a string, starting at position "start".
substr("John Doe", 3, 4) will return "hn D"

If fullName equals "John Doe", substr(fullName, 3, 4) will return "hn D"
 StartsWithVowel(ByVal value As String)  
Decides if a string starts with a vowel (a, e, i, o, u).
 
StartsWithVowel("Edward") will return "True" StartsWithVowel("John") will return "False"

Use in constructions like:  Ifs(StartsWithVowel(name), "an " & name, "a " & name)
 BarPostNet(ByVal str As String)
Encodes a string for use with the PostNet barcode font.
 
OBSOLETE: Please use the Barcode field type instead, which natively supports many more types of barcode.
BarPostNet("606091234 ")
will add the start character, the checksum character and the stop character. 

In addition to using this function, you must also upload the PostNet font to your template and apply it to the field. Please contact technical support if you have any difficulty in locating a PostNet font.
 
 setNumber
setPageNumber
runPageNumber
 Special variables for use in the vdp jobs
setNumber will be replaced with the record number within the VDP run.
setPageNumber will be replaced with the page number within this VDP record.
runPageNumber will be replaced with the overall page number (before imposition).

These variables can be used both in VBScript expressions and directly in HTML scripted fields (by surrounding with @ signs, e.g. @setNumber@).

For example, consider a run of 100 records where each record consists of three pages. If the second page in the template uses these three variables in a VBScript or an HTML scripted field, then for the 75th record of the production run, setNumber will be replaced with 75, setPageNumber will be replaced with 2, and runPageNumber will be replaced with 224.    
replace(target as string, search as string, replacement as string) Replace parts of strings Target is the string to look in. Search is the substring to look for. Replacement is what to replace it with.   replace("the dog is the best of the breed", "the", "a") will return "a dog is a best of a breed"
replaceregex(target as string, search as string, replacement as string) Replace parts of strings, using regular expressions to search Target is the string to look in. Search is the regular expression to look for. Replacement is what to replace it with. replaceregex("Charles Warlas", "l[ae]s", "lie") will return "Charlie Warlie" Regular expressions are powerful, but very complex. See a public site like http://www.regular-expressions.info/for detailed information and training.  
choose(x as string, choices as string, [default] as string) Multiple choice lookup
 
x is the value to be looked up. choices is a string of all the lookup possibilities and their results, alternating, separated by pipe characters "|". [default] is what should be returned if none of the choices match.   choose("dog","cat|Siamese|dog|Pekingese|horse|Arabian", "animal") will return "Pekingese" choose("ferret","cat|Siamese|dog|Pekingese|horse|Arabian", "animal") will return "animal"  
 
There are three arguments in a Choose function:
1) The field being used to lookup
2) Each possible value of the lookup field and each corresonding value you want to insert in the current field, separated by pipes
3) The value you want in the current field if none of them match.
Sample:
Choose(Location, "Chicago|123 Main St.|St. Louis|123 Madison Ave.", "")
Explanation:
If the Location field’s value is Chicago, populate this field with 123 Main St.  If the Location field’s value is St. Louis, populate this field with 123 Madison Ave.  If neither of these match the value of the Location field, then leave the current field blank.
 
 
superscriptOrdinal(input as string) Add superscripts to ordinal numbers, like 1st 2nd 3rd superscriptOrdinal("This is the 5th time I have tried the 31st choice") will return "This is the 5<sup>th</sup> time I have tried the 31<sup>st</sup> place"
smallCaps(src as string, fontsize as decimal) Convert lowercase to small caps The fontsize specifies how large the small caps will be. The true capital letters, therefore, need to have their size set outside of this function, either in the field format tab, or in other HTML.  
 smallCaps("Call the FBI and Tell Them We've Got Dillinger") will return
"C<FONT FONTSIZE='12'>ALL THE&nbsp;</FONT>FBI<FONT FONTSIZE='12'>&nbsp;AND&nbsp;</FONT>T<FONT FONTSIZE='12'>ELL&nbsp;</FONT>T<FONT FONTSIZE='12'>HEM&nbsp;</FONT>W<FONT FONTSIZE='12'>E'VE&nbsp;</FONT>G<FONT FONTSIZE='12'>OT&nbsp;</FONT>D<FONT FONTSIZE='12'>ILLINGER</FONT>"
sin(v as double)   Sine of angle in radians. sin(0.5)
cos(v as double)   Cosine of angle in radians.
Now()   Current date and time
Today()   Current date (without time)
Rnd()   Random number between 0 and 1
LeftTrim(str as string)   Trim white space from left of string.
 LeftTrim("   giraffe  ") returns "giraffe  "
RightTrim(str as string)   Trim white space from right of string
 RightTrim("   giraffe  ") returns "   giraffe"
PadLeft(str as string, wantedlen as integer, addedChar as string = " ")  
Adds spaces to left of string until it is at least wantedlen characters long. If provided, addedChar will be used instead of spaces.
 PadLeft("dog",5," ") returns "  dog"
 PadLeft("17",5,"0") returns "00017"
If you want to use spaces as the added character, make sure to turn on the "Preserve Spaces" setting on the format tab.  You can use this with the contents of a field as well.  For example:
PadLeft(dog,5, " ") returns the contents of the dog field padded by spaces on the left.  The number of spaces will vary depending upon the length of the contents of the dog field.  For example, if the dog field is 3 characters in length, then 2 spaces would be added to the left of the text in the dog field.
Mod(x as double, y as double)   Returns modulus - i.e., the remainder when x is divided by y. Mod(17,7) = 3 Mod(12,4) = 0
Format(x as double, fmt as string) Format numbers
Formats a number using a format string. The format strings are defined by Microsoft here.

Example:
  format(3713.192, "#,##0.##")
  result: 3,713.19
format_date(d as Date, fmt as String) Format dates
Use these placeholders in your format string. NOTE: All language elements (day and month names, ordinal suffixes, etc.) are English only.
  w    named day of week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
  x    named day of week, first three letters: Sun, Mon, Tue, Wed, Thu, Fri, Sat
  d    ordinal day number of month: 1st, 2nd, 3rd, 4th, etc.
  e    day number of month, no leading zero (1, 2, 3, ... 10, 11, ... 31)
  f    day number of month, leading zero (01, 02, 03 ... 10, 11, ... 31)
  m   named month (January, February, etc.)
  n    month number, no leading zero (1, 2, 3 ... 9, 10, 11, 12)
  o    month number, leading zero (01, 02, 03 ... 09, 10, 11, 12)
  p    named month, first three letters: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  y    full year (2013)
  z    last two digits of year (2013 = 13, 2005 = 05)

Any other characters in your string will be output exactly as given.

Example:
  format_date(today, "m e, y")
  result: January 2, 2014
(Please note that "today" is not a field name.  It is a built in to the VB script library that determines the current date.)
 
 


Previous Chapter 34