Tip

LotusScript equivalent of @DBLookup and @DBColumn for Lotus Notes and Domino Server

Related resources:
Ajax code equivalent of the @DBColumn formula for Lotus Notes

JavaScript version of @DBLookup and @DBColumn

Tutorial: 30 LotusScript tips

LotusScript Reference Center

You can use the @DBLookup and @DBColumn functions in LotusScript with the help of the Evaluate statement, but this tends to decrease Lotus Domino Server's performance. The code below will achieve functionality similar to the @DBLookup and @DBColumn statements in LotusScript without creating the same performance drag.

Note: The return value for @DBColumn can be used in dialogbox.

Function DbColumn(strServer 
As String, strDB As String, strView 
As String, column As Variant)
Dim session As notesSession
Dim db As notesDatabase
Dim dbTmp As notesDatabase 
Dim viewTmp As NotesView
Dim docCTmp As
 notesDocumentCollection
Dim docTmp As notesDocument
Dim col As notesViewColumn
Dim itm As notesItem
Dim iTmp As Integer  
Dim aList() As String
Dim i As Integer
Dim j As Integer
Dim strField As String
Dim strFormula As String
Dim strLS As String

Set session = New NotesSession
Set db = session.currentDatabase

If strServer = "" Then
strServer = db.Server
End If
If strDB = "" Then
Set dbTmp = db
Else
Set dbTmp = session.GetDatabase
(strServer, strDB)
End If
 
If dbTmp Is Nothing Then
Msgbox "Attempt to lookup to a 
non-existent database on " & strServer, 
MB_ICONSTOP + MB_OK, "Error"
DbColumn = ""
Exit Function
End If

Set viewTmp = dbTmp.GetView(strView)
If viewTmp Is Nothing Then
Msgbox "DBCOLUMN : Attempt to lookup 
to a non-existent view " & strView & ", 
Server=" & dbTmp.Server & " NSF=" & 
db.FilePath, MB_ICONSTOP + MB_OK, "Error"
DbColumn = ""
Exit Function
End If

Set docTmp = viewTmp.GetFirstDocument
iTmp = 0
While Not (docTmp Is Nothing)     
Select Case Datatype(column)
Case V_STRING:
strField = column
ITM_NAME:               
Set itm = docTmp.GetFirstItem(strField)
If itm Is Nothing Then 
   valTmp = Null
Else
valTmp = itm.values
End If
Case V_INTEGER, V_LONG:
Set col = viewTmp.Columns(column-1)
strFormula = col.Formula
If strFormula = "" Then
strField = col.ItemName
Goto ITM_NAME
Else
Set docG = docTmp
strLS = "valTmp = Evaluate(""" & 
strFormula & """, docG)"
Execute strLS
End If
Case Else:
Msgbox "Invalid parameter for DbColumn", 
MB_ICONSTOP + MB_OK, "Error"
Exit Function
End Select
If Not Isnull(valTmp) Then
For j = 0 To Ubound(valTmp)
Redim Preserve aList(iTmp)
aList(iTmp) = valTmp(j)
iTmp = iTmp + 1
Next
End If
Set docTmp = viewTmp.
GetNextDocument(docTmp)
Wend
If iTmp = 0 Then 
DbColumn = ""
Else
DbColumn = aList      
End If
 
End Function

Function DbLookup(strServer As String, 
strDB As String, strView As String, 
strKey As String, column As Variant)
Dim session As notesSession
Dim db As notesDatabase
Dim dbTmp As notesDatabase 
Dim viewTmp As NotesView
Dim docCTmp As 
notesDocumentCollection
Dim docTmp As notesDocument
Dim col As notesViewColumn
Dim itm As notesItem
Dim iTmp As Integer  
Dim aList() As String
Dim i As Integer
Dim j As Integer
Dim strField As String
Dim strFormula As String
Dim strLS As String

Set session = New NotesSession
Set db = session.currentDatabase

If strServer = "" Then
strServer = db.Server
End If
If strDB = "" Then
Set dbTmp = db
Else
Set dbTmp = session.GetDatabase
(strServer, strDB)
End If
 
If dbTmp Is Nothing Then
Msgbox "Attempt to lookup to a 
non-existent database on " & strServer,
 MB_ICONSTOP + MB_OK, "Error"
DbLookup = ""
Exit Function
End If

Set viewTmp = dbTmp.GetView(strView)
If viewTmp Is Nothing Then
Msgbox "DBLOOKUP : Attempt to lookup 
to a non-existent view " & strView & ",
 Server=" & dbTmp.Server & " NSF=" & 
db.FilePath, MB_ICONSTOP + MB_OK, "Error"
DbLookup = ""
Exit Function
End If

Set docCTmp = viewTmp.
GetAllDocumentsbyKey(strKey , True)
iTmp = 0
For i = 1 To docCTmp.count
Set docTmp = docCTmp.GetNthDocument(i)
Select Case Datatype(column)
Case V_STRING:
strField = column
ITM_NAME:               
Set itm = docTmp.GetFirstItem(strField)
If itm Is Nothing Then 
valTmp = Null
Else
valTmp = itm.values
End If
Case V_INTEGER, V_LONG:
Set col = viewTmp.Columns(column-1)
strFormula = col.Formula
If strFormula = "" Then
strField = col.ItemName
Goto ITM_NAME
Else
strField = col.ItemName
Set docG = docTmp
strLS = "valTmp = Evaluate(""" & 
strFormula & """, docG)"
Execute strLS
End If
Case Else:
Msgbox "Invalid parameter for DbLookup", 
MB_ICONSTOP + MB_OK, "Error"
Exit Function
End Select
If Not Isnull(valTmp) Then
For j = 0 To Ubound(valTmp)
Redim Preserve aList(iTmp)
aList(iTmp) = valTmp(j)
iTmp = iTmp + 1
Next
End If
Next
If iTmp = 0 Then 
DbLookup = ""
Else
DbLookup = aList      
End If
 
End Function

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Nithya K. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our tip contest and you could win a prize.

This was first published in June 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

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.