473,791 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AutoNumber Regeneration

Sometimes I use Autonumber fields for ID fields. Furthermore,
sometimes I use those same fields in orderdetail type tables. So it's
important in that case that once an autonumber key value is assigned to
a record that it doesn't change. Occasionally I find that due to
corruption or an accidental deletion and restore of a record from a
backup the autonumber field needs to be tidied up. So when I create
(through AddNew) the autonumber key to be used for joins, I also save a
copy in a backup ID field (Long). I could get by with always using the
backup ID for the join but I don't like having backup ID's that are
different from the autonumber value. I decided that I really wanted to
regenerate the autonumber field to match the Backup ID values. I
couldn't get the 'force update on autonumber field to previously
deleted values' idea from a recent post to work so I created some code
to do it. It's still a little rough but might suffice to get someone
to point out an easier way. The code is in A97. I didn't have any RI
to deal with. The form shows the tables in the database and once the
table is selected, the fields populate two comboboxes for choosing the
primary key field and the backup ID field. txtNewTableName is for the
name of the new table with the repaired autonumber values. The main
idea is to use AddNew without an Update until the next backup ID is
reached.

'-------Form Code
Option Compare Database
Option Explicit

Private Sub cbxDatabaseTabl e_AfterUpdate()
Dim MyDB As Database
Dim tdf As TableDef
Dim fld As Field

If IsNull(cbxDatab aseTable.Value) Then
cbxIDFieldName. RowSource = ""
cbxBackupIDFiel dName.RowSource = ""
cbxIDFieldName. Value = Null
cbxBackupIDFiel dName.Value = Null
Exit Sub
End If
'Put the field names in cbxIDFieldName and cbxBackupIDFiel dName
Set MyDB = CurrentDb
cbxIDFieldName. RowSourceType = "Value List"
cbxBackupIDFiel dName.RowSource Type = "Value List"
For Each fld In MyDB.TableDefs( cbxDatabaseTabl e.Value).Fields
If Nz(cbxIDFieldNa me.RowSource, "") = "" Then
cbxIDFieldName. RowSource = fld.Name
Else
cbxIDFieldName. RowSource = cbxIDFieldName. RowSource _
& ";" & fld.Name
End If
If Nz(cbxBackupIDF ieldName.RowSou rce, "") = "" Then
cbxBackupIDFiel dName.RowSource = fld.Name
Else
cbxBackupIDFiel dName.RowSource = cbxBackupIDFiel dName.RowSource _
& ";" & fld.Name
End If
Next fld
Set MyDB = Nothing
End Sub

Private Sub cmdFixAutonumbe r_Click()
If IsNull(cbxDatab aseTable.Value) Then
MsgBox ("No table was selected.")
Exit Sub
End If
If IsNull(txtNewTa bleName.Value) Then
MsgBox ("No new table name was selected.")
Exit Sub
End If
If IsNull(cbxIDFie ldName.Value) Then
MsgBox ("No ID Field was selected.")
Exit Sub
End If
If IsNull(cbxBacku pIDFieldName.Va lue) Then
MsgBox ("No Backup ID Field was selected.")
Exit Sub
End If
Call FixAutoNumber(c bxDatabaseTable .Value, txtNewTableName .Value, _
cbxIDFieldName. Value, cbxBackupIDFiel dName.Value)
MsgBox ("Done.")
End Sub

Private Sub Form_Load()
Dim MyDB As Database
Dim tdfLoop As TableDef

Set MyDB = CurrentDb
cbxDatabaseTabl e.RowSourceType = "Value List"
For Each tdfLoop In MyDB.TableDefs
If Left(tdfLoop.Na me, 4) <> "MSys" Then
If Nz(cbxDatabaseT able.RowSource, "") = "" Then
cbxDatabaseTabl e.RowSource = tdfLoop.Name
Else
cbxDatabaseTabl e.RowSource = cbxDatabaseTabl e.RowSource _
& ";" & tdfLoop.Name
End If
End If
Next
Set MyDB = Nothing
End Sub
'-------End Form Code

'-------Module Code
Option Compare Database
Option Explicit

