I am very new to writing code for Lotus Notes. I am wondering how to write a piece of code to counts the number of times a word (such as “car”) appears in documents in a folder or database? Also, can I put this into a view or would this have to appear in a form? Thanks for any advice/suggestions.
Subject: Help with counts…
To show in a view would require that each time a document is saved, the count is saved to a field. You can then display and sum that field in a view column.
The other option would be to create an agent that does it on the fly. The agent would traverse all documents in a document collection, and display the total count.
Below are two functions that may be of use for you in getting that count. --Ron
Function Occurrences(Byval sBig As String, sLittle As String) As Double
'This function returns the number of times the string sLittle occurrs in the string sBig.
'Returns -1 if sLittle is empty.
'
'Parameters:
' sBig = String to be searched.
' sLittle = Character (string) to search for in sBig.
'
'Calls:
' ReplaceSubString()[RL-Misc - Script Library]
'
'Examples:
' If Occurrences(Doc.SendTo(0), "@") <> 1 then MessageBox "Invalid e-mail address."
If Len(sLittle) = 0 Then
Occurrences = -1 'Invalid parameter so return -1.
Else
'The magic is performed by comparing the length of the origional string against the length
'of the string after the specified sub-string has been remvoed from it, and then doing an
'interger divide of the difference in length by the length of the string removed.
Occurrences = (Len(sBig) - Len(ReplaceSubString(sBig, sLittle, ""))) \ Len(sLittle)
End If
End Function
Function ReplaceSubString(Byval sSource As String, sSearch As String, sReplace As String) As String
'Replaces all occurances of string "A" with string "B". Returns source string
'if no occurances of string "A" are found.
'
'Parameters:
' sSource = String that replace is to be performed against.
' sSearch = Original string (character) that is to be removed from 'sSource'.
' sReplace = New string (character) that is to replace 'sSearch' in 'sSource'.
'
'A sample call would be:
' sString = ReplaceSubString(ReplaceSubString(sString, Chr(10), ""), Chr(13), "")
Dim iLenR As Integer
Dim iLenS As Integer
Dim iPos As Integer
iLenR = Len(sReplace)
iLenS = Len(sSearch)
iPos = Instr(1, sSource, sSearch)
While iPos > 0
sSource = Left(sSource, iPos - 1) + sReplace + Mid(sSource, iPos + iLenS)
iPos = Instr(iPos + iLenR, sSource, sSearch)
Wend
ReplaceSubString = sSource
End Function
Subject: RE: Help with counts…
Thanks for your help… I found something that I can use in an Agent that kinda works… However, I want to list the catagories and the counts within each catagory… here’s what I have…Sub Initialize
Dim db As NotesDatabase
Dim view As NotesView
Dim nav As NotesViewNavigator
Dim entry As NotesViewEntry
Dim count As Integer
Set db = New NotesDatabase( "", "buildreq.nsf" )
Set catview = db.GetView( "SPRINT PCCB Meeting" )
Set catnav = catview.CreateViewNav( )
Set catentry = catnav.GetFirst( )
catcount = 0
’ begin counting categories
’ stop when there are no more categories
Do Until catentry Is Nothing
Set nav = catview.CreateViewNavFromCategory("Scheduled")
Set entry = nav.GetFirstDocument
count = 0
’ begin counting documents
’ stop when there are no more documents
Do Until entry Is Nothing
count = count + 1
Set entry = nav.GetNextDocument( entry )
Loop
Messagebox "Number of documents = " & count
catcount = catcount + 1
Set catentry = catnav.GetNextCategory( catentry )
Loop
Messagebox "Number of categories = " & catcount
End Sub
The thing is, I want the catagory name (like “Scheduled”) to be used in the CreateViewNavFromCategory method and also display it… can you help?