Object Variable not set

I am receiving a message in debugger stating the Object Variable is not set. What have I done wrong? Currently this is being called by a hotspot button on a form. Thanks!

Line error:

targetDoc.Form = “Acknowledgment”

Sub Initialize

Dim s As New NotesSession

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIdocument

Set uidoc=ws.CurrentDocument

Dim targetDb As NotesDatabase

Dim targetDoc As NotesDocument

Dim sourceDoc As NotesDocument

Set sourcedoc=uidoc.document



Dim dateTime As NotesDateTime







Set targetDb = s.CurrentDatabase

Dim vPeople As Variant

'populate the fields



targetDoc.Form = "Acknowledgment"

targetDoc.empName = sourceDoc.empCapture(0) 

vPeople=sourceDoc.GetItemValue("empCapture")

Forall p In vPeople

	Set targetDoc = targetDb.CreateDocument

	Set dateTime = New NotesDateTime(Format$(Now,"Long Date"))

	Call targetDoc.ReplaceItemValue("Date", dateTime)

	

	Call targetDoc.Save( False, False ) 

End Forall

End Sub

Subject: Object Variable not set

Angie,

I cannot empahsize enough that you need to read your code before posting here. That is the nbest way to see what has happened.

In anutshell, you have tried to set a field on the targetDoc before you have created the targetDoc.

Walking through the debugger and looking at the variables would point this out to you.

Subject: RE: Object Variable not set

Sorry, File Save I didn’t recognize it before you’d pointed it out. I do understand but am now experiencing a different problem.

I’ve changed my code and it runs error free but it doesn’t seem to create the new documents. Watching it through debugger I see that although sourceDoc.lpkNon has a value it doesn’t transfer to the empName field in the targetDoc.

Any ideas why its not displaying? Thanks!

Sub Initialize

Dim s As New NotesSession

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIdocument

Set uidoc=ws.CurrentDocument

Dim targetDb As NotesDatabase

Dim targetDoc As NotesDocument

Dim sourceDoc As NotesDocument

Set sourcedoc=uidoc.document



Dim dateTime As NotesDateTime



Set targetDb = s.CurrentDatabase

Set targetDoc = targetDb.CreateDocument



Dim vPeople As Variant

'populate the fields

vPeople=sourceDoc.GetItemValue("lpkNon")

Forall p In vPeople

	targetDoc.Form = "Acknowledgment"

	targetDoc.empName = sourceDoc.lpkNon(0) 

	

	Set dateTime = New NotesDateTime(Format$(Now,"Long Date"))

	Call targetDoc.ReplaceItemValue("Date", dateTime)

	

	Call targetDoc.Save( False, False ) 

End Forall

End Sub

Subject: RE: Object Variable not set

targetDoc.empName = p

Subject: RE: Object Variable not set

Hi File Save,I’ve tried your code and its not working either. Do I have it in the wrong place again?

Thanks again for your help!

Sub Initialize

Dim s As New NotesSession

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIdocument

Set uidoc=ws.CurrentDocument

Dim targetDb As NotesDatabase

Dim targetDoc As NotesDocument

Dim sourceDoc As NotesDocument

Set sourcedoc=uidoc.document



Dim dateTime As NotesDateTime



Set targetDb = s.CurrentDatabase

Set targetDoc = targetDb.CreateDocument



Dim vPeople As Variant

'populate the fields

vPeople=sourceDoc.GetItemValue("lpkNon")

Forall p In vPeople

	targetDoc.Form = "Acknowledgment"

	'targetDoc.empName = sourceDoc.lpkNon(0) 

	targetDoc.empName = p

	Set dateTime = New NotesDateTime(Format$(Now,"Long Date"))

	Call targetDoc.ReplaceItemValue("Date", dateTime)

	

	Call targetDoc.Save( False, False ) 

End Forall

End Sub

Subject: RE: Object Variable not set

Use your debuger: you should find the the last entry of vPeople is a null. Your are writing each person into the same document. If you want a new doc for each person, move the createdocument into the for loop.

PS The variable p is dim by the forall command

Subject: You going to the games this weekend?

Subject: File Save, I owe you big time!!

Thank you so much for your help with the code! Its so well documented and easy to read that it has definitely helped to increase my understanding of the code!!!

I am still having problems with it skipping the creation of the target document. I’m able to watch it in debugger, verify that there is a value in the lpkNon field but then it seems to skip. The final tracking that I can view in debugger takes me to the targetDatabase. I don’t get as far as the targetDocument.

Yes, this code is behind a button for the moment.

Subject: RE: File Save, I owe you big time!!

Angie,

I do not get the error you did, so like I always ask, please post the code exactly as you have used it.

Subject: RE: File Save, I owe you big time!!

I have a hotspot button which I’ve saved your code as follows. Thanks!

Sub Initialize

'GET A HANDLE ON THE NOTES SESSION