Public Sub FixAutoNumber(s trOriginal As String, strNew As String, _
strIDFieldName As String, strBackupIDFiel dName As String)
Dim MyDB As Database
Dim AutoRS As Recordset
Dim NewRS As Recordset
Dim strSQL As String
Dim tdfAuto As TableDef
Dim fldAuto As Field
Dim lngCount As Long
Dim lngI As Long
Dim lngKey As Long
Dim tdf As TableDef
Dim fld As Field
Dim idxAuto As Index
Dim idx As Index
Dim boolFound As Boolean

'Place contents of table called strOriginal into table called
'strNew whenever the new autonumber matches BackupID
Set MyDB = CurrentDb
'Make sure index names and fields match
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
boolFound = False
For Each fld In idxAuto.Fields
If idxAuto.Name = fld.Name Then
boolFound = True
Exit For
End If
Next fld
If boolFound = False Then
MsgBox ("An index name doesn't match a field name.")
Set MyDB = Nothing
Exit Sub
End If
End If
Next idxAuto
'Delete the new table if it already exists
For Each tdf In MyDB.TableDefs
If tdf.Name = strNew Then
MyDB.Execute "DROP TABLE " & strNew & ";"
Exit For
End If
Next tdf
Set tdf = MyDB.CreateTabl eDef(strNew)
Set tdfAuto = MyDB.TableDefs( strOriginal)
For Each fldAuto In tdfAuto.Fields
If fldAuto.Type = dbText Then
Set fld = tdf.CreateField (fldAuto.Name, dbText, fldAuto.Size)
Else
Set fld = tdf.CreateField (fldAuto.Name, fldAuto.Type)
fld.Attributes = fldAuto.Attribu tes
End If
tdf.Fields.Appe nd fld
Next fldAuto
MyDB.TableDefs. Append tdf
tdf.Fields.Refr esh
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
Set idx = tdf.CreateIndex (idxAuto.Name)
If idxAuto.Name = strIDFieldName Then idx.Primary = True
If idxAuto.Require d Then idx.Required = True
idx.Fields.Appe nd idx.CreateField (idxAuto.Name)
tdf.Indexes.App end idx
End If
Next idxAuto
tdf.Indexes.Ref resh
DoEvents
strSQL = "SELECT * FROM " & strOriginal & " ORDER BY " _
& strBackupIDFiel dName & ";"
Set AutoRS = MyDB.OpenRecord set(strSQL, dbOpenSnapshot)
strSQL = "SELECT * FROM " & strNew & ";"
Set NewRS = MyDB.OpenRecord set(strSQL, dbOpenDynaset)
If AutoRS.RecordCo unt > 0 Then
AutoRS.MoveLast
lngCount = AutoRS.RecordCo unt
AutoRS.MoveFirs t
For lngI = 1 To lngCount
lngKey = 0
Do Until lngKey = AutoRS(strBacku pIDFieldName)
NewRS.AddNew
DoEvents
lngKey = NewRS(strIDFiel dName)
Loop
For Each fldAuto In tdfAuto.Fields
If fldAuto.Name <> strIDFieldName Then
NewRS(fldAuto.N ame) = AutoRS(fldAuto. Name)
End If
Next fldAuto
NewRS.Update
If lngI <> lngCount Then AutoRS.MoveNext
Next lngI
End If
AutoRS.Close
Set AutoRS = Nothing
NewRS.Close
Set NewRS = Nothing
Set MyDB = Nothing
End Sub
'-------End Module Code

James A. Fortune

Nov 13 '05 #1
26 3821
The issue of using AutoNumber keys in joins is problematic for the reasons
outlined. You should use hardcoded id's for joins to avoid the issue
altogether...
Tony D'Ambra
Web Site: aadconsulting.c om
Web Blog: accessextra.net

<ji********@com pumarc.com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
Sometimes I use Autonumber fields for ID fields. Furthermore,
sometimes I use those same fields in orderdetail type tables. So it's
important in that case that once an autonumber key value is assigned to
a record that it doesn't change. Occasionally I find that due to
corruption or an accidental deletion and restore of a record from a
backup the autonumber field needs to be tidied up. So when I create
(through AddNew) the autonumber key to be used for joins, I also save a
copy in a backup ID field (Long). I could get by with always using the
backup ID for the join but I don't like having backup ID's that are
different from the autonumber value. I decided that I really wanted to
regenerate the autonumber field to match the Backup ID values. I
couldn't get the 'force update on autonumber field to previously
deleted values' idea from a recent post to work so I created some code
to do it. It's still a little rough but might suffice to get someone
to point out an easier way. The code is in A97. I didn't have any RI
to deal with. The form shows the tables in the database and once the
table is selected, the fields populate two comboboxes for choosing the
primary key field and the backup ID field. txtNewTableName is for the
name of the new table with the repaired autonumber values. The main
idea is to use AddNew without an Update until the next backup ID is
reached.

