Storing multiple item values from a multi-value fields to an Array

How do you store all values from a multi-value field for all documents you are processing? I am trying the following code but my values are always overwritten.

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim item As NotesItem



Dim duplicatedNameIDX As Integer

Dim duplicatedNames() As String

Dim cleanNames As Variant

Redim duplicatedNames(0)



Set db = session.CurrentDatabase	

Set view = db.GetView("ConversionApproverNames")	

Set doc = view.GetFirstDocument



Do While Not doc Is Nothing

	Set item = doc.GetFirstItem( "Approver1")	

	varApprovers = 	item.Values	  'save the approver names off

	Redim Preserve duplicatedNames(Ubound(duplicatedNames) +Ubound(varApprovers) )

	For duplicatedNameIDX = 0 To Ubound(varApprovers)

		strCommonName = Cstr(varApprovers(duplicatedNameIDX))

		duplicatedNames(duplicatedNameIDX) = strCommonName

	Next

	Set doc = view.GetNextDocument(doc) 

	intRecordsProcessed = intRecordsProcessed + 1

Loop

Subject: storing multiple item values from a multi-value fields to an Array

Your loop starts at duplicatedNames(0) each time through - try this

Do While Not (doc Is Nothing)

Set item = doc.GetFirstItem( “Approver1”)

varApprovers = item.Values 'save the approver names off

Redim Preserve duplicatedNames(Ubound(duplicatedNames) +Ubound(varApprovers) )

For duplicatedNameIDX = (ubound(duplcatedNames) - ubound(varApprovers)) To Ubound(duplcatedNames)

strCommonName = Cstr(varApprovers(ubound(duplcatedNames) -duplicatedNameIDX))

duplicatedNames(duplicatedNameIDX) = strCommonName

Next

Set doc = view.GetNextDocument(doc)

intRecordsProcessed = intRecordsProcessed + 1

Loop

Subject: RE: storing multiple item values from a multi-value fields to an Array

It spins through all of the items fine but when it goes to moe the strCommonName to duplicatedNames(duplicatedNameIDX) it overlays what it there. I stepped though the debugger and I think the problem could be that the upperbound of the varApprovers is often times 0. So when it sets the range for the index (the from) it sets back to 0. I tried various things but cannot get it to work.

Subject: storing multiple item values from a multi-value fields to an Array

Hi Michael,

The problem is in the line:

duplicatedNames(duplicatedNameIDX) = strCommonName

This means that you are using duplicatedNameIDX as the array index, but this

always starts at 0.

You are redimming the array correctly to have extra slots after the end of the

current array, but you have to write to these slots.

So, before you redim the array, set an integer to the Ubound of the array, then

add this figure to duplicatedNameIDX to access the slots higher up in the

array, as follows:

varApprovers = item.Values 'save the approver names off

iStart = Ubound(duplicatedNames)

Redim Preserve duplicatedNames(Ubound(duplicatedNames) +Ubound(varApprovers) )

For duplicatedNameIDX = 0 To Ubound(varApprovers)

strCommonName = Cstr(varApprovers(duplicatedNameIDX))

duplicatedNames(iStart + duplicatedNameIDX) = strCommonName

Next

HTH

-Brendan

Subject: solution

Dim lbd As IntegerDim ubd As Integer

Dim duplicatedNames As Variant

Dim varApprovers As Variant

Set doc = view.GetFirstDocument

Do While Not doc Is Nothing

Set item = doc.GetFirstItem( “Approver1”)

varApprovers = item.Values 'save the approver names off

If Isarray(duplicatedNames) Then

lbd = Ubound(duplicatedNames) + 1

ubd = Ubound(duplicatedNames) + Ubound(varApprovers)

For duplicatedNameIDX = lbd To ubd

If duplicatedNameIDX = 0 Then Redim duplicatedNames(duplicatedNameIDX)

If duplicatedNameIDX > 0 Then Redim Preserve duplicatedNames(duplicatedNameIDX)

strCommonName = Cstr(varApprovers(duplicatedNameIDX))

duplicatedNames(duplicatedNameIDX) = strCommonName

Next

Else

duplicatedNames = varApprovers

duplicatedNameIDX = Ubound(duplicatedNames)

End If

lbd = 0

ubd =0

Set doc = view.GetNextDocument(doc)

intRecordsProcessed = intRecordsProcessed + 1

Loop

Subject: RE: solution

This solution worked great. Thank you so very much. :slight_smile: