Object Variable Not Set

Could someone tell me what is wrong with my code?

I’m getting “Object Variable Not Set” for line temp4=doc2.GetItemValue(“doc2.Fatalities_1(0)”)

Sub Initialize

Dim workspace As NotesUIWorkspace	

Set workspace=New NotesUIWorkspace

Dim uidoc As notesuidocument

Set uidoc=workspace.currentdocument

Dim doc As NotesDocument

Set doc=uidoc.Document



Dim temp1 As String, temp2 As String, temp3 As Integer, temp4 As Integer, janfatalitytotal As Integer



'here are the keys to search view below

temp1=doc.CurrentYear(0)

temp2=doc.PlantLocation(0)

temp3=doc.death(0)





Dim Session As NotesSession

Set Session = New NotesSession

Dim db As NotesDatabase

Set db=Session.CurrentDatabase



Dim view As NotesView	

Dim keys(1 To 2) As String



Set view = db.GetView("All 2004 - By Plant")

keys(1)=temp1

keys(2)=temp2

Dim doc2 As NotesDocument

Set doc2 =view.GetDocumentByKey(keys)



'temp4=doc2.Fatalities_1(0)

temp4=doc2.GetItemValue("doc2.Fatalities_1(0)")



janfatalitytotal=temp4+temp3

Call doc2.ReplaceItemValue("Fatalities_1(0)",janfatalitytotal)

Call doc2.Save(False,True)

End Sub

-------- Here is what I’m trying to do -----------------

When saving “doc”, post save calls up the above.

Based on 2 fields on doc 1, I’m looking to a certain view. The view will contain only 1 document. I want to take the value of doc.field and doc2.field and add them together, then replace the value of the field on doc2.

From what I can tell in the debugger, everything is working until I get to the temp4 line.

Thanks for any suggestions…

Subject: Object Variable Not Set

Your doc2 is not set (Empty when you debug). That’s why you are getting that error. Make sure your keys are right. It’s the problem with your keys. Hence not returning any document.

Subject: RE: Object Variable Not Set

I’m not sure I understand. I set doc2Dim doc2 As NotesDocument

Set doc2=view.GetDocumentByKey(keys)

In the debugger, the keys show up at at temp1 and temp2 then show up as “keys” for 1 and 2.

Can you explain what I’m doing wrong?

Subject: RE: Object Variable Not Set

When you run this through in the debugger is doc2 set to anything? If it is then the problem is detailed in the posts below.

If not then it may be that that you need to set the columns in the view to be sorted and categorised.

Subject: Object Variable Not Set

Actually, you don’t need to reference doc2 twice in your line:temp4=doc2.GetItemValue(“doc2.Fatalities_1(0)”)

It should be:

temp4=doc2.GetItemValue(“Fatalities_1”)

Subject: RE: Object Variable Not Set

Lloyd,

Tom pointed out the problem. I overlooked this. As Tom said, do not use doc2.Facilities_1(0) inside the brackets. Inside brackets when you use GetItemValue should be only “Facilities_1” with double quotes, instead of doc2.Facilities_1(0)

Subject: RE: Object Variable Not Set

It keeps stopping on the temp4 line saying object variable not set.

I took off the (0) as suggested, also took it off in the replaceitemvalue line. The view I’m looking to has first column Ascending and Categorized, the second column is Ascending only.

Subject: RE: Object Variable Not Set

Both columns need to be sorted and categorised.

From the notes help

If you specify a single key, the key can correspond to a value in a sorted column that is either categorized or not categorized. If you specify multiple keys, each key must correspond to a value in a sorted column that is also categorized.

Subject: RE: Object Variable Not Set

Ok, I fixed the view…oops, I misread that.

I’m still getting the same message on that temp4 line…Object variable not set.

???

Subject: RE: Object Variable Not Set

See my or Tom’s posting above. You cannot get the error if you put “Finalists” instead of doc.Finalists2(0). If you still get it, make sure those two columns are sorted and identify the problem in your both ‘keys’.

Subject: RE: Object Variable Not Set

Key1 and Key2 show up correctly in the debugger. I took off that (o). Doc 2 doesn’t have a + sign in debugger. It just shows Doc 2 and nothing drops down under it.

Here is the updated code:

Sub Initialize

Dim workspace As NotesUIWorkspace	

Set workspace=New NotesUIWorkspace

Dim uidoc As notesuidocument

Set uidoc=workspace.currentdocument

Dim doc As NotesDocument

Set doc=uidoc.Document



Dim temp1 As String, temp2 As String, temp3 As Integer, temp4 As Integer, janfatalitytotal As Integer



'here are the keys to search view below

temp1=doc.CurrentYear(0)

temp2=doc.PlantLocation(0)

temp3=doc.death(0)