'-------Form Code
Option Compare Database
Option Explicit

Private Sub cbxDatabaseTabl e_AfterUpdate()
Dim MyDB As Database
Dim tdf As TableDef
Dim fld As Field

If IsNull(cbxDatab aseTable.Value) Then
cbxIDFieldName. RowSource = ""
cbxBackupIDFiel dName.RowSource = ""
cbxIDFieldName. Value = Null
cbxBackupIDFiel dName.Value = Null
Exit Sub
End If
'Put the field names in cbxIDFieldName and cbxBackupIDFiel dName
Set MyDB = CurrentDb
cbxIDFieldName. RowSourceType = "Value List"
cbxBackupIDFiel dName.RowSource Type = "Value List"
For Each fld In MyDB.TableDefs( cbxDatabaseTabl e.Value).Fields
If Nz(cbxIDFieldNa me.RowSource, "") = "" Then
cbxIDFieldName. RowSource = fld.Name
Else
cbxIDFieldName. RowSource = cbxIDFieldName. RowSource _
& ";" & fld.Name
End If
If Nz(cbxBackupIDF ieldName.RowSou rce, "") = "" Then
cbxBackupIDFiel dName.RowSource = fld.Name
Else
cbxBackupIDFiel dName.RowSource = cbxBackupIDFiel dName.RowSource _
& ";" & fld.Name
End If
Next fld
Set MyDB = Nothing
End Sub

Private Sub cmdFixAutonumbe r_Click()
If IsNull(cbxDatab aseTable.Value) Then
MsgBox ("No table was selected.")
Exit Sub
End If
If IsNull(txtNewTa bleName.Value) Then
MsgBox ("No new table name was selected.")
Exit Sub
End If
If IsNull(cbxIDFie ldName.Value) Then
MsgBox ("No ID Field was selected.")
Exit Sub
End If
If IsNull(cbxBacku pIDFieldName.Va lue) Then
MsgBox ("No Backup ID Field was selected.")
Exit Sub
End If
Call FixAutoNumber(c bxDatabaseTable .Value, txtNewTableName .Value, _
cbxIDFieldName. Value, cbxBackupIDFiel dName.Value)
MsgBox ("Done.")
End Sub

Private Sub Form_Load()
Dim MyDB As Database
Dim tdfLoop As TableDef

Set MyDB = CurrentDb
cbxDatabaseTabl e.RowSourceType = "Value List"
For Each tdfLoop In MyDB.TableDefs
If Left(tdfLoop.Na me, 4) <> "MSys" Then
If Nz(cbxDatabaseT able.RowSource, "") = "" Then
cbxDatabaseTabl e.RowSource = tdfLoop.Name
Else
cbxDatabaseTabl e.RowSource = cbxDatabaseTabl e.RowSource _
& ";" & tdfLoop.Name
End If
End If
Next
Set MyDB = Nothing
End Sub
'-------End Form Code

'-------Module Code
Option Compare Database
Option Explicit

Public Sub FixAutoNumber(s trOriginal As String, strNew As String, _
strIDFieldName As String, strBackupIDFiel dName As String)
Dim MyDB As Database
Dim AutoRS As Recordset
Dim NewRS As Recordset
Dim strSQL As String
Dim tdfAuto As TableDef
Dim fldAuto As Field
Dim lngCount As Long
Dim lngI As Long
Dim lngKey As Long
Dim tdf As TableDef
Dim fld As Field
Dim idxAuto As Index
Dim idx As Index
Dim boolFound As Boolean

