Q
Problem solve Get help with specific problems with your technologies, process and projects.

How can I remove one element in a field without removing all values and replacing them?

I want to remove a element from a multi-valued field (specifically, the third element).

myfield(0)-"red"
myfield(1)="blue"
myfield(2)="white"
myfield(3)="green"
I want to remove element "white" from the field. How can this be done in script without removing all of the values and replacing them?
Either use Evaluate (inefficient, and the formula can be complex) or write LotusScript code to remove the unwanted item from the array, then assign the field from the array.

As with any other task that's more complex than you might like, you can make it easier on yourself by modularizing the code into reusable bits. For example, you could use the following routine:

Sub RemoveElementFromField
(doc As NotesDocument, Byval 
fieldname As String, Byval index As Integer) 
        Dim value As Variant, i As Integer 
        value = doc.GetItemValue(fieldname) 
        If index > 0 And index 
<= Ubound(value) Then 
                If Ubound(value) = 0 Then 
                        doc.
ReplaceItemValue fieldname, "" 
                Else 
                        Redim Preserve 
value(0 To Ubound(value) - 1) 
                        For i = index To Ubound(value)-1 
                                value(i) = value(i+1) 
                        Next 
                        doc.ReplaceItemValue 
fieldname, value 
                End If 
        End If 
End Sub
Once you have this, you can remove the value you wanted from the array using a statement such as:
RemoveValueFromField doc, "Colors", 2
If you wanted to be more general about it, you could write a routine to manipulate arrays:
Function ArrayRemove(arr As Variant, Byval ind As Integer) As Variant ' Returns the array with the element at index 'ind' taken out.
...
then (alternately) you could reassign t he field using the statement:
doc.Colors = ArrayRemove(myfield, 2)
Also, you could write a function to scan the array for the value you wanted to remove, for cases where you don't know the index:
Function SearchAndDestroy(arr As Variant, 
value As Variant, Byval compMethod As 
Integer) 
        Dim pos 
        pos = ArrayGetIndex(arr, value, compMethod) 
        If Isnull(pos) Then 
                SearchAndDestroy = arr 
        Else 
                SearchAndDestroy = ArrayRemove(arr, pos) 
        End If 
End Function 

This was last published in July 2003

Dig Deeper on LotusScript

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

Actually there is an failure in the function RemoveElementFromField.

The command "ReDim Preserve value(0 To UBound(value) - 1) " have to be after the For-loop.
Cancel

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close