473,598 Members | 2,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 programmaticall y 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*****@attglob al.net

Nov 12 '05 #1
4 6447
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.createOb ject("ADODB.con nection")
conn.Mode = adModeReadWrite
conn.Timeout = 20
conn.open "DSN","user","p ass"

'create a recordset
set rs = Server.CreateOb ject("ADODB.rec ordset")
rs.ActiveConnec tion = conn
rs.CursorLocati on = adUseServer
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimisti c
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*****@attglo bal.net> wrote in message
news:3f******** @news1.prserv.n et...
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 programmaticall y 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.supern ews.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 programmaticall y 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*****@attglob al.net


Nov 12 '05 #5

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

Similar topics

17
6130
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
4
11843
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 oFolderContents = oFolder.Files For Each oFileItem in oFolder.Files .....
3
1460
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 where to fix my menuregardless the browser, slidebar width, tool etc. Thanks Ernst
0
1130
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 simple xml structure that I'm using to determine a workflow process. I've read all the xpath documentation, but can't quite get my brain around the precise methods I need. When a user enters a serial number (sn), I need to find the last board with...
4
9709
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 the expected extension is not returned. I can write another function to get around this, but if there is a clean way to determine the state of that toggle I would rather do that. I may need this again in the future and don't want to always...
6
14335
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, excluding holidays and weekends.' I didn't want to build a table of holidays that would have to be continuously updated, so I searched high and low for a function that would tell me whether a given date was a holiday, to no avail. I did find an...
2
7742
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 As Font For Each c As DataGridViewCell In dgrv1.SelectedCells cs = c.Style Console.WriteLine(c.Style.Font) fnt = dgrv1.Font Console.WriteLine(fnt.Name)
9
2673
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 T. Smith" "Fred Barzowsky" "Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc. etc. I am wondering what the efficient way to do this in code might be. The dumb and brute-force way would be to loop through the content...
1
1098
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 bsParent is navigated, bsChild follows. In code, I did this to determine the rowstate: dim dtParent as datatable = ds.Parent.copy for i as integer to dtParent.defaultview.count - 1 if dtParent.defaultview(i).row.rowstate = datarowstate.modified...
15
16591
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 displaying under the IMG because outerDIV is not wide enough to contain both of them (in some instances). In the instances where the outerDIV is wide, I don't have this problem. IE wraps the text but still keeps it to the right of the IMG. FF doesn't...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.