'Place contents of table called strOriginal into table called
'strNew whenever the new autonumber matches BackupID
Set MyDB = CurrentDb
'Make sure index names and fields match
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
boolFound = False
For Each fld In idxAuto.Fields
If idxAuto.Name = fld.Name Then
boolFound = True
Exit For
End If
Next fld
If boolFound = False Then
MsgBox ("An index name doesn't match a field name.")
Set MyDB = Nothing
Exit Sub
End If
End If
Next idxAuto
'Delete the new table if it already exists
For Each tdf In MyDB.TableDefs
If tdf.Name = strNew Then
MyDB.Execute "DROP TABLE " & strNew & ";"
Exit For
End If
Next tdf
Set tdf = MyDB.CreateTabl eDef(strNew)
Set tdfAuto = MyDB.TableDefs( strOriginal)
For Each fldAuto In tdfAuto.Fields
If fldAuto.Type = dbText Then
Set fld = tdf.CreateField (fldAuto.Name, dbText, fldAuto.Size)
Else
Set fld = tdf.CreateField (fldAuto.Name, fldAuto.Type)
fld.Attributes = fldAuto.Attribu tes
End If
tdf.Fields.Appe nd fld
Next fldAuto
MyDB.TableDefs. Append tdf
tdf.Fields.Refr esh
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
Set idx = tdf.CreateIndex (idxAuto.Name)
If idxAuto.Name = strIDFieldName Then idx.Primary = True
If idxAuto.Require d Then idx.Required = True
idx.Fields.Appe nd idx.CreateField (idxAuto.Name)
tdf.Indexes.App end idx
End If
Next idxAuto
tdf.Indexes.Ref resh
DoEvents
strSQL = "SELECT * FROM " & strOriginal & " ORDER BY " _
& strBackupIDFiel dName & ";"
Set AutoRS = MyDB.OpenRecord set(strSQL, dbOpenSnapshot)
strSQL = "SELECT * FROM " & strNew & ";"
Set NewRS = MyDB.OpenRecord set(strSQL, dbOpenDynaset)
If AutoRS.RecordCo unt > 0 Then
AutoRS.MoveLast
lngCount = AutoRS.RecordCo unt
AutoRS.MoveFirs t
For lngI = 1 To lngCount
lngKey = 0
Do Until lngKey = AutoRS(strBacku pIDFieldName)
NewRS.AddNew
DoEvents
lngKey = NewRS(strIDFiel dName)
Loop
For Each fldAuto In tdfAuto.Fields
If fldAuto.Name <> strIDFieldName Then
NewRS(fldAuto.N ame) = AutoRS(fldAuto. Name)
End If
Next fldAuto
NewRS.Update
If lngI <> lngCount Then AutoRS.MoveNext
Next lngI
End If
AutoRS.Close
Set AutoRS = Nothing
NewRS.Close
Set NewRS = Nothing
Set MyDB = Nothing
End Sub
'-------End Module Code

James A. Fortune

Nov 13 '05 #2
"Tony D'Ambra" <td*****@swiftd sl.com.au> wrote:
The issue of using AutoNumber keys in joins is problematic for the reasons
outlined. You should use hardcoded id's for joins to avoid the issue
altogether.. .


Eh? I've been using autonumber primary keys in all my systems since the first one I
created in A2.0 using natural keys. Thus the autonumber keys are present in all the
joins. Or am I misunderstandin g something?

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #3
In A97 you can APPEND a record to an autonumber table/field,
to put any unused value into the field.

In later versions you can ALSO change the table permissions
to allow you to edit autonumbers.

In both A97 and later versions you can APPEND a value to
an autonumber table/field in SQL Server, but it is a two-step
process in A97: later versions have /different/ problems
with SQL Server.

(david)

<ji********@com pumarc.com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
Sometimes I use Autonumber fields for ID fields. Furthermore,
sometimes I use those same fields in orderdetail type tables. So it's
important in that case that once an autonumber key value is assigned to
a record that it doesn't change. Occasionally I find that due to
corruption or an accidental deletion and restore of a record from a
backup the autonumber field needs to be tidied up. So when I create
(through AddNew) the autonumber key to be used for joins, I also save a
copy in a backup ID field (Long). I could get by with always using the
backup ID for the join but I don't like having backup ID's that are
different from the autonumber value. I decided that I really wanted to
regenerate the autonumber field to match the Backup ID values. I
couldn't get the 'force update on autonumber field to previously
deleted values' idea from a recent post to work so I created some code
to do it. It's still a little rough but might suffice to get someone
to point out an easier way. The code is in A97. I didn't have any RI
to deal with. The form shows the tables in the database and once the
table is selected, the fields populate two comboboxes for choosing the
primary key field and the backup ID field. txtNewTableName is for the
name of the new table with the repaired autonumber values. The main
idea is to use AddNew without an Update until the next backup ID is
reached.

