Solved

How to create an autonumber field in DAO?

Posted on 1997-10-17
9
401 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

635 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