Solved

How to create an autonumber field in DAO?

Posted on 1997-10-17
9
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:Riffy
ID: 1438119
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
ID: 1438120
What about the RANDOM issue?
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1438121
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:cymbolic
ID: 1438122
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
 
LVL 6

Expert Comment

by:anthonyc
ID: 1438123
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
ID: 1438124
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
ID: 1438125
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
ID: 1438126
Yes, that's the answer!   Thanks.
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6982760
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
Excel - Save a copy of work book 13 96
RUNRMTCMD from AS/400 13 129
Assign a value in Excel V-Basic 3 38
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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