Get all documents by key

I’m getting all documents by key. What is wrong with this loop? It’s returning the last document in the view, not all the docs.

The docs are found but not all shown in the new doc2.

If collection.count > 0 Then

	Set doc2 = db.CreateDocument

	doc2.form = "Form2"

	

	Set doc1 = collection.GetFirstDocument

	Do Until doc1 Is Nothing

	

		

	doc2.fname = doc1.GetItemValue("firstname")

		

		

	Set doc1 = collection.GetNextDocument(doc1)

	

            Loop

	

	Set uidoc = ws.EditDocument(False, doc2)

End If 

End Sub

Subject: get all documents by key

You overwrite your fname value every time you run through the loop. So it makes sense you will only see the final value.

Create an array to accept all of the values and use the Redim Preserve to collect all the values. Once you are out of the loop, then set doc2.fname = to the array. That should accomplish what you are trying to do.

hth.

brandt

Subject: RE: get all documents by key

Thanks Brandt…I’ll look this up. I’ve never used the Redim Preserve.

Subject: RE: get all documents by key

Try something like this:

Dim fName() as string <–dim a string array variable

If collection.count > 0 Then

Set doc2 = db.CreateDocument

doc2.form = “Form2”

Redim fName(0) <–set array variable to have one postion before entering the loop

Set doc1 = collection.GetFirstDocument

Do Until doc1 Is Nothing

If fName(0) = “” then <–check to see if array has any values, if not, then fill the first value since we know no values have been put in the array yet

fName(0) = doc1.GetItemValue(“firstname”)

Else <–otherwise, we know that we need to add a new position

Redim Preserve fName(Ubound(fName) + 1)<–we call Redim Preserve here and add another position to the array, one more than whatever the current upper bound is. The Preserve part of the command will allow us to retain the values we have already placed in the array.

fName(Ubound(fName)) = doc1.GetItemValue(“firstName”)<–we always put the most recently accessed value in the newly created position in the array.

Set doc1 = collection.GetNextDocument(doc1)

Loop

doc2.fName = fName

Set uidoc = ws.EditDocument(False, doc2)

End If

End Sub

hth.

brandt

Subject: RE: get all documents by key

Thanks for the detailed explanation! I’ll try this…

Subject: RE: get all documents by key

one last caveat…make sure that fName is a multi-value field…or else you will only see the first value!

Subject: RE: get all documents by key

Yeap, it is thanks!

Subject: RE: get all documents by key

Brandt,

I’m getting an error: Illegal Parenthesized Reference: Unbound - line 57. I have several fields that I would need to use the redim on so that may be causing the problem.

I put an “End If” under the Else…undboud


Sub Click(Source As Button)

Dim session As New NotesSession

Dim db As NotesDatabase

Set db = session.CurrentDatabase



Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc1 As NotesDocument, doc2 As NotesDocument



Dim a() As String,b() As String,c() As String,d() As String, e() As String, f() As String

Dim picklist As Variant

Dim key As String

Dim location As String



server = db.server  'picklist gets info from here...

dbname = db.FileName

viewname = "ShowLocation" 



picklist = ws.Pickliststrings(3,1,server,dbname,viewname, "OSHA Form 300","Select a Location.",3,"")

If Isempty(picklist) Then Exit Sub 'they have pressed cancel



location = picklist(0)



temp1 = Strleft(location, "^")

temp2 = Strright(location, "^")



key = temp1



Msgbox "key = " & Cstr(key)



Dim view As NotesView

Set view=db.Getview("301 Incident Report by Year") 'give them the view to choose a document from

Dim collection As NotesDocumentCollection

Set collection = view.GetAllDocumentsByKey(key,True) 

Msgbox "collection.count is " & collection.count





