Select to view content in your preferred language

A script for advance label expression

929
6
Jump to solution
09-24-2012 12:21 AM
SiyangTeo
Deactivated User
Hi, I was wondering if anyone can help with a specific script for labelling the below. I am totally new to VBscript so any help will be greatly appreciated!

There are two fields: [Animal] & [Species_Name]. E.g.: [Animal] = Frog, [Species_Name] = Rana catesbeiana Shaw, 1802

How could I make the label appear like this below?

Frog
Rana catesbiana
Shaw,1802

Basically the font size for the frog is set and bold, and the second field is text wrapped after the second spacing.

Thank u in advance!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
In this case the following script should do the work:
Function FindLabel ( [Animal], [Species_Name] )   Dim SpNameArray   SpNameArray = Split([Species_Name])   Dim Authors   Authors = ""   For i=2 to UBound(SpNameArray)     Authors = Authors & SpNameArray(i) & " "    Next   FindLabel = "<BOL><FNT name='Arial' size='12'>" & [Animal] & "</FNT></BOL>" & _   "<FNT name='Arial' size='10'><ITA>" & vbnewline & _    Split( [Species_Name]) (0) & " " & Split( [Species_Name] )(1) & "</ITA>" & _    vbnewline & Authors & "</FNT>" End Function

Here's the result:
[ATTACH=CONFIG]17952[/ATTACH]

This script puts two first words from [Species_Name] in one line and the rest words from this field in second line.

View solution in original post

0 Kudos
6 Replies
MarcinGasior
Frequent Contributor
Try the following code in Label Expression with Advanced marked:
Function FindLabel ( [Animal], [Species_Name] )
  FindLabel = "<BOL><FNT name='Arial' size='12'>" & [Animal] & "</FNT></BOL>" & _
  "<FNT name='Arial' size='10'>" & vbnewline & _ 
  Split( [Species_Name]) (0) & " " & Split( [Species_Name] )(1) & _ 
  vbnewline & _
  Split( [Species_Name] )(2) & " " & Split( [Species_Name] )(3) & "</FNT>"
End Function

This code, after setting Left Horizontal Alignment in Symbol properties, gives me this result:
[ATTACH=CONFIG]17913[/ATTACH]

You can read more about advanced labeling and formatting tags here.
0 Kudos
SiyangTeo
Deactivated User
That's really useful! I think I can decipher most of what you wrote there.

However, my field [Species_Name] contains a few differing names, some with 2 spaces, 3 spaces and so on.

For example, three of the [Species_Name] are:

Cassia fistula L.
Andrographis paniculata (Burm. f.) Wall. ex Nees
Fittonia albivensis (Lindl. ex hort. Veitch)

They will not show up in the labels since the expression did not specify the number of spaces. How do edit your code so that only the first two characters are at the first line while the remainder in the second?

Specifically, how do I make them like this:

Frog
Cassia fistula
L.

Frog
Andrographis paniculata
(Burm. f.) Wall. ex Nees

Frog
Fittonia albivensis
(Lindl. ex hort. Veitch)

Thanks once again for the help.
0 Kudos
MarcinGasior
Frequent Contributor
In this case the following script should do the work:
Function FindLabel ( [Animal], [Species_Name] )   Dim SpNameArray   SpNameArray = Split([Species_Name])   Dim Authors   Authors = ""   For i=2 to UBound(SpNameArray)     Authors = Authors & SpNameArray(i) & " "    Next   FindLabel = "<BOL><FNT name='Arial' size='12'>" & [Animal] & "</FNT></BOL>" & _   "<FNT name='Arial' size='10'><ITA>" & vbnewline & _    Split( [Species_Name]) (0) & " " & Split( [Species_Name] )(1) & "</ITA>" & _    vbnewline & Authors & "</FNT>" End Function

Here's the result:
[ATTACH=CONFIG]17952[/ATTACH]

This script puts two first words from [Species_Name] in one line and the rest words from this field in second line.
0 Kudos
SiyangTeo
Deactivated User
Thanks! I'm learning quite a lot of vbscript through your help.

Actually I found that this function of yours earlier works best for me, since the authorship of the species is redundant for my purpose. I modified slightly to become this:

Function FindLabel ( [Animal] , [Species_Name] )
  FindLabel = "<BOL><FNT name='Arial' size='12'>" & [Animal] & "</FNT></BOL>" & _
  "<FNT name='Arial' size='10'>" & vbnewline &_
  Split ( [Species_Name] ) (0) & " " & Split ( [Species_Name] ) (1)
End Function

So:

Frog
Rana catesbiana
Shaw,1802

Will become this instead:

Frog
Rana catesbiana

However, I encountered another problem. I found out that when a species names [Species_Name] is not listed, the entire point for it will not be labelled at all. How can I modify this code so that when such a case happen, the label will show only the [Animal] field?

For this case, it means that only Frog will be shown when it has no species name.

Thanks again in advance.
0 Kudos
MarcinGasior
Frequent Contributor
I suppose the easiest way will be to create additional Label Class (in Label Manager) and apply SQL Query to chose only those record with empty [Species_Name] field and then use label expression with only first part.
The Default Label Class should have SQL Query specified to select only those record with [Species_Name] not empty.
0 Kudos
SiyangTeo
Deactivated User
Thanks! It works now~

Appreciate the help 🙂
0 Kudos