473,327 Members | 1,976 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,327 software developers and data experts.

Get primary key value from append query

Seth Schrock
2,965 Expert 2GB
My database is linked through an ODBC driver to a SQL Server backend. I know that when using the recordset.addnew method, to get the auto numbered primary key value for the record just entered you have to set the bookmark to the last modified record and then go to that record and get the value from the primary key field. Due to having to encrypt a few fields, I have to use an Append query to insert the data (the query will encrypt it). Can I still use the method of bookmarking the last modified record to get the primary key value?
May 30 '13 #1
10 2364
Rabbit
12,516 Expert Mod 8TB
Since it's SQL Server, you can try a pass through query using this SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT @@IDENTITY
I don't know if it will work in Access but in SSMS it returns the most recently assigned primary key.
May 30 '13 #2
zmbd
5,501 Expert Mod 4TB
Make sure it's a "pass thru" query as you've done with the others.
Jun 2 '13 #3
Seth Schrock
2,965 Expert 2GB
My apologies for getting back so late. I was gone over the weekend.

I will give that a try tomorrow. It does sound promising.
Jun 2 '13 #4
Seth Schrock
2,965 Expert 2GB
Well, I tried it and nothing is returned. I read the following on the MSDN website:
"The scope of the @@IDENTITY function is current session on the local server on which it is executed. This function cannot be applied to remote or linked servers. To obtain an identity value on a different server, execute a stored procedure on that remote or linked server and have that stored procedure (which is executing in the context of the remote or linked server) gather the identity value and return it to the calling connection on the local server."
Does this mean that running it as a pass-through query won't work? I would have thought that since both my INSERT query and your @@IDENTITY query were pass-through queries that they both would have happened on the same server (not a remote server) and would thus work.
Jun 3 '13 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Are you maintaining and re-using the same connection between the append query and the query for the identity?
Jun 3 '13 #6
Seth Schrock
2,965 Expert 2GB
I'm running two separate querydefs. Is that two separate connections? Is it possible to combine the two queries into one querydef so that it would be the same connection (separating the queries using the word GO)?
Jun 3 '13 #7
Seth Schrock
2,965 Expert 2GB
Would it just be better to do a Recordset.AddNew for all of the fields that aren't encrypted, get the PK field, and then run an update query to add the encrypted fields to the record I just created? This just seems like a bad idea and a waste of system resources, but I can't figure anything else out.
Jun 4 '13 #8
Seth Schrock
2,965 Expert 2GB
Well, I went ahead and just used the Recordset.AddNew idea to create the record, get the value of the primary key field and then I do an update query to encrypt the last two fields. This works, but I would still like to be able to do this all in one step. At least I can move on for now and continue testing.
Jun 4 '13 #9
Rabbit
12,516 Expert Mod 8TB
If you want to try it in one step, you could try using a stored procedure to do your insert and have it return the @@identity.
Jun 4 '13 #10
Seth Schrock
2,965 Expert 2GB
I'll have to look into how to do that. If I need help I'll post in the MS SQL Server forum. Thanks Rabbit.
Jun 4 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi - I am trying to perform a simple append query, with no luck. I have a table (MktPrices) that has the following fields: BondID, PriceDate, Price. The objective is to allow the user to input a...
9
by: JMCN | last post by:
hi- i have inherited an access 97 database that keeps track of the loans. i have been running into referential intergrity problems when i try to append new loans to table. first of all is a...
2
by: JMCN | last post by:
hi i have a general question regarding append queries in access 97. each week i need to update my table(tblonlinereg) with new or modified records. firstly, i import the text file into my...
1
by: jpr | last post by:
Friends, I would like some help with a code that allows me to run an append query only if a specific field is not already stored into another table. Example. I add a new customer to my database...
1
by: James Hallam | last post by:
I have a form with a subform. When there are no entries in the subform, I have an append query which makes a default entry in the subform (for what I am doing there needs to be at least one value...
7
by: pltmcs | last post by:
I am trying to generate some records from one table into another. The problem is that the new table has an autonumber field (PID). The PID is part of the primary key since it is possible to have...
1
by: hr833 | last post by:
hi.. i'm using a append query to filter some new records that must be updated in the lookup table. In this lookup table it consist of the part number and the type of the product. the part number...
10
by: MeeMee | last post by:
Hi I have a problem appending data into an oracle table from access. I imported the new data from an excel sheet into a table in access and useed an append query to add the data into a linked...
10
by: Rob Amor | last post by:
I am trying to move records from one table to another using an append query. Neither table is connected via any relationship. All fields being appended have matching data types. No field is set to...
4
by: Adam Tippelt | last post by:
Situation: When a user 'confirms' that they want to save the information they've inputted, I need to append the database records from a temporary table to a different table. Problem: The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.