If Me.YourCounterField = 0 Or IsNull(Me.YourCounterField) Then
Me.YourCounterField = Nz(DMax("YourCounterField", "YourTableName"), [B][COLOR="Red"]X[/COLOR][/B]) + 1
End If
The numbers generated by the AutoNumber function should be relied upon to produce nothing other than a unique Key., and are considered by many to be not fit for Human consumption.Code:If Me.YourCounterField = 0 Or IsNull(Me.YourCounterField) Then Me.YourCounterField = Nz(DMax("YourCounterField", "YourTableName"), [B][COLOR=red]X[/COLOR][/B]) + 1 End If
Note; Replace X with your seed number (the number you wish your series to start at).
' NextNumber
' -------------------------------------------------------------------
' A function to replace AutoNumber sequencing. Sample call:
' Me!AutoNumberedField = NextNumber ("tblMyTable","AutoNumberedField")
' where AutoNumberedField is Number/Long
'
' Optional Boolean argument for NextNumber determines whether the records will
' be compacted before issuing a new ordinal.
' So, NextNumber ("tblMyTable","AutoNumberedField", True) will cause the availabe low
' numbers from deletes to be reissued to existing records before returning the
' next available number. The original sequence remains unaffected.
'
Public Function NextNumber(tTable As String, fField As String, Optional sSwitch As Boolean) As Long
Dim dbs As DAO.Database
Dim rst As DAO.Recordset, SQLstr As String
Dim i As Integer
Set dbs = CurrentDb
SQLstr = "SELECT " & fField & " FROM " & tTable & " ORDER BY " & fField
On Error GoTo NextNumber_Error
Set rst = dbs.OpenRecordset(SQLstr, dbOpenDynaset)
i = 0
If Not rst.EOF Then
' if no optional switch present or its set to FALSE
' return the value of last record
If IsMissing(sSwitch) Or Not sSwitch Then
rst.MoveLast
i = rst.Fields(fField)
Else
rst.MoveFirst
Do While Not rst.EOF
i = i + 1
If rst.Fields(fField) <> i Then
rst.Edit
rst.Fields(fField) = i ' the final value of i = number of recs.
rst.Update
End If
rst.MoveNext
Loop
End If
End If
NextNumber = i + 1
ExitNextNumber:
rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing
Exit Function
NextNumber_Error:
MsgBox "NextNumber Error: " & Err.Number & " " & Err.Description
NextNumber = 0
Resume ExitNextNumber
End Function
That's an interesting function. Do you use it? The autonumber approach is used in other DBMS also eg Oracle Sequence.
see http://sqlzoo.net/howto/source/z.dir/tip000001/access
How do you deal with foreign keys if you compact and reseed Ids with your function?
I am puzzled by the Access engineers not dealing with the glaring weaknesses of the AutoNumber facility.
I say, make it the eleventh commandment for develpment in Access : STAY AWAY FROM AUTONUMBERS !
If a record is deleted then there is no point reusing the number because it clealy has no special meaning and any unique number would be fine.
A transaction number should never change. How would it be traceable if it changed?
Five years later an auditor contacts you and asks about a specific invoice number. You are going to tell them that number has been changed so you can't confirm it?
If you are going to run out of numbers then the choice of code is inappropriate. Using so many of the available digits to duplicate information in the record is where you started going wrong.
There is no danger of duplicating information in the table. Record 20130126 does not 'duplicate information' in record 20130001 just because the first four digits denote the year of the transaction.
Galaxiom said:Using so many of the available digits to duplicate information in the record is where you started going wrong.
If there is no realistic chance of having more than 99999 current transactions in a table why would it be wrong to fix length of the subkey ?
why would it be wrong to fix length of the subkey ?
Seems to me like you want to wax contentious, Galaxiom.
I am not talking about invoice numbers or cheque numbers but key subsets denoting transaction sequence. There is no issue of numbered document matching here. It is purely a question of internal order.
You are talking through your hat, Galaxiom. There is no danger of duplicating information in the table. Record 20130126 does not 'duplicate information' in record 20130001 just because the first four digits denote the year of the transaction. You just don't know what you are talking about. If there is no realistic chance of having more than 99999 current transactions in a table why would it be wrong to fix length of the subkey ?
Because you don't understand the difference between current and historical data ?
Jiri