Postopen code leaves document open twice

This is a weird one for you.

I have a "report" form that collects and formats information from other documents in the database, dumping the results nicely into a rich-text field. The data collection is handled in the Postopen event, if the document is new.

Because the resulting field is rich-text, it does not show updates until the document is closed and reopened. So I have this code at the very end of all the rest of the stuff in the Postopen:

==== BEGIN PASTE ====
      Call Source.Document.Save(True, False)
      Source.Document.SaveOptions = "0"
      Call Source.close
      Call ws.ViewRefresh
      Set uidoc = ws.EditDocument(False, doc)
===== END PASTE =====

Note that doc, in this case, is a NotesDocument that is declared in the form's (Declarations) section (not the Globals (Declarations) section, and its value is set to Source.Document. Likewise, ws is the NotesUIWorkspace that is declared in the (Declarations) section and set as a New NotesUIWorkspace.

This is all fine -- half the time. I have two major classifications of reports that can be generated, based on the user's responses to some ws.Prompts in the PostOpen. For one classification of reports, I have no problems. For the other, when the process is complete the new report is on the screen, but there are two "task buttons" in the bar, indicating that the document is open twice. The first task button contains the calculated name of the re-opened report document, and the second contains the calculated name of a new report document. If you click on the two task buttons, you can go back and forth between the two documents: the one that shows the complete report, and the one that shows the document as it appeared before it was saved.

The two classifications of reports collect information from the same pool of documents, though they collect different information. The report format that exhibits this quirk uses an array of objects of a class declared in the form's (Declarations) section. The array is processed in a subroutine, where it declared dynamically. As the subroutine closes, it runs this code:

==== BEGIN PASTE ====
      For intCount = 1 To Ubound(detailLines)
            Set detailLines(intCount) = Nothing
      Redim detailLines(0 To 0)
      Set detailLines(0) = Nothing
      Set varDetailLines = Nothing
===== END PASTE =====

Obviously, detailLines() is the dynamic array of objects of my class. varDetailLines is another instance of the class.

The report format that doesn't exhibit this quirk does not use any variable type other than the standard Notes classes. I don't know that the class thing is causing my problems, but it is the chief significant difference between the processing of the two formats of report.

Does anyone have any idea how I can get that second "empty" document to not appear? It's all that much more baffling, given that the empty one appears after the "full" document in the Task Button bar. Even more so, when the report generation is complete, the "full" document is showing, though the "empty" task button appears to be selected.

Thanks for any help you can provide. I always like to come in with a good challenge for you!

-- b.r.t.
Barry TiceBusiness AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is it a single form that does blth classes of report, or different forms?
Barry TiceBusiness AnalystAuthor Commented:
Same form, qwaletee. Whichever report is generated is dumped into a calculated rich-text field (called "Body", with a formula of Body). That way, when the report is viewed after creation, it's just showing the collected data.
-- b.r.t.
Here is my suggestion

Create an agent which will collect all the data thru prompts and then Create empty report document with all necessary parameters collected into fields and save it and open the document. Then during QueryOpen using this script get backend handle of the document and then begin processing (you can also do this in agent and after dumping data open the document that is more easy)

Dim session as New NotesSession
Dim db as NotesDatabase
Dim doc as NotesDocument
set db = session.CurrentDatabase
set doc = db.GetDocumentByUNID( Source.Document.UniversalID )
' Here do reporting on the doc -- this will effectively open the report with complete data

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Oh, c'mon, let's try fixing Varry's problem before completely reworking the code.
Sjef BosmanGroupware ConsultantCommented:
I think Hemanth does have a point here. If doc is set to Source.document, then doc should be Nothing after calling the Close method on Source. It could be that the Close is skipped, for whatever reason, since the form is in the PostOpen and therefore intrinsically open.

Possible solution: get an independent doc and use that one to open the form on the screen. Like the statement
   Set doc = db.GetDocumentByUNID( Source.Document.UniversalID )
Barry TiceBusiness AnalystAuthor Commented:
OK, so I've changed the closing code to this:
==== BEGIN PASTE ====
      strID = doc.UniversalID
      Call Source.Document.Save(True, False)
      Source.Document.SaveOptions = "0"
      Call Source.close
      Call ws.ViewRefresh
      Set doc = db.GetDocumentByUNID(strID)
      Set uidoc = ws.EditDocument(False, doc)
===== END PASTE =====
with strID declared in the Postopen as a string.

No change. I'm still getting two open documents at the end.

In the absence of figuring out what's going on here, I expect I'll end up moving this to an agent. But I'd rather figure out what's going on.

-- b.r.t.
Sjef BosmanGroupware ConsultantCommented:
Weird. But reopening documents for the sake of computed rich text fields has always been an tricky thing. I think it must be a timing issue. You could try to move your code to the QueryClose event, like this:

in Declarations:
Dim process_flag As Integer

in PostOpen:
If "proccessing is required" Then
   process_flag= True
   Call Source.Close
End If

in QueryClose:
If process_flag Then
   ' do_your_thing
End If

If you are going to rework your code, since I wonder why you want to open and reopen the document, you could just use a view button running on unprocessed documents.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.