GetLastDocumentByKey

GetLastDocumentByKey

You Can View User Feedback To This Tip

How often we want to get the handle of the last document from the view based on some key value(s).. equivalent of NotesViewHandle.getdocumentByKey(). Here is the LS function getLastDocumentByKey(),which solves that purpose.

Function getlastDocumentByKey ( CatValue As Variant, ViewName As String, db As Notesdatabase ) As Notesdocument

 
 '''' This function finds the last document that matches the specified key
 
 Dim CatView As NotesView
 Dim CatEntryColl As NotesViewEntryCollection
 Dim LastEntry As NotesViewEntry
 Dim LastDoc As NotesDocument
 
 Set CatView = db.GetView ( ViewName )
 
 If CatView Is Nothing Then
  
  Print "Error in geting last document. View not found."
  Set getlastDocumentByKey = Nothing
  Exit Function
  
 End If
 
 Call CatView.Refresh 
 Set CatEntryColl = CatView.GetAllEntriesByKey ( CatValue, True )
 
 If CatEntryColl.Count = 0 Then
  
  Print "No documents for the specified key."
  Set getlastDocumentByKey = Nothing
  Exit Function
  
 End If
 
 Set LastEntry = CatEntryColl.GetFirstEntry
 
 While Not LastEntry Is Nothing
  
  If LastEntry.IsDocument Then
   
   Set LastDoc = LastEntry.Document
   
  End If
  
  Set LastEntry = CatEntryColl.GetNextEntry ( LastEntry )
  
 Wend
 
 Set getlastDocumentByKey = Lastdoc
 
End Function

USER FEEDBACK TO THIS TIP

  • This will *not* get

    Requires Free Membership to View

    Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

  • you the last document in the view as the ordering of documents in collections is undefined except when doing full text searches. Instead you have to do the following:

    1. Find the first document with the key in the view.

    2. Traverse the view until you meet a document with another key. The previous document was the last with the given key.

    The code shows you how. It can also handle multi-value keys (arrays).

    Code:

    Function GetLastDocumentByKey(view As Notesview, key As Variant) As NotesDocument
     ' Gets the last document for the given key in a view.
     Dim curdoc As NotesDocument
     Dim previousdoc As NotesDocument
     Dim keycounter As Integer
     
     Set curdoc = view.GetDocumentByKey(key, True)
     While Not (curdoc Is Nothing)
      Set previousdoc = curdoc
      Set curdoc = view.GetNextDocument(curdoc)
      If Isarray(key) Then
       For keycounter = 0 To Ubound(key)
        If curdoc.Columnvalues(keycounter) <> key(keycounter) Then 
         Set curdoc = Nothing
         Exit For
        End If
       Next keycounter
      Else
       If curdoc.Columnvalues(0) <> key Then Set curdoc = Nothing
      End If ' Isarray(key)
     Wend ' Not (curdoc Is Nothing)
     
     Set GetLastDocumentByKey = previousdoc
    End Function ' GetLastDocumentByKey
    
    —Morten Clausen
  • hi, I would like to clarify the feedback given by Morten Clausen. The original code posted by me will "definitely" fetch the last document for the specified key. The feedback point mentioned by "Morten Clausen" is true only for the 'NotesDocumentCollection' NOT for 'NotesViewEntryCollection'. The major advantage of NotesViewCollection over NotesDocumentCollection is that it gets all the entries in the view (whether it's "document" or "category" or "total" or "average" row) in the order it appears in the view. — - Sriram P

This was first published in May 2002

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.