Dim Session As NotesSession

Set Session = New NotesSession

Dim db As NotesDatabase

Set db=Session.CurrentDatabase



Dim view As NotesView	

Dim keys(1 To 2) As String



Set view = db.GetView("All 2004 - By Plant")

keys(1)=temp1

keys(2)=temp2



Dim doc2 As NotesDocument

Set doc2=view.GetDocumentByKey(keys)



'temp4=doc2.Fatalities_1(0)

temp4=doc2.GetItemValue("Fatalities_1")



janfatalitytotal=temp4+temp3

Call doc2.ReplaceItemValue("Fatalities_1",janfatalitytotal)

Call doc2.Save(False,True)

End Sub

Subject: RE: Object Variable Not Set

It must be a problem with your keys. Check that the columns in your view are both text

If so try to narrow it down by only searching for the first key. If this works then you know it is the second key that is the problem.

If not switch the order of the columns in the view so that the 2nd key is the first column and then try searching for the 2nd key only to see if that works.

You should then be able to narrow down the cause of the problem.

Subject: RE: Object Variable Not Set

Ok, I’m getting somewhere. I switched around the keys. I had the second categorized column first…oops.

Now that I made that change,

That same temp4 line is now throwing: type mismatch.

Subject: RE: Object Variable Not Set

GetItemValue always returns an array so you can not assign it to temp4 which is declared as an integer.

Change temp 4 to be a variant and then use the CInt function to convert the return value.

Alternatively the first line you have commented out should work and get rid of the GetItemValue line

'temp4=doc2.Fatalities_1(0)

temp4=doc2.GetItemValue(“Fatalities_1”)

Subject: RE: Object Variable Not Set

Simplify your code as below:

Dim Session As NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim workspace As NotesUIWorkspace

Dim uidoc As notesuidocument

Dim doc As NotesDocument

Dim doc2 As NotesDocument

Dim keys(1 To 2) As String

Set Session = New NotesSession

Set db=Session.CurrentDatabase

Set view = db.GetView(“All 2004 - By Plant”)

Set workspace=New NotesUIWorkspace

Set uidoc=workspace.currentdocument

Set doc=uidoc.Document

'here are the keys to search view below

keys(1)= trim$(cstr(doc.CurrentYear(0))) 'change it to string, if not string

keys(2)= trim$(cstr(doc.PlantLocation(0)))

Set doc2= view.GetDocumentByKey(keys)

doc2.Fatalities_1 = cint(doc.Fatalities_1(0))+ cint(doc.death(0)) 'change to integer and sum up. Hope doc2.Fatalities_1 is a number field.

Call doc2.Save(False,True)

End Sub

Subject: RE: Object Variable Not Set

I’m still getting object variable not set on this line:

doc2.Fatalities_1 = Cint(doc.Fatalities_1(0))+Cint(doc.death(0)) 'change to integer and sum up.

I want to sum up the fields: doc(death) + field on

doc2(Fatalities_1)

Thanks!

Subject: RE: Object Variable Not Set

Your doc2 seems not getting set. That’s why you still get error. It is not setting up because of your keys. Though you get values in keys, they might not be matching with the column values in the view. Hence your doc2 is not at all setting. Change the lookup keys as below(in code and also in view column):

dim key as string

key = cstr(doc.CurrentYear(0)) & “-” & cstr(doc.PlantLocation(0))

set doc2 = getdocumentbykey(key)

doc2.Fatalities_1 = Cint(doc2.Fatalities_1(0))+Cint(doc.death(0))

This should definitely set your the doc2. And you can replace Fatalities in doc2 by adding the existing value with the value of ‘death’ field from doc, without any problem.

Now in the view:

In First column —>Concatenate the fields of your first column and second column so that they will be like @Text(CurrentYear) + “-”+@Text(PlantLocation)

Delete your second column so that it will not exist.

Good luck this time.

Subject: RE: Object Variable Not Set

It’s updating doc2 now, thanks.

However, I have to edit the doc2 manually and save or select it and choose refresh selected docs in order for the view to update.

I do have “Refresh fields on Keyword change” on my form too.

The field on doc2 is a computed number field. That field adds up the field that was changed by the LS. On doc2 there is a fatalities field for each month.

I’ve tried a few things but can’t get goc2 to refresh to appear in the view correctly.

Subject: RE: Object Variable Not Set

This seemed to do the trick!

Call doc2.Save(False,True)

Set agent = db.GetAgent("myagentname")

If agent.Run = 0 Then

	Messagebox "Agent ran",, "Success"

Else

	Messagebox "Agent did not run",, "Failure"

End If

https://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/587b6024f4d5430a85256df30068f2b9?OpenDocument

Subject: RE: Object Variable Not Set

Congratulations. Finally you got it to work. Ha!