Dim s As New NotesSession

'GET A HANDLE ON THE CURRENT NOTESUIWORKSPACE

Dim ws As New NotesUIWorkspace

'GET A HANDLE ON THE UIDOC AND UNDERLYING

'DOCUMENT

Dim uidoc As NotesUIdocument

Set uidoc=ws.CurrentDocument

Dim sourceDoc As NotesDocument

Set sourcedoc=uidoc.document

'GET A HANDLE ON THE CURRENT DATABASE

Dim targetDb As NotesDatabase

Set targetDb = s.CurrentDatabase

'IF THE ITEM/FIELD EXISTS IN THE DOCUMENT

If sourceDoc.HasItem("lpkNon") Then

'GET A HANDLE ON THE FIELD TO BE PROCESSED AS A

'VARIANT

	Dim vPeople As Variant

	vPeople=sourceDoc.GetItemValue("lpkNon")

'DECLARE THE DOCUMENT THAT WILL BE CREATED

	Dim targetDoc As NotesDocument

'DECLARE THE NOTESDATETIME OBJECT TO BE SET

	Dim dateTime As NotesDateTime

'LOOP THROUGH THE VALUES (p) OF THE FIELD AND

'CREATE A DOCUMENT FOR EACH VALUE

	Forall p In vPeople

'CREATE THE TARGET DOCUMENT

		Set targetDoc = targetDb.CreateDocument

'IF THE TARGET DOC EXISTS, SET THE FIELD

'VALUES

		If Not (targetDoc Is Nothing) Then

			targetDoc.Form = "Acknowledgment"

			targetDoc.empName = p

			Set dateTime = New NotesDateTime(Format$(Now,"Long Date"))

			Call targetDoc.ReplaceItemValue("Date",dateTime)

'SAVE THE TARGET DOCUMENT

			Call targetDoc.Save( False, False ) 

		End If

		

	End Forall

End If

End Sub

Subject: RE: File Save, I owe you big time!!

There is no reason this code should not be working that I can see.

If possible, go ahead and send me a zipped copy of the database to lotushelp2003NOSPAM@yahoo.com, taking out the NOSPAM of course, and I will take a look.

Subject: RE: Object Variable not set

I should have done more rearranging of the code.

One technique that might be helpful to you is to write ‘psuedo-code’, or a plain English representation of what you are trying to do. After you have written this, you then go back and fill in the code.

What is below is what it might look like when all is said and done (and for testing, put the code in the hotspot button if not there yet. If successful, then you should move the code to a script library and call the subroutine):

Sub Click(Source As Button)

'GET A HANDLE ON THE NOTES SESSION

Dim s As New NotesSession

'GET A HANDLE ON THE CURRENT NOTESUIWORKSPACE

Dim ws As New NotesUIWorkspace

'GET A HANDLE ON THE UIDOC AND UNDERLYING

'DOCUMENT

Dim uidoc As NotesUIdocument

Set uidoc=ws.CurrentDocument

Dim sourceDoc As NotesDocument

Set sourcedoc=uidoc.document

'GET A HANDLE ON THE CURRENT DATABASE

Dim targetDb As NotesDatabase

Set targetDb = s.CurrentDatabase

'IF THE ITEM/FIELD EXISTS IN THE DOCUMENT

If sourceDoc.HasItem(“lpkNon”) then

'GET A HANDLE ON THE FIELD TO BE PROCESSED AS A

'VARIANT

Dim vPeople As Variant

vPeople=sourceDoc.GetItemValue(“lpkNon”)

'DECLARE THE DOCUMENT THAT WILL BE CREATED

Dim targetDoc As NotesDocument

'DECLARE THE NOTESDATETIME OBJECT TO BE SET

Dim dateTime As NotesDateTime

'LOOP THROUGH THE VALUES (p) OF THE FIELD AND

'CREATE A DOCUMENT FOR EACH VALUE

Forall p In vPeople

'CREATE THE TARGET DOCUMENT

Set targetDoc = targetDb.CreateDocument

'IF THE TARGET DOC EXISTS, SET THE FIELD

'VALUES

If not (targetDoc is nothing) then

  targetDoc.Form = "Acknowledgment"

  targetDoc.empName = p

  Set dateTime = New NotesDateTime

    (Format$(Now,"Long Date"))

  Call targetDoc.ReplaceItemValue("Date",

    dateTime)

  

  'SAVE THE TARGET DOCUMENT

  Call targetDoc.Save( False, False ) 

end if

End Forall

End Sub

This method forces you to think through the code before you actually write it, reducing the likelihood of error.

You will also note that, for the most part, I pair up all my “Dim” and “Set” statements. (The exception being when something is being set inside a loop) I do not see that done much in code, but it is a method taught to be by one of the best LorusScript instructors around and really helps when trying to find errors in code.

Now if you are dealing with Names type fields here, you may have to do some more manipulation.