'-------Form Code
Option Compare Database
Option Explicit

Private Sub cbxDatabaseTabl e_AfterUpdate()
Dim MyDB As Database
Dim tdf As TableDef
Dim fld As Field

If IsNull(cbxDatab aseTable.Value) Then
cbxIDFieldName. RowSource = ""
cbxBackupIDFiel dName.RowSource = ""
cbxIDFieldName. Value = Null
cbxBackupIDFiel dName.Value = Null
Exit Sub
End If
'Put the field names in cbxIDFieldName and cbxBackupIDFiel dName
Set MyDB = CurrentDb
cbxIDFieldName. RowSourceType = "Value List"
cbxBackupIDFiel dName.RowSource Type = "Value List"
For Each fld In MyDB.TableDefs( cbxDatabaseTabl e.Value).Fields
If Nz(cbxIDFieldNa me.RowSource, "") = "" Then
cbxIDFieldName. RowSource = fld.Name
Else
cbxIDFieldName. RowSource = cbxIDFieldName. RowSource _
& ";" & fld.Name
End If
If Nz(cbxBackupIDF ieldName.RowSou rce, "") = "" Then
cbxBackupIDFiel dName.RowSource = fld.Name
Else
cbxBackupIDFiel dName.RowSource = cbxBackupIDFiel dName.RowSource _
& ";" & fld.Name
End If
Next fld
Set MyDB = Nothing
End Sub

Private Sub cmdFixAutonumbe r_Click()
If IsNull(cbxDatab aseTable.Value) Then
MsgBox ("No table was selected.")
Exit Sub
End If
If IsNull(txtNewTa bleName.Value) Then
MsgBox ("No new table name was selected.")
Exit Sub
End If
If IsNull(cbxIDFie ldName.Value) Then
MsgBox ("No ID Field was selected.")
Exit Sub
End If
If IsNull(cbxBacku pIDFieldName.Va lue) Then
MsgBox ("No Backup ID Field was selected.")
Exit Sub
End If
Call FixAutoNumber(c bxDatabaseTable .Value, txtNewTableName .Value, _
cbxIDFieldName. Value, cbxBackupIDFiel dName.Value)
MsgBox ("Done.")
End Sub

Private Sub Form_Load()
Dim MyDB As Database
Dim tdfLoop As TableDef

Set MyDB = CurrentDb
cbxDatabaseTabl e.RowSourceType = "Value List"
For Each tdfLoop In MyDB.TableDefs
If Left(tdfLoop.Na me, 4) <> "MSys" Then
If Nz(cbxDatabaseT able.RowSource, "") = "" Then
cbxDatabaseTabl e.RowSource = tdfLoop.Name
Else
cbxDatabaseTabl e.RowSource = cbxDatabaseTabl e.RowSource _
& ";" & tdfLoop.Name
End If
End If
Next
Set MyDB = Nothing
End Sub
'-------End Form Code

'-------Module Code
Option Compare Database
Option Explicit

Public Sub FixAutoNumber(s trOriginal As String, strNew As String, _
strIDFieldName As String, strBackupIDFiel dName As String)
Dim MyDB As Database
Dim AutoRS As Recordset
Dim NewRS As Recordset
Dim strSQL As String
Dim tdfAuto As TableDef
Dim fldAuto As Field
Dim lngCount As Long
Dim lngI As Long
Dim lngKey As Long
Dim tdf As TableDef
Dim fld As Field
Dim idxAuto As Index
Dim idx As Index
Dim boolFound As Boolean

