Solved

How to create an autonumber field in DAO?

Posted on 1997-10-17
9
384 Views
Last Modified: 2008-03-17
I am creating a tabledef in code using DAO 3.5 and can not find the way to create an Autonumber field (Random).  When copying a current tabledef, the Autonumber field is picked up as a Long Integer (which is of course what it is.)  But what is the property setting? to make this field an Autonumber field?
0
Comment
Question by:bknouse
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:Riffy
Comment Utility
After setting up your field object you must set the Attributes property of that field object.

The following is the syntax to do this:

object.Attributes = dbAutoIncrField

See Vb's online documentation under Attributes for a comlete
explanation.
0
 
LVL 6

Author Comment

by:bknouse
Comment Utility
What about the RANDOM issue?
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
You can't make a "random" autonumber field.  Riffy was correct about how to make an auto increment field.  That's what an autonumber field is.  It increments.
0
 
LVL 9

Expert Comment

by:cymbolic
Comment Utility
Auto Number or auto increment fields are always sequentially created numbers automatically generated by your database.  In order to generate these columns randomly, you must define them as a numeric type field, and calculate your own random numbers.  Generally, if you are considering an autonumber field for a primary key, I would advise against it.  Your best bet is to create an algorithm that recognizes some inherent randomness in your underlying data, and generate a unique number key using that randomness as a base.  The problem with sequential keys as primary keys is that most database systems try to colocate new rows on the same pages based upon primary keys.  In using an autonumber field as a primary key, you create a "hot spot" page for all new updates (and most lookups also seem to involve newer data) that causes locking problems under multiuse situations.

Also, you are not in control of the keys in an autonumber situation, your database is.  They are solely incremental, always generate larger numbers, and you can never reuse lower numbers already allocated, even when their identified rows are long deleted.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
Cymbolic:

Your answer is correct about the autonumber field, but your advice on their use is absolutely horrid.  


0
 
LVL 6

Author Comment

by:bknouse
Comment Utility
Why in Access 95 (and 97) do they have the AutoNumber field that can be set to Random or Increment, yet that property is not available through VB?  I am trying to copy a new structure created for a table through code to many disk copies.  The primary key is an autoincrement field with a property of random.  When I create the field as simply dbAutoIncrField attribute, I only get the increment sequential property.  Why can Access do it and you can't in VB?

Thanks.
0
 
LVL 9

Accepted Solution

by:
cymbolic earned 50 total points
Comment Utility
Regarding the side issue of hot spots and sequential increasing keys, see the following from SQL server help:

While row-level locking is not a substitute for a well-designed application, there are specific scenarios where it is especially useful. Row-level locking is useful when a hotspot develops on tables structured as a sequential file. With SQL Server, hotspots can occur when records are inserted at the end of a table and one of the following conditions exists:

·      A table does not have an index.
·      A table has a nonclustered index.
·      A table has a clustered index with a monotonically increasing key (for example, a clustered index on an identity column).

When concurrent users try to insert data into the table's last page and contend for exclusive page access, a hotspot results. To alleviate these performance bottlenecks, enhancements to the lock manager in SQL Server 6.5 provide row-level concurrency for INSERT operations. Properly implemented IRL increases the speed of multiuser INSERT operations. The IRL operations occur in all data pages and in all levels of an index.

VB COUNTER type is only autoincrement on data type definition.  If you want random counter, why not define a source table using MS Access, then use the .CopyObject method to get  your template source table duplicated.
0
 
LVL 6

Author Comment

by:bknouse
Comment Utility
Yes, that's the answer!   Thanks.
0
 
LVL 6

Expert Comment

by:xSinbad
Comment Utility
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now