D
dymondjack
Second (third, fourth) opinions?
When I first started learning about access and VBA programming, I came
across a few threads regarding inconsistancies with the Autonumber datatype.
Here is some information on it provided by Allen Browne.
http://allenbrowne.com/ser-40.html
One would not expect to have to worry about failures with access's
autonumber field, but this type of problem could be detrimental should it
arise.
I have since come up with a workaround that involves a table (tblSys, again
adapted from Allen: http://allenbrowne.com/ser-40.html (Section 1)) to store
the last used number (Variable = "LastANum_" & tablename).
I set my PK fields to Long Integer rather than Autonumber, and I have a
public function fAutoNum(TableName) that looks up the last used number for
that particular table, and finally, edits the Value of the tblSys variable by
an increment of 1. The function looks something like this:
Public Function fAutoNum(TableName As String) As Long
Dim lID As Long
lID = 1 + Clng(Elookup("Value", _
"tblSys", _
"Variable = 'LastANum_" & TableName & "'"))
fSysTableEdit("Variable = 'LastANum_" & TableName & "'", Str(lID))
End Function
(fSysTableEdit updates a Value for the specified Variable in tblSys)
So, my function returns the next number for the table, and updates the value
by an increment of 1.
So far this has worked out very well. ID values on a form are easily set by
calling the function on the BeforeUpdate event of a new record, setting the
value in code when adding a record using the Recordset object is just as
easy, and calling the function from within an SQL string has not been a big
deal either. So as long as I follow the 2nd commandment (Thou shalt never
allow thy users to see or edit tables directly, but only through forms and
thou shalt abhor the use of "Lookup Fields" which art the creation of the
Evil One.) I think this should work out fine.
Aside from a slight performace drag when processing large amounts of
records, does anyone see any reason NOT to do this? I've never heard of it
being done before, and so far I've processed approx. 1500 records using this
method.
Has anyone ever tried this or can think of any major downfalls to this in
the longrun? The only issue I can see is with replication databases, but as
near as I can tell that would be an issue using the default Autonumber field
setting as well.
Any thoughts would be great, I'd hate to have this one bite me later on in
case I'm missing something.
Thanks!
--
Jack Leach
www.tristatemachine.com
- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery
When I first started learning about access and VBA programming, I came
across a few threads regarding inconsistancies with the Autonumber datatype.
Here is some information on it provided by Allen Browne.
http://allenbrowne.com/ser-40.html
One would not expect to have to worry about failures with access's
autonumber field, but this type of problem could be detrimental should it
arise.
I have since come up with a workaround that involves a table (tblSys, again
adapted from Allen: http://allenbrowne.com/ser-40.html (Section 1)) to store
the last used number (Variable = "LastANum_" & tablename).
I set my PK fields to Long Integer rather than Autonumber, and I have a
public function fAutoNum(TableName) that looks up the last used number for
that particular table, and finally, edits the Value of the tblSys variable by
an increment of 1. The function looks something like this:
Public Function fAutoNum(TableName As String) As Long
Dim lID As Long
lID = 1 + Clng(Elookup("Value", _
"tblSys", _
"Variable = 'LastANum_" & TableName & "'"))
fSysTableEdit("Variable = 'LastANum_" & TableName & "'", Str(lID))
End Function
(fSysTableEdit updates a Value for the specified Variable in tblSys)
So, my function returns the next number for the table, and updates the value
by an increment of 1.
So far this has worked out very well. ID values on a form are easily set by
calling the function on the BeforeUpdate event of a new record, setting the
value in code when adding a record using the Recordset object is just as
easy, and calling the function from within an SQL string has not been a big
deal either. So as long as I follow the 2nd commandment (Thou shalt never
allow thy users to see or edit tables directly, but only through forms and
thou shalt abhor the use of "Lookup Fields" which art the creation of the
Evil One.) I think this should work out fine.
Aside from a slight performace drag when processing large amounts of
records, does anyone see any reason NOT to do this? I've never heard of it
being done before, and so far I've processed approx. 1500 records using this
method.
Has anyone ever tried this or can think of any major downfalls to this in
the longrun? The only issue I can see is with replication databases, but as
near as I can tell that would be an issue using the default Autonumber field
setting as well.
Any thoughts would be great, I'd hate to have this one bite me later on in
case I'm missing something.
Thanks!
--
Jack Leach
www.tristatemachine.com
- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery