Solved

How to create an autonumber field in DAO?

Posted on 1997-10-17
9
391 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 116
VBA Shell can't Find Word document 11 114
Help me. 3 60
Export Data to Different .csv Files 26 115
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 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…
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…

821 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