Redim preserve issue

I’m getting a viewentry collection and will email it. The first collection (document created) is correct, but the next keeps adding to the first collection. I’m using this from another piece of code that I have. I know the Preserve is doing this but I’m not sure how to write this one section. Thanks for any help…

If x = 0 Then

Redim tmp1(x)

Redim tmp2(x)

Redim tmp3(x)

Else

Redim Preserve tmp1(x)

Redim Preserve tmp2(x)

Redim Preserve tmp3(x)

End If

tmp1(x) = DocTwo.fied1(0)

tmp2(x) = DocTwo.field2(0)

tmp3(x) = DocTwo.field3(0)

Set Ventry = VeCol.GetNextEntry(Ventry)

x = x+1

Wend

NewDoc.fielda = tmp1

NewDoc.fieldb = tmp2

NewDoc.fieldc = tmp3

Subject: redim preserve issue

Please post the rest of your code. This appears to be working as designed. the array index keeps growing as you loop through it because you are adding one to x at the end of the loop. What is it that you’re trying to do?

Subject: RE: redim preserve issue

If I take the x = x+1 out then there is a problem. Say if I have 5 documents, all fives docs show in the collection or newdoc but only the first doc in the collections shows the 3 fields (fielda, fieldb, fieldc) on the newdoc. The other 4 docs (entries) show nothing for those fields.

Here is the first part…

Dim ViewTwo As NotesView

Dim Ventry As NotesViewEntry

Dim VeCol As NotesViewEntryCollection

Dim DocTwo As NotesDocument

Set db = session.CurrentDatabase

Set ViewTwo = db.Getview(“view1”)

Set VeCol = ViewTwo.GetAllEntriesByKey(key, True)

Set Ventry = VeCol.GetFirstEntry()

While Not (Ventry Is Nothing)

Set DocTwo = Ventry.Document

Thanks for the help!!!

Subject: RE: redim preserve issue

Where does NewDoc come from? If this is part of a collection then after you have collected the information in the arrays then you also need to loop through the collection to set the fields.

At the end of the loop you are only setting one documents values with the lines

NewDoc.fielda = tmp1

NewDoc.fieldb = tmp2

NewDoc.fieldc = tmp3

Also Redim Preserve is inefficient and it is best to avoid using it where possible. As you know the size of the array that you will need it would be better to remove all your Redim statements from the loop and set the arrays after you have got your collection.

Redim tmp1(VeCol.count - 1)

Redim tmp2(VeCol.count - 1)

Redim tmp3(VeCol.count - 1)

This is the code you have posted so far

Dim ViewTwo As NotesView

Dim Ventry As NotesViewEntry

Dim VeCol As NotesViewEntryCollection

Dim DocTwo As NotesDocument

Set db = session.CurrentDatabase

Set ViewTwo = db.Getview(“view1”)

Set VeCol = ViewTwo.GetAllEntriesByKey(key, True)

Set Ventry = VeCol.GetFirstEntry()

While Not (Ventry Is Nothing)

Set DocTwo = Ventry.Document

If x = 0 Then

Redim tmp1(x)

Redim tmp2(x)

Redim tmp3(x)

Else

Redim Preserve tmp1(x)

Redim Preserve tmp2(x)

Redim Preserve tmp3(x)

End If

tmp1(x) = DocTwo.fied1(0)

tmp2(x) = DocTwo.field2(0)

tmp3(x) = DocTwo.field3(0)

Set Ventry = VeCol.GetNextEntry(Ventry)

x = x+1

Wend

NewDoc.fielda = tmp1

NewDoc.fieldb = tmp2

NewDoc.fieldc = tmp3

Subject: RE: redim preserve issue

I’m looking to a view initially to get a document. That gets a few field values and creates the newdoc. At that point I look to the viewentry to get more fields values for the newdoc. Once done, it goes to the initial view and gets next doc to creat another newdoc. The problem is that the newdoc keeps adding the same info. I want to create the newdoc and get values then stop…go back to initial view get next doc, create newdoc with values…stop…get nextdoc, get next doc create newdoc and stop…back to initual view until doc is nothing…I hope that makes sense…

Subject: RE: redim preserve issue

I think I get the idea. You are creating a new document for every document in a view, copying some values to the new document and then adding the values abtained in your arrays from your viewentrycollection.

Can you post all your code as it is impossible to tell what is happening with what you have posted so far.

Subject: RE: redim preserve issue

Here is my full code. I need to insert a piece to loop through values of my field “key” building the array. I’m trying to use the viewentry collection to get the newdocs order. I’m not sure if this is possible with viewentry collection. Thanks!

Here is what I want to add to loop throught the field, building the array:

Dim item As NotesItem

Set item = NewDoc.GetFirstItem( “key” )

Forall v In item.Values ???