'Place contents of table called strOriginal into table called
'strNew whenever the new autonumber matches BackupID
Set MyDB = CurrentDb
'Make sure index names and fields match
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
boolFound = False
For Each fld In idxAuto.Fields
If idxAuto.Name = fld.Name Then
boolFound = True
Exit For
End If
Next fld
If boolFound = False Then
MsgBox ("An index name doesn't match a field name.")
Set MyDB = Nothing
Exit Sub
End If
End If
Next idxAuto
'Delete the new table if it already exists
For Each tdf In MyDB.TableDefs
If tdf.Name = strNew Then
MyDB.Execute "DROP TABLE " & strNew & ";"
Exit For
End If
Next tdf
Set tdf = MyDB.CreateTabl eDef(strNew)
Set tdfAuto = MyDB.TableDefs( strOriginal)
For Each fldAuto In tdfAuto.Fields
If fldAuto.Type = dbText Then
Set fld = tdf.CreateField (fldAuto.Name, dbText, fldAuto.Size)
Else
Set fld = tdf.CreateField (fldAuto.Name, fldAuto.Type)
fld.Attributes = fldAuto.Attribu tes
End If
tdf.Fields.Appe nd fld
Next fldAuto
MyDB.TableDefs. Append tdf
tdf.Fields.Refr esh
For Each idxAuto In MyDB.TableDefs( strOriginal).In dexes
If idxAuto.Name <> "PrimaryKey " Then
Set idx = tdf.CreateIndex (idxAuto.Name)
If idxAuto.Name = strIDFieldName Then idx.Primary = True
If idxAuto.Require d Then idx.Required = True
idx.Fields.Appe nd idx.CreateField (idxAuto.Name)
tdf.Indexes.App end idx
End If
Next idxAuto
tdf.Indexes.Ref resh
DoEvents
strSQL = "SELECT * FROM " & strOriginal & " ORDER BY " _
& strBackupIDFiel dName & ";"
Set AutoRS = MyDB.OpenRecord set(strSQL, dbOpenSnapshot)
strSQL = "SELECT * FROM " & strNew & ";"
Set NewRS = MyDB.OpenRecord set(strSQL, dbOpenDynaset)
If AutoRS.RecordCo unt > 0 Then
AutoRS.MoveLast
lngCount = AutoRS.RecordCo unt
AutoRS.MoveFirs t
For lngI = 1 To lngCount
lngKey = 0
Do Until lngKey = AutoRS(strBacku pIDFieldName)
NewRS.AddNew
DoEvents
lngKey = NewRS(strIDFiel dName)
Loop
For Each fldAuto In tdfAuto.Fields
If fldAuto.Name <> strIDFieldName Then
NewRS(fldAuto.N ame) = AutoRS(fldAuto. Name)
End If
Next fldAuto
NewRS.Update
If lngI <> lngCount Then AutoRS.MoveNext
Next lngI
End If
AutoRS.Close
Set AutoRS = Nothing
NewRS.Close
Set NewRS = Nothing
Set MyDB = Nothing
End Sub
'-------End Module Code

James A. Fortune

Nov 13 '05 #4
david epsom dot com dot au wrote:
In A97 you can APPEND a record to an autonumber table/field,
to put any unused value into the field.

In later versions you can ALSO change the table permissions
to allow you to edit autonumbers.

In both A97 and later versions you can APPEND a value to
an autonumber table/field in SQL Server, but it is a two-step
process in A97: later versions have /different/ problems
with SQL Server.

(david)


Thanks for the excellent information. I learned a lot about how
indexes are implemented in Access from prior posts so writing the code
was not a total waste. The other post about rolling your own key, such
as MAX + 1, is also worthy of consideration in a case like mine where
collisions are rare. I'm simply trying to get the best of both worlds.

James A. Fortune
Access numerical data to pdf histogram:
http://www.oakland.edu/~fortune/Histogram.zip

Nov 13 '05 #5
> Access numerical data to pdf histogram:
http://www.oakland.edu/~fortune/Histogram.zip


I had an experimental version there. I will replace it tonight with
the correct one.

James A. Fortune

Nov 13 '05 #6
Firstly, I find your use of "Eh?" offensive. Your MVP status does not exempt
you from the rules of simple courtesy...

By hard-coded joins I mean joins made using a custom ID field: look at the
sample Northwinds .mdb join: Cutomers-Orders, which uses a custom ID field,
CustomerID, which is not an autonumber field.

Also, the following article excerpt is relevant:
Teach your Access users to be wary of AutoNumbered primary keys

Feb 5, 2002
Peter Nelson
© 2002 TechRepublic, Inc.

As an active Microsoft Access developer, I've got a good view of one side of
the database development environment. But as an active Microsoft Access
instructor, I also get to see what the rest of the nontechnical developer
world is doing with Access. It's this latter view of Access that has
revealed how common it is for nontechnical Access users to incorrectly use
an AutoNumber field type as a table's primary key.

Here is why it's so important to steer end users away from this dangerous
practice.

