IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

I've been poking around in a lot of applications recently, and I see that people like to use the statement:

Set uidoc = wksp.CurrentDocument

in form events, especially Postopen, as a way of retrieving a handle to the document associated with that event code.

This is a bad idea. CurrentDocument actually returns the document from the window that has focus. In a lot of cases, that's the same thing, but not necessarily. In composite applications, in framesets, or when using an embedded editor, the window with focus may contain a different document -- or no document. The NotesUIWorkspace object is context-neutral as regards where the code that calls it is running, so there's only one "current" document at any given time (or sometimes, none). When different documents open in different frames or components, they can't all be the "current" document at the same time.

Instead, as a best practice, use the Source As NotesUIDocument calling argument to the event handler function. So instead of:

Sub Postopen(Source As Notesuidocument)
        Dim wksp As NotesUIWorkspace
        Dim uidoc As NotesUIDocument
        Dim doc As NotesDocument
        Set uidoc = wksp.CurrentDocument
        Set doc = uidoc.Document
...
End Sub

you would write:

Sub Postopen(Source As Notesuidocument)
        Dim doc As NotesDocument
        Set doc = Source.Document
...
End Sub

which is not only more reliable, but also shorter.

Even if the CurrentDocument property works in your specific situation, you can't control how other people will use your form in composite applications, where the document isn't opened in its own window and so, might not have focus.

In certain cases, it makes sense to define a global variable "Dim Guidoc As NotesUIDocument," assign it during the Queryopen event (Set Guidoc = Source) and refer to that in places in the code where you'd like to know the current document but can't assume your window has focus. The Initialize events of fields, for instance, or the event subroutine for a NotesTimer. In fact, NotesTimer events often need to take advantage of the behavior of CurrentDocument to test whether "their" window has focus by comparing it to Guidoc.

BTW: It's okay to use CurrentDocument in action buttons and other contexts where the code can't be activated unless the document has focus.

Andre Guirard | 15 July 2009 08:00:00 AM ET | Home, Plymouth, MN, USA | Comments (1)

Search this blog 

Disclaimer 

    About IBM Privacy Contact