API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Get Database Function
It's pretty easy to get a database in LotusScript - NotesSession.GetDatabase(server$, path$[, createOnFail]). But what if you don't want to "create on fail"? The method will trigger an error if the database doesn't exist. In a lot of my applications, I want to gracefully handle errors. Maybe the user typed in the wrong database path - I want to give that error. Or maybe it's something that is purposely checking for missing databases - I don't want to error when it doesn't exist. So I wrote a function to get a database. If the database doesn't exist, the function returns the LotusScript constant Nothing instead of triggering an error.

Function GetDatabase(session As NotesSession, dbPath As String) As NotesDatabase
   On Error Goto BubbleError
   ' Get a handle to the passed-in Notes database. Format of the database path
   ' is server!!path. If there is no "!!", then the server is assumed to be local.
   ' If the session is on the server (running in a scheduled agent, for example) then
   ' the server should be local. It can go across servers if TrustedServer is enabled.
   Dim server As String
   Dim path As String
   Dim tempDb As NotesDatabase
   
   If InStr(dbPath, "!!") <> 0 Then
      server = StrLeft(dbPath, "!!")
      path = StrRight(dbPath, "!!")
   Else ' No "!!" in the passed-in database... assume local
      server = ""
      path = dbPath
   End If
   
   ' If the session is on the server and the server name is the current server (the "person"
   ' running the agent on schedule) then we can get the database locally
   If session.Isonserver Then
      If Abbreviate(session.Username) = Abbreviate(server) Then server = ""
   End If
   
   ' Attempt to open the database
   On Error Resume Next
   Set tempDb = session.Getdatabase(server, path, False)
   If Not tempDb.Isopen Then Call tempDb.Open("", "")
   If Not tempDb.Isopen Then Set tempDb = Nothing
   On Error GoTo BubbleError
   If Err <> 0 Then
      Err = 0
      Set tempDb = Nothing
   End If
   
   ' Return the now-open database, or NOTHING if it couldn't be opened or found
   Set GetDatabase = tempDb

   Exit Function
BubbleError:
   Error Err, Error$ & Chr$(10) & " in function " & Getthreadinfo(1) & ", line " & Cstr(Erl)
End Function

The Abbreviate function is a quick little utility function to abbreviate any name. That way I don't have to worry about if "CN=" is in there or not.

Function Abbreviate(fullName As String) As String
   On Error Goto BubbleError
   ' Take a name that may or may not be fully canonicalized (with CN=) and return
   ' the abbreviated name
   Dim tempName As NotesName
   Dim retVal As String
   
   Set tempName = New NotesName(fullName)
   retVal = tempName.Abbreviated
   Delete tempName
   
   Abbreviate = retVal

   Exit Function
BubbleError:
   Error Err, Error$ & Chr$(10) & " in function " & Getthreadinfo(1) & ", line " & Cstr(Erl)
End Function