Ignorance is rarely bliss when it comes to Access
Most business users, or even junior developers, do not have an in-depth
understanding as to what a primary key is, let alone the ramifications of an
ill-conceived primary key selection.

When a user saves a new table in Access, a dialog box pops up asking if the
user wants to create a primary key if one doesn't exist. The message says,
"Although a primary key isn't required, it's highly recommended. A table
must have a primary key for you to create a relationship between this table
and other tables in the database. Do you want to create a primary key now?"

Many of the users I've talked to in introductory, intermediate, and
sometimes even advanced Access classes usually say, "I saw the message and I
figured, what the heck, seems like a good thing to do." So, while they are
actually taking the right step in creating a primary key, they don't know
how to correctly complete the task.

A simple plan
Poorly designed applications are usually more cumbersome and costly to
maintain than those with the benefit of good initial planning.

Well-designed databases are generally characterized by a group of tables
storing related data that is joined together through keys. For example, a
database that stores information on customers and their related orders would
likely have two tables: Customers and Orders. The Orders table would not
contain any information about an order's related customer (address, phone
number, and so forth). Instead, it would contain the key that identifies the
row containing the customer's information in the Customers table.

AutoNumber: A double-edged sword
When choosing a key, it's generally a bad idea to choose a field that can be
edited by a user. Doing so forces you either to restrict the user from
editing the field after the record's creation or to provide a way of
detecting and correcting key collisions. If you restrict the user from
editing the field, you may discover that your application isn't flexible
enough. The second choice is problematic as well; providing for the
detection and correction of key collisions can be too complicated, seriously
hindering your application's performance.

The AutoNumber datatype offers a handy solution to this problem but not
without making your application more vulnerable to failure. On the positive
side, using the AutoNumber datatype provides a field that will give you a
unique value for every record, while also paving the way for establishing
relationships between multiple tables. The negative side is that the
application stands a much better chance of failing if the AutoNumbered
values become corrupt.

Tony D'Ambra
Web Site: aadconsulting.c om
Web Blog: accessextra.net

"Tony Toews" <tt****@teluspl anet.net> wrote in message
news:uo******** *************** *********@4ax.c om...
"Tony D'Ambra" <td*****@swiftd sl.com.au> wrote:
The issue of using AutoNumber keys in joins is problematic for the reasons
outlined. You should use hardcoded id's for joins to avoid the issue
altogether. ..


Eh? I've been using autonumber primary keys in all my systems since the
first one I
created in A2.0 using natural keys. Thus the autonumber keys are present
in all the
joins. Or am I misunderstandin g something?

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm

Nov 13 '05 #7
"Tony D'Ambra" <td*****@swiftd sl.com.au> wrote:
Firstly, I find your use of "Eh?" offensive. Your MVP status does not exempt
you from the rules of simple courtesy...
Sorry you found usage of that offensive. I did not realize that it was discourteous.
By hard-coded joins I mean joins made using a custom ID field: look at the
sample Northwinds .mdb join: Cutomers-Orders, which uses a custom ID field,
CustomerID, which is not an autonumber field.
Yup, I see what you mean. And as an example that's adequate. But what about the
OrderID field on the order table?

As far as I'm concerned Northwinds should have two versions. One with autonumber
primary keys and one with natural keys.
Also, the following article excerpt is relevant:
Teach your Access users to be wary of AutoNumbered primary keys
He's wrong. Autonumber keys work and work well. Finding a natural key and coding
it can be a lot of work. For example how do you get a natural key for a person?
Name? No. Name and birthdate? No. SSN? No. Name and address? Name and phone
number?

How do you handle the situation where you have invoice line items? Now you need code
to create the multipart key, ie invoice #, line 0001, line 0002, line 0003 and so on.
The negative side is that the
application stands a much better chance of failing if the AutoNumbered
values become corrupt.


This was a problem in some versions of Jet 4.0 but SPs fixed those. While I still
mostly work in A97 I've never had a problem with Autonumber values becoming corrupt.

Also the Access GUI simply doesn't handle multi part keys well. Although Jet seems
to reasonably well.

That said this can become a religuous argument. Tom Ellison has argued very
eloquently for the user of natural keys. But I still disagree and am exceedingly
happy with autonumber keys.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 13 '05 #8
rkc
Tony Toews wrote:
"Tony D'Ambra" <td*****@swiftd sl.com.au> wrote:

