Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Would someone please give me some example code that adds numbers to a field in an MS Access database. I wish to replace the autonumber feature with a VB 6 created one. Basically add consequetive numbers for each record I add to the database via my VB 6 Forms.
Thank you.
M

You add numbers just the same way as you add any other data. Define a Field as a long Int and take it from there. The difference is that in this instance you are going to have to keep track of where you are up to yourself.
Personally I would stick with the auto-number feature. The auto-number field is a very good way of generating a guaranteed unique data item for use in a one to may relationship. It works, its effective and takes some of the maintenance problems away from you and gives you a maximum count of over 4 billion.
Stuart

Thankyou Stuart but I am newish to VB and autonumber is no use to me in this instance as you cannot sort by it. I need to sort by this field. Surely VB can know what number it is up to.
An example of code the way you would do it would be sufficient.
Thankyou.
M

You can sort by Auto-number. Once the number is automatically inserted by the auto number facility it behaves like any other numerical data. You can sort either in Access itself or using the Order By clause in an SQL query.
If you override the auto function then VB cannot possibly know what number its up to, you have to tell it. It's just an abstract number that could mean anything. Anyway, its not VB doing to the work, it the Microsoft Jet Database Engine which VB talks to which in turn talks to the Access Database. Every time you add a record you are going to need to find out what the number of the last record added was. You can do that by either keeping a separate tally in another field or using an SQL query to find the highest number in the table. Not very efficient.
Its not VB you need to learn about, it's Access database and how it works. VB is just used to manipulate the database via the Jet Engine.
An example code of the way I would do it is to use Autonumber. You define a field of Type auto number and every time you create a new record, that field is filled in for you. The numbers are not created in sequential order and there is no need for them to be. If you want numbers in sequential order, you are going to have to do it yourself.
Sample code is not necessary. It's no different than adding any other type of data. I assume you know how to add a record to a database. If not, you have a lot of reading to do.
Stuart

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |