473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to Determine the Next Auto-Numbering Value for a Field

Hi all,

I have a table with an “autonumber” primary key field that also acts as
a foreign key in 2 other tables. I would like to programmatically add a
new record to the first table and the other 2 child tables. The reason
for doing this is to copy one complete record (parent and child table
records) into a new record so the user can make a few modifications.

So how do I determine what will be the next auto-generated number for my
primary key? I visualize doing this schematically like:

1. Open a Transaction
2. Get the next autonumber value
3. Add a new record to my first table (have Access generate the primary key)
4. Add a new record to my 2 child tables using the “next autonumber
value” as the foreign keys
5. End Transaction
I have always resisted using auto-numbered primary keys in this type of
instance for this very reason. But in this situation I almost had to.
This database was an “observation database” and it’s a bit difficult to
ask people to manually keep track of how many observations they take.

Thanks,

Phillip J. Allen
e-mail: pa*****@attglobal.net

Nov 12 '05 #1
4 6436
Phillip J. Allen wrote:

1. Open a Transaction
2. Get the next autonumber value
3. Add a new record to my first table (have Access generate the primary
key)
4. Add a new record to my 2 child tables using the “next autonumber
value” as the foreign keys
5. End Transaction


I recently solved this one (with some pain and suffering). Here's how I
did it:

---------------start code------------------
'create a connection
set conn = server.createObject("ADODB.connection")
conn.Mode = adModeReadWrite
conn.Timeout = 20
conn.open "DSN","user","pass"

'create a recordset
set rs = Server.CreateObject("ADODB.recordset")
rs.ActiveConnection = conn
rs.CursorLocation = adUseServer
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Source = "table name"
rs.Open

'Add the new record
rs.AddNew
rs("field1") = "value"
rs("field2") = "value"
rs.Update

rs.MoveLast

'get the newly created autonumber from the recordset
AutoNumber = rs("Auto-Number-Field")

'close the recordset and kill the object
rs.close
set rs = nothing

-------------------end code--------------------

Then i just did a loop for the rest of the database entries.

I used my connection and recordset options the "long way", but I find it
easier to remember what I was doing when I revisit the code months
later. You can put the connection params and recordset params on one
line if you want.

HTH,

--
Jon Trelfa
There 10 kinds of people in this world...
Those who understand binary and those who don't

Nov 12 '05 #2
rkc

"Phillip J. Allen" <pa*****@attglobal.net> wrote in message
news:3f********@news1.prserv.net...
Hi all,

I have a table with an “autonumber” primary key field that also acts as
a foreign key in 2 other tables. I would like to programmatically add a
new record to the first table and the other 2 child tables. The reason
for doing this is to copy one complete record (parent and child table
records) into a new record so the user can make a few modifications.

So how do I determine what will be the next auto-generated number for my
primary key? I visualize doing this schematically like:

1. Open a Transaction
2. Get the next autonumber value
3. Add a new record to my first table (have Access generate the primary key) 4. Add a new record to my 2 child tables using the “next autonumber
value” as the foreign keys
5. End Transaction


You can use the recordsets LastModified and Bookmark properties to retrieve
the autonumber of a just entered record. Steps 2 and 3 in your outline
would
be reversed.

rs.AddNew
rs!LastName = "Updike"
rs!FirstName = "Eugene"
rs.Update

rs.Bookmark = rs.LastModified
Autonumber = rs.EmployeeID

Nov 12 '05 #3
rkc

"John Mishefske" <mi****@execpc.com> wrote in message
news:vm***********@corp.supernews.com...
rkc wrote:
You can use the recordsets LastModified and Bookmark properties to retrieve the autonumber of a just entered record. Steps 2 and 3 in your outline
would
be reversed.

rs.AddNew
rs!LastName = "Updike"
rs!FirstName = "Eugene"
rs.Update

rs.Bookmark = rs.LastModified
Autonumber = rs.EmployeeID

How about:

rs.AddNew
rs!LastName = "Updike"
rs!FirstName = "Eugene"
Autonumber = rs!EmployeeID
rs.Update

Which works fine inside Jet but ODBC?


I should mention that I was assuming DAO and a Jet database.

Your code would return the EmployeeID of the current record or
cause an error if it was an empty recordset.

I don't know whether .LastModified works with an ODBC connection.

Nov 12 '05 #4
Everyone,

Thanks. You have definately got me thinking on the right track. My
application exists as both native Access tables and sometimes ODBC
connection to PostgreSQL tables. Both I have to manage but the
PostgreSQL problem I have undercontrol and can manage.

Thank again

Phillip J. Allen

Phillip J. Allen wrote:
Hi all,

I have a table with an “autonumber” primary key field that also acts as
a foreign key in 2 other tables. I would like to programmatically add a
new record to the first table and the other 2 child tables. The reason
for doing this is to copy one complete record (parent and child table
records) into a new record so the user can make a few modifications.

So how do I determine what will be the next auto-generated number for my
primary key? I visualize doing this schematically like:

1. Open a Transaction
2. Get the next autonumber value
3. Add a new record to my first table (have Access generate the primary
key)
4. Add a new record to my 2 child tables using the “next autonumber
value” as the foreign keys
5. End Transaction
I have always resisted using auto-numbered primary keys in this type of
instance for this very reason. But in this situation I almost had to.
This database was an “observation database” and it’s a bit difficult to
ask people to manually keep track of how many observations they take.

Thanks,

Phillip J. Allen
e-mail: pa*****@attglobal.net


Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
4
by: Bart Plessers \(artabel\) | last post by:
Hello, I have an asp script that lists the files in a directory: CurrentPATH = "c:\temp\" Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set...
3
by: Ernst | last post by:
I have a script for a menu. However, this menu uses absolute coordinates. This menu had to be placed on a website. This website is position (centered) using a table. How can I determine/calculate...
0
by: KathyB | last post by:
Hi, sorry if this is the wrong group...but I don't see an xpath group and hoping someone here can help me. How do you assign variable to different elements within a single node set? I have a...
4
by: lcifers | last post by:
Is there a way, through VB.NET, to determine if the user has selected this option? I am writing an application that does some string functions to rename files, and the file names get chopped up if...
6
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future,...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
I have code to bold text in a datagridviewcell: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim cs As DataGridViewCellStyle, fnt...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
1
nev
by: nev | last post by:
Good day Sirs & Madams, I know some of you will find this problem very simple. Kindly share a little knowledge to me on how to deal with this. Thank you. I have a parent-child table relation. when...
15
dlite922
by: dlite922 | last post by:
I'm back again, Intro: I've got a floating div (outerDIV) with fixed width that contains an image (IMG) and a div that contains a short text (innerDIV) Problem: In FF, the innerDIV is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.