Solved

How to create an autonumber field in DAO?

Posted on 1997-10-17
9
386 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now