Object Variable Not Set in a Function

Hi,

i need your help :slight_smile:

I’ve a public function in a Script library correctly declared in a “Options” module of form.

I call this function from a “QueryOpen” avent, passing the NotesUiDocument argoument but, when i create a “NotesItem” object i’ve an error “Object Variable Not Set”.

Following, before the “QueryOpen” event code and after the function Code with the breackpoint error:

Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)

Dim v_CheckAccess		 As Variant



v_CheckAccess 			= fn_checkAccess(Source)



If v_CheckAccess = True Then

	continue = True

Else

	continue = False

End If

End Sub

Public Function fn_CheckAccess(obj_Source As NotesUIDocument)

'On Error Resume Next	

'____________________________________________________________________________________________________________

Dim obj_S	 								As NotesSession

Dim obj_db 								As NotesDatabase

Dim obj_ws	 							As NotesUIWorkspace

Dim obj_doc 								As NotesDocument

Dim obj_utenti 							As NotesItem

Dim obj_figura								As FigureProfessionali

Dim v_respCed 							As String

Dim v_analyst 								As String

'____________________________________________________________________________________________________________

Set obj_db 								= obj_S.CurrentDatabase

Set obj_doc 								= obj_Source.Document

Set obj_utenti 							= obj_doc.GetFirstItem("contact_lstSecurity")--------- BREAKPOINT ERROR

Set obj_figura 							= New FigureProfessionali

'____________________________________________________________________________________________________________

If(obj_source.IsNewDoc) Then

	fn_CheckAccess = True

Else

	Call obj_figura.pax(50)

	v_respCed 								= obj_figura.Personale

	Call obj_figura.pax(100)

	v_analyst 									= obj_figura.Personale

	

	If obj_S.CommonUserName = v_RespCed  Or obj_S.CommonUserName = v_analyst Then

		fn_CheckAccess = True

	Else

		

		Forall v In obj_source.document.contact_lstSecurity

			If v = obj_S.CommonUserName  Or v = "" Then

				fn_CheckAccess = True

			End If

		End Forall

		If Not existvalue Then

			Call Messaggi (102, "System", obj_utenti.text)

			fn_CheckAccess = False

		End If

	End If

End If

End Function

Someone can help me to understand this error ?? :-))

Thanks

Subject: Object Variable Not Set in a Function

Per documentation always use conditional:

if notesDocument.HasItem(“xxxxxxxx”) then

prior to any statement that uses notesDocument.GetItem(“xxxxxxx”)

Subject: RE: Object Variable Not Set in a Function

It doesn’t say that in the documentation, and I disagree with it, and that’s not the problem in this case anyway.

Subject: RE: Object Variable Not Set in a Function

Hey Andre,

I always use the “hasitem” test before I attempt to call the GetFirstItem method on a NotesDocument object. Is it a waste of time that offers no benefit? I just thought that it was good defensive programming practice to do so. I was under the impression that calling GetFirstItem on an item that does not exist will produce a runtime error. Am I mistaken?

Subject: RE: Object Variable Not Set in a Function

Yes, you are mistaken; GetFirstItem returns Nothing in that situation. If you call GetFirstItem and test if result Is Nothing, you only have to search for the item once; if you first call HasItem, you have to search for the item twice. Stands to reason the former approach is more efficient.

Even for functions which do return errors in case of failure, you don’t have to do all the work to make sure they’re not going to return an error, before you call them – that’s often impossible. Just establish an On Error action for the specific error code you expect, and do your exception handling there.

Also, I’m not going to tell anyone that they must do a specific exception test. If the document they’re looking at is from a result set of a search for TotalAmount > 10000, it’s a waste of time to test that the TotalAmount item really exists.

Or, since you always automatically add a general On Error trap to all your functions to log the line number and error text of any unexpected error conditions, you can just program as if the item is always present, knowing that if you’re wrong, you’ll be able to figure out the problem from the error log. This makes sense if you didn’t have a more useful recovery action you could take, such as using a default value instead…

Subject: RE: Object Variable Not Set in a Function

Thanks Andre, as usual a complete and clear explanation from a source that I trust. This community is a much better place with your participation and insight. I will change my evil ways!

Subject: Object Variable Not Set in a Function

When you are composing a document, in the Queryopen event there is as yet no back-end NotesDocument, so Source.Document returns Nothing. You will have to check whether IsNewDoc and skip the access check in that case.

Incidentally, LotusScript event code is not a security feature, since it is easy to circumvent. If you want to allow only certain users to view a document, you must use Readers fields.

Subject: Object Variable Not Set in a Function

GetFirstItem is the method of notesdocument class and you are using it in Notesuidocument.you need to convert this notesuidocument to notesdocument and then use this method

Subject: RE: Object Variable Not Set in a Function

but i done it so:

obj_Source is the NotesUIDocument.

I declared a Notesdocument (obj_doc) and then set my obj_doc as a objSource.Document.

In an error ??

Subject: RE: Object Variable Not Set in a Function

I solved it.

The reason was becouse i opened a new document therefore the Noteuidocument was empty yet.

Thanks !!