Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

Random 'Object Variable Not Set' on Notes

I've created a database to track the projects in my workplace, but from time to time different users got 'Object Variable Not Set' error. The problem is it doesn't always happen. For example one user opened a project in process and got the error, when I debugged it, I see that one field somehow did not show up on the document hence causing the error when the code tried to get the value of the field. BUT when I tried to open the same project, it opened just fine.

Again, this doesn't happen everytime and the same user that got an error opening one project can open other projects just fine. But I know when the error message show up it's always caused by one field 'Status Comment'. What I don't understand is why it works sometimes and not the other times.

Attached is the snippet of the code
Set workspace = New NotesUIWorkspace
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document

...

Set item = doc.GetFirstItem("StatusComments")

' This is the point where the program stops. For some reason 'StatusComments field does not show up on the Item list in the doc
statuscomments1 = item.Text

Open in new window

0
coronoahcoro
Asked:
coronoahcoro
  • 6
  • 6
  • 3
1 Solution
 
Bill-HansonCommented:
That just means that for some documents, there is no StatusComments item available.

Try using getItemValue which does not raise an exception for missing items.  Instead, if the item does not exist, an empty string is returned.
statuscomments1 = doc.getItemValue("StatusComments")(0)

Open in new window

0
 
Bill-HansonCommented:
... Unless StatusComments is rich text.

For rich text, my idea won't work.  In this case, you need to check the 'item' variable before trying to access it.
0
 
Bill-HansonCommented:
Also, what event is this code behind?  You can't access new RichText in query save.  RichText is only available after the document is saved.

Sorry for the multi-post, but it's kind of hard to guess what context you are running under.  Posting the full code would help.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
coronoahcoroAuthor Commented:
1. It is strange because when I open the document from my PC it opens just fine, but it gives the user the error. But if I go to edit mode and save the doc, then the user will not get the error anymore. I noticed that when the user get the error some other fields are missing and there is one item called 'NewForm' shows up on the debugger, which is strange because I never created that item.

2. It's just a text field with no default value.

3. The code is behind OnLoad form event. Basically I need to get the value of some fields when a user opens a document, then save the values in different variables so later after the user finishes editing the doc and save the doc, the code can compare the original value and the value on those fields after the save button is clicked. If the values are different then emails are sent depending on which fields are changed.

I attached the code on my OnLoad event.
Set workspace = New NotesUIWorkspace
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
	
If Not(uidoc.IsNewDoc) Then
	Set item = doc.GetFirstItem("Priority")
	priority1 = item.Text
	Set item = doc.GetFirstItem("Assignee")
	assignee1 = item.Text
	Set item = doc.GetFirstItem("Status")
	status1 = item.Text
	Set item = doc.GetFirstItem("IssueDescription")
	description1 = item.Text
	Set item = doc.GetFirstItem("StatusComments")
	statuscomments1 = item.Text
	Set item = doc.GetFirstItem("Updates")
	statusupdater1 = item.Text
End If

Open in new window

0
 
Bill-HansonCommented:
The only thing I see wrong is the way you are accessing the current document.

Never use NotesUIWorkspace.CurrentDocument in form events!  The event methods provide you with an in-memory copy of the current ui document which is more reliable.

Here's the preferred method for reading values in OnLoad...
Sub Onload(Source As Notesuidocument)
	If Not(source.IsNewDoc) Then
		Set doc = source.Document
		priority1 = doc.GetItemValue("Priority")(0)
		assignee1 = doc.GetItemValue("Assignee")(0)
		status1 = doc.GetItemValue("Status")(0)
		description1 = doc.GetItemValue("IssueDescription")(0)
		statuscomments1 = doc.GetItemValue("StatusComments")(0)
		statusupdater1 = doc.GetItemValue("Updates")(0)
	End If
End Sub

Open in new window

0
 
doninjaCommented:
if using back end document from uidoc whtn I always do a check for field existence before trying to access any value. you can then atleast give the user mare valid return message rather than an error

Or more importantly you can set the item to a default value, bypass or try reloading the back end document incase it was due to a timeout processing the doc etc.

You can also try testing the type to ensure it is a text item





if item is nothing then
  messagebox("Error with StatusComment field", 0 , "Error")
elseif item.type <> 1280 then
  'item not text
end if

Open in new window

0
 
doninjaCommented:
As other things to look at, does the form use an subforms or any areas that have lookups to other databases. Maybe the access the user has to perform some of the lookups does not fill in all the fields. bt when you run it your permissions save all of the relevant lookup data into the document for the next person to view ?
0
 
coronoahcoroAuthor Commented:
Thanks Bill-Hanson for the suggestion. I am just following what the Help file says :) I'll try your code tomorrow.

doninja, checking the field existence is a good idea but if the user gets an error, what's next? sending more meaningful error message won't fix the problem. Like I mentioned before, it occurs randomly, there is no pattern whatsoever. I want to know is it a mistake on my code or Notes just behaving strangely. if it is a mistake on my part then there has to be a way to fix it, but it is one of Notes quirkiness then I'm so ready just to leave it alone.

No the form doesn't use a subform, and there is only one field that uses lookup and it isn't the comment field.
0
 
doninjaCommented:
can't give you exact reason why this happens as a lot of variables.
Maybe instead of error message you could first try reloading the backend doc.
clear the Doc variable and reoad from uidoc.document

It is a work around rather than solution but if it works then it could point to some client issue with instal of notes, connectivity etc
0
 
coronoahcoroAuthor Commented:
Bill, I'm reading your last comment again. 'Never use NotesUIWorkspace.CurrentDocument in form events! '. I'm curious, in what situation is NotesUIWorkspace.CurrentDocument used? Like I said most of the examples from Notes Help use NotesUIWorkspace.CurrentDocument to reference to the document that is currently opened.
0
 
Bill-HansonCommented:
You would use it everywhere except in a form event that already provides the necessary handle: behind an action button, a hotspot link, in an agent, in a script library, in a  field's onChange event, etc...

Notes documentation has never been known for completeness, but I've been doing this for a very long time.  Have you even tried my code yet?
0
 
coronoahcoroAuthor Commented:
Yes I've changed my code on the onChange field according to your code and it behaves normally. I'm in the process of rewriting my action button using your method.
0
 
coronoahcoroAuthor Commented:
Now if I have an action button, can I use the code below to access current doc?




Sub Click(Source As Button)
  Set workspace = New NotesUIWorkspace
  Set uidoc = workspace.CurrentDocument	
  Set doc = uidoc.Document

...

  Set item = doc.GetFirstItem("ReportedBy_Email")
...
End Sub

Open in new window

0
 
Bill-HansonCommented:
Yes.

The difference is in the form timing.  Behind a button, workspace.CurrentDocument correctly represents the current document, but in the form events (onLoad, queryOpen, postOpen, queryModeChange, etc...), it may or may not depending on the context and timing.  That's why they give you a direct handle in all form events.
0
 
coronoahcoroAuthor Commented:
It seems that no one is having the problem after I changed the code to what Bill-Hanson suggested. Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now