The negative side is that the
application stands a much better chance of failing if the AutoNumbered
values become corrupt.

This was a problem in some versions of Jet 4.0 but SPs fixed those. While I still
mostly work in A97 I've never had a problem with Autonumber values becoming corrupt.


What are the symptoms of a corrupt autonumber field? Do the values
already stored mysteriously change or are duplicates suddenly allowed
in a primary key field?

Or something else?

Nov 13 '05 #9
rkc <rk*@rochester. yabba.dabba.do. rr.bomb> wrote in
news:Ov******** ***********@twi ster.nyroc.rr.c om:
Tony Toews wrote:
"Tony D'Ambra" <td*****@swiftd sl.com.au> wrote:

The negative side is that the
applicatio n stands a much better chance of failing if the
AutoNumber ed values become corrupt.

This was a problem in some versions of Jet 4.0 but SPs fixed
those. While I still mostly work in A97 I've never had a problem
with Autonumber values becoming corrupt.


What are the symptoms of a corrupt autonumber field? Do the values
already stored mysteriously change or are duplicates suddenly
allowed in a primary key field?

Or something else?


The most common is the "loss of seed value" error, where each new
record gets the same Autonumber, sometimes a previously-used value,
sometimes not.

This was a very common weakness in the first versions of Jet 4, one
that I'd never seen in any previous version of Jet. It was fixed
about SP4 or so, if I'm remembering correctly.

Of course, it wasn't until SP6 that Jet 4 was really stable.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4775
by: Ilan Sebba | last post by:
I have a 'supertype' table with only one field: autonumber. Call this table the 'parent' table. There are two subtypes, 'androids' and 'martians'. Martian have only one thing in common: they give birth to identical mules. So each android and a martian have primary key which is a foreign key to the parent table. Now, I want to insert a new record in 'android' or 'martian'. This can be done easily using MS-Access forms. But I want to do...
33
4322
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and setting default value to a UDF that manages the auto-numbering. Access won't take a UDF as a default value. Okay, I'll use SQL WITHOUT any aggregate functions, for the default value. Access won't do that either. Okay, I create a second...
35
7278
by: Traci | last post by:
If I have a table with an autonumber primary key and 100 records and I delete the last 50 records, the next record added would have a primary key of 101. Is there any way to have the primary key start at 51 after the last 50 records are deleted? Thanks! Traci
4
5170
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is 4,294,967,295. Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber. Suppose an application (a) successfully INSERTS "X" records, then (b) successfully DELETES "Y" records (X >= Y), then
1
602
by: jimfortune | last post by:
Sometimes I use Autonumber fields for ID fields. Furthermore, sometimes I use those same fields in orderdetail type tables. So it's important in that case that once an autonumber key value is assigned to a record that it doesn't change. Occasionally I find that due to corruption or an accidental deletion and restore of a record from a backup the autonumber field needs to be tidied up. So when I create (through AddNew) the autonumber...
0
1065
by: raca | last post by:
Please look at this code fragment: #region Custom Code - Generated_Methods // Generated_Discussion_Methods public abstract IDataReader GetDiscussion(); public abstract void DeleteDiscussion(int itemID); #endregion // Custom Code - Generated_Methods // Generated_NEWCLASS_Methods public abstract IDataReader GetNEWCLASS();
8
15968
by: petebeatty | last post by:
I have created a SQL string the properly inserts a record in the table. However, the insert does not occur at the end of the table. Instead it inserts a record after the last record that I viewed. This would be OK, except it assigns a autonumber to be one greater than the last viewed record. This causes a duplicate autonumber. I know I can change the autonumber index (Primary Key) to not allow duplicates. How can I force the insert...
11
4499
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
1
3960
by: gtwannabe | last post by:
I'm having a problem with a form that uses AutoNumber as the primary key. I have an Abort button to delete the current record and close the form. If AutoNumber is assigned, the code executes a SQL statement that deletes the current record. I need to be able to detect when AutoNumber is unassigned (a new blank record) so that I can simply close the form without running the SQL delete statement. Unfortunately, no tests I can think of...
6
11763
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the contents of the form is inserted into the MS Access database. The Customer table in the database already has 30 records (with CustomerIDs 1 - 30) in it (from when the database was first created). The CustomerID field in the database is an...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.