…here is the full code…

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Set db = session.CurrentDatabase

Dim rtitem As NotesRichTextItem

Dim key As Variant

x = 0

Dim DocOne As NotesDocument

Dim NewDoc As NotesDocument

Dim ViewOne As NotesView

Set ViewOne = db.Getview(“ViewOne”)

Set DocOne = ViewOne.GetFirstDocument

While Not (DocOne Is Nothing)

Set NewDoc = DB.CreateDocument

Set rtitem = New NotesRichTextItem(NewDoc, “Link”)

NewDoc.form = “form1”

NewDoc.sendto = DocOne.sendto(0)

NewDoc.type = DocOne.type(0)

NewDoc.title = DocOne.title(0)

NewDoc.subject = DocOne.title(0)

key = DocOne.group(0) - * need to loop through values of field building array - place values on Newdoc

Dim ViewTwo As NotesView

Dim Ventry As NotesViewEntry

Dim VeCol As NotesViewEntryCollection

Dim DocTwo As NotesDocument

Set db = session.CurrentDatabase

Set ViewTwo = db.Getview(“ViewTwo”)

Set VeCol = ViewTwo.GetAllEntriesByKey(key, True)

Set Ventry = VeCol.GetFirstEntry()

While Not (Ventry Is Nothing)

Set DocTwo = Ventry.Document

If x = 0 Then

Redim tmp1(x)

Redim tmp2(x)

Redim tmp3(x)

Else

Redim Preserve tmp1(x)

Redim Preserve tmp2(x)

Redim Preserve tmp3(x)

End If

tmp1(x) = DocTwo.fied1(0)

tmp2(x) = DocTwo.field2(0)

tmp3(x) = DocTwo.field3(0)

Set Ventry = VeCol.GetNextEntry(Ventry)

Wend

NewDoc.fielda = tmp1

NewDoc.fieldb = tmp2

NewDoc.fieldc = tmp3

Call NewDoc.Send( True, NewDoc.SendTo )

Set DocOne = ViewOne.GetNextDocument(DocOne)

Wend

Call rtitem.Update

End Sub

Subject: RE: redim preserve issue

In your full code you are getting the key from DocOne not NewDoc.

Dim item As NotesItem

Set item = NewDoc.GetFirstItem( “key” )

Forall v In item.Values

Is there any reason why you are building arrays? If you just want to copy the values to NewDoc then you can do something like the following

Forall v in Item.Values

get view entry collection that matches v

loop through collection

append field value from collection to new doc

end loop through collection

End Forall

Subject: RE: redim preserve issue

I get the first document from viewone and create the new doc. Then I go to view two and get the first item in the field - grab it’s field values for the new doc, go back to next item in field, grab it’s field values for the new doc, once done, go back to initial view, viewone and get next doc, then back to viewtwo…this is so tricky!

Subject: redim preserve issue

I don’t know if this will help or even if this is part of the issue, but I want to show you…

Sometimes I have an issue when redimming with counters, instead of using the counter in the redim, use …

Redim Preserve tmp1(ubound(tmp1) + 1)

You can still use the condition of the counter, but you are not using that to build the array.

Also, have you debugged this? What happens to the values in debug mode?

Subject: redim preserve issue

Make sure your loops are like this: ‘Redim preserve is fine’

…Initialize objects and set first view to get each document

set doc = view.GetFirstDocument (hope that’s what you are doing)

do until doc is nothing

set newdoc = db.CreateDocument

…

…set some of the field values of newdoc from doc

…set the ‘key’

…

Set ViewTwo = db.Getview(“view1”)

Set VeCol = ViewTwo.GetAllEntriesByKey(key, True)

Set Ventry = VeCol.GetFirstEntry()

While Not (Ventry Is Nothing)

…process…

…Get tmp1, tmp2, tmp3

…

Wend

NewDoc.fielda = tmp1

NewDoc.fieldb = tmp2

NewDoc.fieldc = tmp3

Call NewDoc.Save(True, True)

set doc = view.GetNextDocument(doc)

x = 0

Loop

This way you can get the tmp values into first NewDoc, save it and reinitialize x to zero. Get the next document, Create new document again in this first loop(Do until…), get your view entry collection, loop thru it(while loop…) and set tmp1, tmp2, tm3 values again and save it in newdoc…and so on.

Hope this helps.

Subject: RE: redim preserve issue

Narasimhan and others…

Thanks for the reply. My code is doing what you are saying. I’m having a hard time though because of my key. It’s a multivalue field that should be put into an array. I know I need to get a hold of the item and loop through it. The newdoc should have all the entries where the key matched and place their values down the newdoc. Once done go back to the initial view then over the next view where the key is an array, get all docs where key matches and put on the new doc and so on…until nothing.

I see how getfirstitem work with the ForAll. I assume I don’t need getfirstentry?