Computing.Net > Forums > Programming > VB 6 & Access

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB 6 & Access

Reply to Message Icon

Name: Mark Long
Date: May 14, 2004 at 18:50:40 Pacific
OS: Win 98
CPU/Ram: 300/512
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: May 15, 2004 at 03:50:05 Pacific
Reply:

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


0

Response Number 2
Name: Mark Long
Date: May 15, 2004 at 04:40:31 Pacific
Reply:

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


0

Response Number 3
Name: StuartS
Date: May 15, 2004 at 05:56:49 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VB 6 & Access

VB 6 with Access www.computing.net/answers/programming/vb-6-with-access/12470.html

VB 6 & Access 2000 www.computing.net/answers/programming/vb-6-amp-access-2000/10789.html

Declaring Variables in VB 6.0 www.computing.net/answers/programming/declaring-variables-in-vb-60/9957.html