Unique & Easy Sequential Numbering

Unlike other methods for generating sequential numbers, this method guarantees
that you have a unique number every time. Since Notes does not enforce record
locking, using a dblookup on a document storing the last used number can result
in two users getting the same number if your application is heavily used. This
can cause confusion if your application or users depend on the uniqueness of
the ID number. This tip is an easy method for using file locking to get a
number that is unique and sequential (and faster than a dblookup). The only
requirement is that your users all have access to the same shared network
drive, mapped to the same letter, where the file storing the last used number
is kept. I usually name the file something ending with a .bin extension.The
first part of the code is an action agent to create and / or set the initial
number in the file. The second part of the code is a subroutine to be called
from the QuerySave event (or possibly inserted into it) to get the number. I
use a database profile
document here to store the file name and path, but you can replace this with a
constant string to simplify.
Sub Initialize
'this agent sets the binary number file to the value entered at the prompt
'it should only be run once to create the binary file, or to recreate it
or alter its
'initial value at a later date
Dim session As New NotesSession
Dim db As NotesDatabase
Dim profiledoc As NotesDocument 'database profile document
Dim filenum As Integer
Dim seqnum As Long 'the number to get/write
Dim numberfile As String 'path and filename of number file on shared
drive

'If not using a profile document, these are not required
Set db = session.CurrentDatabase
Set profiledoc = db.GetProfileDocument("f_dbProfile")

filenum = Freefile
'if not using a profile document, set numberfile to the path and filename
numberfile = profiledoc.p_NumberFile(0) 'set path & filename of shared
drive file

Open numberfile For Binary Access Read Write Lock Read Write As filenum
seqnum = Clng(Inputbox$( "Enter the new number","Set Number"))
Put #filenum, 1, seqnum 'write it
Close filenum

End Sub

Sub GetNumber()
'This sub should be called from querysave to get the unique idnumber. The code
' could also be placed directly into the querysave event
Const MAXATTEMPTS = 5 'maximum number of attempts to read/write number
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim uidoc As NotesUIDocument 'document to have idnumber set on it
Dim profiledoc As NotesDocument 'database profile document
Dim filenum As Integer
Dim failcount As Integer 'counts attempts at r/w
Dim success As Integer 'boolean test var
Dim seqnum As Long 'the number to get/write
Dim tempnum As Long
Dim finalnum As String 'the final number
Dim numberfile As String 'path and filename of number file on shared
drive

Set db = session.CurrentDatabase
Set uidoc= ws.CurrentDocument 'current document,which will get the
idnumber
Set profiledoc = db.GetProfileDocument("f_dbProfile") 'database profile
doc

filenum = Freefile
success = False
'set numberfile to path and filename of the number storage file
numberfile = profiledoc.GetItemValue("p_NumberFile")(0)

On Error Goto Errorhandler 'error routine is at end of sub

Writenum:
Do While (failcount < MAXATTEMPTS And Not success)
Open numberfile For Binary Access Read Write Lock Read Write As
filenum
Get #filenum,1,seqnum 'get the number for use from the
file
Call uidoc.FieldSetText("IDNumber", Cstr(seqnum)) 'set IDNumber
seqnum = seqnum + 1 'increment the number for the next use
Put #filenum, 1, seqnum 'write number for next use back to
file
Close filenum
success = True 'set exit condition
Loop
Exit Sub

Errorhandler:
Close 'close all open files
failcount = failcount + 1 'increment the number of failed attempts
Resume Writenum 'retry

End Sub

Dig Deeper on Domino Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

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 ...

SearchDataCenter

SearchContentManagement

Close