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

SearchDomino.com member Nithya K. shares a LotusScript code equivalent of @DBLookup and @DBColumn for Lotus Notes that doesn't decrease Domino Server performance.

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

Dig deeper on LotusScript

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close