Per JR:
I am trying to set up a auotmatic numbering system for projects. Is there a
way to start a autonumbering at 64 or can I set the number to 64 and add +1
each time I add a new project? How do I do this???
I'm guessing that the project number is visible to users and is also being used
as the PK.
If that's the case, I'd use a plain-vanilla AutoNumber as the PK (let's call it
"ProjectID" and have a separate ProjectNumber field for what the users see.
Then you can keep something like stblRecordNumbers with the latest project
number in it and retrieve/increment it when needed. That would allow you to
start with 64. It would also accommodate breaks in the sequence and, in the
extreme, changes in the format/nature of a project number.
Here's what I do. It can be used for multiple field names.
------------------------------------------------------------
Function recordNumberGet(ByVal theName As String) As Long
4000 debugStackPush mModuleName & ": recordNumberGet"
4001 On Error GoTo recordNumberGet_err
' PURPOSE: To retrieve the latest number from a table of latest numbers,
' increment it by one, update the table, and return the number
' ACCEPTS: The name of the field for which we want the next record number
' RETURNS: The new record number or, in case of failure, zero
4010 Dim myRS As Recordset
Dim myQuery As QueryDef
Dim myNextNumber As Long
Dim myAttempts As Integer
Dim i As Integer
Dim v As Variant
Dim myDots As String
Dim beginTime As Double
Const somebodyElseHasRecordLocked = 3188
Const attemptsPerLoop = 50
Const maxLoops = 20
Const myTableName = "zstblRecordNumbers"
4030 beginTime = Now
4040 Set myQuery = curDB().QueryDefs("qryRecordNumberGet")
4041 With myQuery
4042 .Parameters("theName") = theName
4043 Set myRS = .OpenRecordset(dbOpenDynaset)
4049 End With
4061 With myRS
4160 If .BOF And .EOF Then
4170 .AddNew
4180 !FieldOrTableName = theName
4190 !LatestValue = 2
4200 .update
4210 recordNumberGet = 1
4220 Else
4230 .Edit 'Attempt to Lock that suckah!
4240 recordNumberGet = !LatestValue
4250 myNextNumber = !LatestValue + 1
4260 !LatestValue = myNextNumber
4270 .update
4280 recordNumberGet = myNextNumber
4290 End If
4299 End With
recordNumberGet_xit:
debugStackPop
On Error Resume Next
v = SysCmd(acSysCmdRemoveMeter)
Set myQuery = Nothing
myRS.Close
Set myRS = Nothing
Exit Function
recordNumberGet_err:
Select Case Err
Case somebodyElseHasRecordLocked
If myAttempts > maxLoops Then
DoCmd.Hourglass False
MsgBox "Table '" & myTableName & "' appears to be deadlocked after " &
Str(attemptsPerLoop * maxLoops) & " attempts made over a period of" &
Str(DateDiff("s", beginTime, Now)) & " seconds.", 16, "Fatal Error"
Resume recordNumberGet_xit
Else
For i = 1 To attemptsPerLoop
v = SysCmd(acSysCmdSetStatus, "Waiting for somebody else to update
" & theName & myDots)
DoEvents
Next i
myAttempts = myAttempts + 1
myDots = myDots & " ."
Resume
End If
Case Else
DoCmd.Hourglass False
MsgBox "Line " & Str(Erl) & ", Error " & Str(Err) & ": " & Error$, 48,
"recordNumberGet()"
Resume recordNumberGet_xit
End Select
End Function
------------------------------------------------------------
qryRecordNumberGet:
------------------------------------------------------------
PARAMETERS theName Text ( 255 );
SELECT DISTINCTROW zstblRecordNumbers.fieldOrTableName AS Expr1,
zstblRecordNumbers.latestValue AS Expr2
FROM zstblRecordNumbers
WHERE ((([zstblRecordNumbers].[fieldOrTableName])=[theName]));