If collection.count > 0 Then 

	

	Set doc2 = db.CreateDocument

	doc2.form = "OSHA300Form"

	

	Redim a(0)

	'Redim b(0)

	'Redim c(0)

	'Redim d(0)

	'Redim e(0)

	'Redim f(0)

	

	Set doc1 = collection.GetFirstDocument

	Do Until doc1 Is Nothing

		

		If a(0) = "" Then

			a(0) = doc1.GetItemValue("CaseLog")

		Else

			Redim Preserve a(Unbound(a) + 1)

			a(Unbound(a)) = doc1.GetItemValue("CaseLog")

			

			

		     'doc2.a = doc1.GetItemValue("CaseLog")

			doc2.b = doc1.GetItemValue("dispEmpFullName")

			doc2.c = doc1.GetItemValue("EmpJobTitle")

			doc2.d = doc1.GetItemValue("disp2CaseDate")

			doc2.e = doc1.GetItemValue("CaseWhereOccurred")

			doc2.f = doc1.GetItemValue("CaseInjuryorIllnes")

			

		End If

		

		

		Set doc1 = collection.GetNextDocument(doc1)

	Loop

	

	doc2.a = a

	

	Set uidoc = ws.EditDocument(False, doc2)

End If 

End Sub

Subject: RE: get all documents by key

Try this way:

I am hoping that you want to get the information from doc1 and create doc2 and open it. If so make sure all the fields in doc2 are multivalue fields.

Dim tmp1 As Variant

Dim tmp2 As Variant

Dim tmp3 As Variant

Dim tmp4 As Variant

Dim tmp5 As Variant

Dim tmp6 As Variant

Dim ctr As Integer



ctr = 0

If collection.count > 0 Then		

	Set doc2 = db.CreateDocument

	doc2.form = "OSHA300Form"

	

	Set doc1 = collection.GetFirstDocument

	Do Until doc1 Is Nothing

		If ctr = 0 Then			

			Redim tmp1(ctr)

			Redim tmp2(ctr)

			Redim tmp3(ctr)

			Redim tmp4(ctr)

			Redim tmp5(ctr)

			Redim tmp6(ctr)

		Else				

			Redim Preserve tmp1(ctr)

			Redim Preserve tmp2(ctr)

			Redim Preserve tmp3(ctr)

			Redim Preserve tmp4(ctr)

			Redim Preserve tmp5(ctr)

			Redim Preserve tmp6(ctr)

		End If

		tmp1(ctr) = doc1.CaseLog(0) 

		tmp2(ctr) = doc1.dispEmpFullName(0) 

		tmp3(ctr) = doc1.EmpJobTitle(0) 

		tmp4(ctr) = doc1.disp2CaseDate(0) 

		tmp5(ctr) = doc1.CaseWhereOccurred(0) 

		tmp6(ctr) = doc1.CaseInjuryorIllnes(0)

		

		Set doc1 = collection.GetNextDocument(doc1)

		ctr = ctr+1

	Loop

	

	doc2.CaseLog = tmp1

	doc2.dispEmpFullName = tmp2

	doc2.EmpJobTitle = tmp3

	doc2.disp2CaseDate = tmp4

	doc2.CaseWhereOccurred = tmp5

	doc2.CaseInjuryorIllnes = tmp6

	

	If Isarray(tmp1) Then Erase tmp1

	If Isarray(tmp2) Then Erase tmp2

	If Isarray(tmp3) Then Erase tmp3

	If Isarray(tmp4) Then Erase tmp4

	If Isarray(tmp5) Then Erase tmp5

	If Isarray(tmp6) Then Erase tmp6

	

	Set uidoc = ws.EditDocument(False, doc2)

End If

Subject: RE: get all documents by key

Thanks Narasimhan! It’s working!!!

Subject: RE: get all documents by key

This works but some fields do not populate on doc2.

I’m running into problems where the fields on doc1 are numbers or a date. These are computed fields on doc1.

The date field just does not show on doc2…no errors…not sure about that.

The number fields throw an error…debugger runs, it says cannot create field %1.

As for the debugger, when looking at the values for the nubmer fields…the debugger show 0’s for 3 docs that should have 0 as the value, but one doc has a 1 and the debuggers shows null → “”. I just have a formula on the field in doc1…@if(field= “dayoff”;1:0) ← the bebugger shows “” when this should be a value of 1.

Any suggestions? All the fields on doc2 work with text. Something is not converting right for the number fields.

I hope this makes sense…thanks for the help!

Subject: RE: get all documents by key

I’m not sure what I did, but the nubmers are working now. The last thing I did was make all the fields text for doc2. Now that I pasted the original code back in, all the numbers are appearing on doc2.