Mention the idea of placing more than one value in a field to a relational database programmer and you might get a negative response like, Are you crazy? After all, in the rules of normalization, storing multiple values in a field is simply not allowed. Of course, Lotus Notes proprietary database system isnt relational, so you dont need to worry too much about following the rules of normalization. With Notes, youll spend less time dealing with the rules of normalization, which should allow you to develop the application faster and possibly free up more time to design a better application for the user. Now, that is not to say that the transaction scalability of the application will be as good as a relational database system. In general, Notes applications dont require large transaction
volumesand that is the assumption taken here. The bottom line is Notes will allow you to create applications quickly and easily, and multivalue fields contribute to that speed and ease. But, you will need to understand what multivalue fields are and how to use them (from a user and developer perspective) to get the most from them. The purpose of this article is to show you how to use and program multivalue fields. (Please note that some user and designer knowledge of Notes is assumed.)
The Basics of Using Multivalue Fields
A multivalue field is one that may contain a list of values. In Notes, the field will always be of a certain type such as Text, Number, or Date. Except for the Rich Text, ComboBox, Password, and Radio Button field types (which allow only one choice), all Notes field types can contain multivalues. You could think of a Notes multivalue field as an array, but much easier to use than an array when it comes to processing and displaying its values.
The designer of an application indicates that a field is a multivalue field by checking the Allow multiple values property on the Basics tab of the field properties dialog. (You access the property dialog for a field by right-clicking on the field when youre in design mode.)
For a user to enter multiple values in a field, requires the user to indicate an additional value by keying a separator character before keying the new value. For example, in text fields, a user can append a new value to the existing values by placing a comma (,) or semicolon (;) between the previous value and the new value. The text value of This is phrase one, this is phrase two entered into a multivalue text field would be considered as two values: This is phrase one and this is phrase two since a comma was used to
separate the values. Notice that spaces in multivalue text fields are not considered as separators. With numeric fields, you can separate values with a comma or semicolon, or additionally, a space. For example, 1 2 3 4 5 entered into a multivalue numeric field will create a number list containing five values.
Now, using separator characters to enter multivalues is one thing; displaying multivalue is another. You see, Notes doesnt necessarily display the separator character keyed at data entry time by the user. Notes has a number of options for the separator character that will be used to display or print a multivalue field on a Notes form (the electronic representation of a document). Valid separator character values specified by a developer for displaying multivalue fields are Space, Comma, Semicolon, New Line, and Blank Line. Semicolon is the default separator character. The number list mentioned in the previous paragraph by default would appear as 1; 2; 3; 4; 5 when displayed, no matter what separator character was used to separate the number at data entry time. The separator character used for displaying a multivalue field can be changed. To make the change, use the Display Separate Values With property of the field (found on the Advanced tabthe one with the propeller beanie of the field properties dialog) to select the separator of your choice.
When multivalue fields are used in a Notes view (somewhat similar to a spreadsheet) column, the view column will contain all values of the field separated by a comma or any other valid separator character specified by the developer. That is the case as long as the view column has enough horizontal or vertical space to display all the values.
In a view column, you have choices as to how multivalues are separated and displayed. The default separator character value for a view column is None, which causes the values to be separated with a comma. Its the same as if you were to specify a comma as the separator character. The valid separator character values a developer can specify for view columns are None, Space, Comma, Semicolon, and New Line. Choosing the New Line value will cause a line break in the view column for each value contained in the list. The result will be a vertical stack of values. However, if you use this option, be sure to specify that your view is to display more than one line per row (the Lines Per Row view property). Nine rows are the maximum number of lines you can display per view row on Notes clients. This limit does not exist, however, on the Web interface. In fact, the Line Per Row property has no effect on the Web interface.
If you sort a column in a view containing a multivalue field and you specify the sort type as Categorized, Notes will create a separate row for each value in the field. In other words, the document will appear in the view multiple times as if the database contained more documents than it actually does. For example, a Notes document contains a color option field named ColorOptions containing three values (i.e., Red, Green, and Blue). If the column is sorted and the sort type is specified as Categorized, the view will contain three rows for what is essentially one document. Figure 1 illustrates a view similar to the one just described. A single document categorized by its multivalue ColorOptions field, containing values Red, Green, and Blue, causes three rows to be displayed.
Multivalue Field Programming Searching and Retrieving
You can programmatically search for a value in a multivalue field using the @Contains function of the Notes formula language. For example, if the ColorOptions field contains three values (Green, Black, and Red), the following expression would return the value Red:
"Red;Red not found)
If Red was not in the ColorOptions field, the expression would return Red not found. You can determine the number of items in a multivalue field with the @Element
function. For example, the following expression will return the value 3 for the ColorOptions field mentioned in the previous paragraph.
You can retrieve a subset of a multivalue field with the @Subset function. For example, using the same ColorOptions field, the following expression will return the values Green and Black:
The second parameter value of 2 indicates that you want the first two values of the list starting from the left.
Using the same ColorOption field, this expression will return Black and Red.
The second parameter value of -2 indicates that you want the last two values of the list starting from the right.
For more powerful programming, you can use LotusScript to search for values in a multivalue field. (Note that, in all of the LotusScript examples in this article, Im working with the back-end database and not the front-end user interface.) For example, the following LotusScript code uses the Contains method (somewhat similar to the @Contains function used earlier) of the NotesItem class to determine if the value of Preferred is contained in a multivalue field named Type.
Dim item as NotesItem
Set item = GetFirstItem(Type)
If item.Contains(Preferred) Then
PreferredCtr = PreferredCtr + 1
NonPreferredCtr = NonPreferredCtr + 1
The next example accomplishes the same thing but uses a simple comparison in a looping technique instead of the Contains method.
Dim MultiValue as Variant
MultiValue = doc.GetItemValue(Type)
Forall v in MultiValue
If v = Preferred Then
PreferredCtr = PreferredCtr + 1
NonPreferredCtr = NonPreferredCtr + 1
When using the GetItemValue method (as Ive done in the previous code segment), its important to remember to define the receiving variable as type Variant.
Loading and Updating
You can programmatically load a value into a multivalue field using the Notes formula language @SetField function as illustrated here:
In the Notes formula language, the colon (:) is used as a list separator. So, here the value of the Product field (a field that may contain one or more values) and the value of the NextProduct field (also a field that may contain one or more values) are combined to form a list. The list is then used to set the value of Products.
To add a value to a multivalue text field with LotusScript, you can use the AppendToTextList method of the NotesItem class. For example, the following code, which is executed from a form button, will add the value of Purple to the multivalue field ColorOptions:
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim item As NotesItem
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
Set item = doc.GetFirstItem(ColorOptions)
To replace the values of a multivalue field with an entirely new list of values, you could use code similar to the following:
Dim NewVals (1 to 3) as String
Dim item as NotesItem
... set value of doc
Set item = doc.ReplaceItemValue( ColorOptions, Null)
NewVals(1) = Purple
NewVals(2) = Green
NewVals(3) = Blue
Since I want to replace values, I start by replacing the value of the ColorOptions field with Null, so the field is empty. The next three statements load the NewVals string array with three color values. The last statement appends the NewVal array (essentially, a multivalue list) to the ColorOptions field.
Displaying Multiple Values on Separate Lines on a Form
If you find you want to display the values of a multivalue field on separate lines in a Form, you can do so by implementing the following. Create a Computed For Display field whose value is obtained from the multivalue field (its Value property contains the multivalue field name). Use the property dialog for the field you just created to assign these properties: check the Allow multiple values property on the Basics tab and on the Advanced tab specify New Line as the value for the Display Separate Values property. As an option to using New Line, you could specify Blank Line, which would place an extra blank line between each value.
You can use this same technique to display computed multiple values. For example, you could use the @DbLookup function to compute a multivalue list by retrieving a column value from a view. The following expression could be used as the value for a Computed For Display field as described in the previous paragraph.
@DbLookup(:NoCache; servername : dbname;Products; UniqueID; 3);
This function returns the value of column three of the Products view for every row of data in the view where column one (the first sorted column of the view) matches the key
value in UniqueID field. Assuming more than one row matches the key, the system would return a multivalue list.
Stacking a Computed Multivalue List in a View
Assume you want to stack a customers address vertically in a Notes view row by using the New Line technique described earlier. However, the address isnt stored in a multivalue fieldits stored in six separate single-value fields. What you can do is create a multivalue list on the fly by concatenating the single value fields into a text list using the colon character.
Here is the expression for the view column:
Addr1 : Addr2 : Addr3 :
@Implode(City + " " +
State + " " + Zip)
What Im doing here is building a list and a text string from the six address fields. Im concatenating City, State, and Zip (including a space in between) because I want those three fields on the same line. Im using the @Implode function because I want the system to treat the City, State, and Zip fields as a text string, not a text list. If I removed the @Implode function, the system would treat City, State, and Zip as a text list and concatenate the values of State and Zip to the value of fields Addr1, Addr2, Addr3, and City.
Figure 2 (page 61) illustrates a view that uses the technique I just described.
Go Ahead. Break the Rules!
Notes multivalue fields allow you to break relational database rules, so you can create applications much more quickly. Hopefully, youve learned enough here to start using multivalue fields in Notes. I think youll find you can build applications in Notes more quickly than you ever thought possible with a relational database system.
Figure 1: If categorized in a Notes view, a document field containing multiple values will be displayed multiple timesonce for each value in the field.
Figure 2: The values in the Address column are derived from a computed text list and stacked vertically by specifying New Line as the separator character in the column properties.