From time to time, you may find the need to search for a document stored in the current or alternate database. A document search is often used when you need data from one document to populate fields in another document. This section describes several methods to search for a document in a Notes database.
How It Works
A wide variety of solutions can be used to locate a document in a database. This section illustrates how to use While loops, the GeTDocumentByKey method, and the Search method to retrieve a particular document.
ImplementationExample 1
In this first example, the GeTDocumentByKey method is used to search a particular view for a text string. If the text string key is found, a handle to the document is assigned to the Doc object. After they are assigned, objects on the document can be referenced or updated. In order to use this approach, the search key must reside in the first column, which must be sorted in either ascending or descending order.
To implement this solution, create a view where the first column in the view contains the search key. Sort the first column in either ascending or descending order. To locate the document, add the following code in a button, agent, or event. Be sure to replace KEY and VIEW with valid values in the following code example.
Dim s As NotesSession Dim db As NotesDatabase Dim view As NotesView Dim doc As NotesDocument Dim key as String Set s = New NotesSession Set db = s.CurrentDatabase key = "John Doe" Set view = db.GetView("VIEW") Set doc = view.GetDocumentByKey(key, True)
Note
The GetdocumentByKey method includes a match parameter. Set the parameter to trUE to enforce a perfect match or FALSE to allow for a partial string match.
ImplementationExample 2
In this second example, the Search method and a static key are used to locate a document in the database. This approach can be a bit tricky because it requires delimiters, such as the vertical bars or double quotes, to separate the various parameters. This approach also requires a search string key, a DateTime value, a form name, and a field name. If multiple documents are returned, use the GetFirstDocument method to retrieve the first document or the Forall statement to loop through each document.
To implement this solution, add the following code in a button, agent, or event. Be sure to replace KEY, FORM, and FIELD with valid values in the following code example.
Dim w As New NotesUIWorkspace Dim s As New NotesSession Dim db As NotesDatabase Dim doc As NotesDocument Dim collection as NotesDocumentCollection Dim key As String Dim searchformula As String Dim searchdate As NotesDateTime Set db = s.CurrentDatabase Set searchdate = New NotesDateTime("") '----------------------------------------------------------------- ' Set the search key and search formula values '----------------------------------------------------------------- key = "VALUE" searchformula = "Form=""FORM"" & FIELD="""+Key+"""" Set collection = db.Search(searchformula, searchdate, 0) Set doc = collection.GetFirstDocument
ImplementationExample 3
In this third example, the Search method and a dynamic key are used to locate a document in the database. As with the previous example, you will need to pay particular attention to enclose parameters in the correct number of delimiters (or quotes). Here, the actual search is a dynamic value and references a text string field on the current form.
Dim w As NotesUIWorkspace Dim s As NotesSession Dim db As NotesDatabase Dim uidoc As NotesUIDocument Dim collection As NotesDocumentCollection Dim docA As NotesDocument Dim docB As NotesDocument Dim searchformula As String Dim searchdate As NotesDateTime Set w = New NotesUIWorkspace Set s = New NotesSession Set db = s.CurrentDatabase Set searchdate = New NotesDateTime("") Set uidoc = w.CurrentDocument Set docA = uidoc.Document searchformula = "Form=""FORM"" & FIELD="""+DocA.FIELD(0)+"""" Set collection = db.Search(searchformula, searchdate, 0) Set docB = collection.GetFirstDocument
ImplementationExample 4
Finally, this last example illustrates how to loop through all documents in a particular view. If a field on each document matches a target value, then an action is taken. For example, using this approach, you could change all documents where the OWNER field equals a person's name such as John Doe.
Dim w As NotesUIWorkspace Dim s As NotesSession Dim db As NotesDatabase Dim doc As NotesDocument Dim view As NotesView Set w = new NotesUIWorkspace Set s = new NotesSession Set db = s.CurrentDatabase Set view = db.GetView("VIEW") Set doc = view.GetFirstDocument While Not(doc Is Nothing) If doc.FIELD(0) = "VALUE" Then msgbox "Found a match." ' do something here. End If Set doc = view.GetNextDocument(doc) Wend
An Introduction to the Lotus Domino Tool Suite
Getting Started with Designer
Navigating the Domino Designer Workspace
Domino Design Elements
An Introduction to Formula Language
An Introduction to LotusScript
Fundamentals of a Notes Application
Calendar Applications
Collaborative Applications
Reference Library Applications
Workflow Applications
Web Applications
Design Enhancements Using LotusScript
Design Enhancements Using Formula Language
View Enhancements
Sample Agents
Miscellaneous Enhancements and Tips for Domino Databases
Data Management
Security
Application Deployment and Maintenance
Troubleshooting
Appendix A. Online Project Files and Sample Applications
Appendix B. IBM® Lotus® Notes® and Domino®Whats Next?