| ' Assignes a unique number and formats it according to prefs. defined in profileNumber |
| ' profileNumber is NOT and in-memory profile document |
| |
| ' Inputs - |
| ' The name of the form to be assigned a number |
| ' Returns- |
| ' a sequential number |
| ' Calls- |
| ' MaskNumber, CreateProfileSequential |
| |
| Function GenerateSequentialNumber( formName$ ) As String |
| |
| Dim s As New notessession |
| Dim doc As notesdocument |
| Dim viewNumber As notesview |
| |
| Dim bSaved As Integer ' acts as semaphor |
| Dim tries%, delay% ' record locking |
| Dim num%, newnum% ' current and next number |
| Dim currentYear%, currentMonth%, currentDay% ' today in numbers |
| Dim txtYear$, txtMonth$, txtDay$ ' today in text |
| Dim proNum%, proYear%, proMonth%, proDay% ' last vals (in profileNumber) |
| Dim mask$, resetNum$ ' formatting (in profileNumber) |
| Dim maskedNewNum$ ' the final result |
| |
| Dim n As New notesname( s.currentdatabase.server ) |
| Dim srv$ |
| srv$ = n.abbreviated |
| If srv$ = "" Then srv$ = "[Local]" ' cant use blank as key |
| |
| On Error Goto errorHandling |
| |
| Set viewNumber = s.currentdatabase.getview( lookSequentialNumber$ ) ' lookup view |
| |
| bSaved = False |
| currentYear% = Year( Today ) |
| currentMonth% = Month( Today ) |
| currentDay% = Day( Today ) |
| tries% = 1 |
| |
| ' Loop while document profileNumber is not saved |
| ' If another user has the document open then it can not be saved. |
| ' Aborts after 10 tries and returns 0000 as number |
| While bSaved = False |
| |
| profileID$ = formName$ & "_" & srv$ ' key |
| |
| Set doc = viewNumber.getdocumentbykey( profileID$ ) |
| |
| ' Exit if no number setup document |
| [b]If doc Is Nothing Then Error 4001, "No sequential number setup document"[/b] |
| ' if disabled then return blank |
| If doc.mask(0) = "DISABLED" Then |
| GenerateSequentialNumber = "" |
| Exit Function ' exit |
| End If |
| |
| If Not doc.hasitem("Year") Then ' extra precautions |
| doc.Year = currentYear% |
| doc.Month = currentMonth% |
| doc.Day = currentDay% |
| End If |
| |
| ' Get settings from profile document |
| proNum% = doc.Number(0) |
| num% = proNum% |
| proYear% = doc.Year(0) |
| proMonth% = doc.Month(0) |
| proDay% = doc.Day(0) |
| mask$ = doc.Mask(0) |
| ResetNum$ = doc.Reset(0) |
| |
| |
| ' Year |
| If proYear% <> currentYear% Then doc.year = currentYear% |
| txtYear$ = Cstr( currentYear% ) |
| ' Month |
| If proMonth% <> currentMonth% Then doc.month = currentMonth% |
| If currentMonth% < 10 Then txtMonth$ = "0" & Cstr(currentMonth%) Else txtMonth$ = Cstr(currentMonth%) |
| ' Day |
| If proDay% <> currentDay% Then doc.day = currentDay% |
| If currentDay% < 10 Then txtDay$ = "0" & Cstr( currentDay% ) Else txtDay$ = Cstr( currentDay% ) |
| |
| ' Reset |
| Select Case ResetNum$ |
| Case "N" : num% = proNum% ' never |
| Case "Y" : If proYear% = currentYear% Then num% = proNum% Else num% = 0 ' year |
| Case "M" : If proMonth% = currentMonth% Then num% = proNum% Else num% = 0 ' month |
| Case "D" : If proDay% = currentDay% Then num% = proNum% Else num% = 0 ' day |
| End Select |
| |
| newnum% = num% + 1 ' set new number |
| |
| ' Mask (format) the new number |
| maskedNewNum$ = MaskNumber( newnum%, mask$, txtYear$, txtMonth$, txtDay$ ) |
| |
| ' Try to save doc |
| doc.Number = newnum% |
| bSaved = doc.save( False, False ) |
| |
| If bSaved = False Then |
| Set doc = Nothing ' release to prevent deadlock |
| tries% = tries% + 1 |
| If tries% > 10 Then |
| ' make sure we dont end up in an indefinate loop |
| Print msgBarTimedOut$ |
| GenerateSequentialNumber = "0000" ' exit and return 0000 |
| Exit Function |
| Else |
| For delay% = 1 To 10000 |
| ' wait |
| ' redo the whole thing again |
| Next |
| End If |
| Else |
| Print "Number assigned: " & maskedNewNum$ |
| End If |
| |
| Wend ' while bSaved = false |
| |
| |
| '*** New number has been generated and document profileNumber saved |
| GenerateSequentialNumber = maskedNewNum$ |
| |
| |
| Exit Function |
| |
| errorHandling: |
| Print "SequentialNumber.GenerateSequentialNumber reports:" |
| Print Err & " " & Error$ |
| Print "Sequential Number Generator Failed" |
| GenerateSequentialNumber = "0000" |
| Exit Function |
| End Function |