Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Community Discussions (All other subjects)
 Access2000 query using @@Identity
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 06 January 2004 :  11:44:47  Show Profile  Visit D3mon's Homepage
I'm trying to run this query in Access 2000:

INSERT INTO tblBasket ( CardTypeID ) VALUES ( 5 );
SELECT @@Identity;

but I'm fairly sure there can only be one 'statement' per query?


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"

CarKnee
Junior Member

USA
297 Posts

Posted - 06 January 2004 :  12:07:06  Show Profile  Visit CarKnee's Homepage
Try something like this:


Set insertCmd = Server.CreateObject("ADODB.Command")
insertCmd.ActiveConnection = strConnString

insertCmd.CommandText = strSQL
insertCmd.Execute

' Get the new ID number
Set adoRS = insertCmd.ActiveConnection.Execute("select @@identity")
intID = adoRS(0)
Set adoRS = Nothing

insertCmd.ActiveConnection.Close
Set insertCmd = Nothing


Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 06 January 2004 :  13:26:55  Show Profile  Visit HuwR's Homepage
as long as you do not close your connection, you can issue the two commands seperately, it will still return the correct identity.
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 06 January 2004 :  14:36:58  Show Profile  Visit D3mon's Homepage
Thanks guys, that confirms what I had suspected, but unfortunately I'm still having trouble getting back the correct value for the @@Identity. Oddly, it seems to return a zero value.

tblBasket has a Primary Key - BasketID, and it is an 'autonumber' field. Do I need to do anything else to configure the table or its contents? I had read that in Access, it still might might not be properly configured as an Identity field?

I'm probably trying to push the limits of Access a little too far as, essentially, I am trying to use Access Queries in the same way that I would have used SQL Stored Procedures. There don't seem to be many resources online for hand-writing Access Queries, so I guess I'll have to get hold of a good book.


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20584 Posts

Posted - 06 January 2004 :  16:09:49  Show Profile  Visit HuwR's Homepage
are you using the Jet drivers or the older ODBC drivers. If you aren't using jet it won't work.

you could always requery the table to find the id, for instance if you wanted the id of the reply you just posted, issue a query for max(reply_id) where member_id is you.

so for your basket, when you do the insert store an extra column which allows you to retreive the id
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 06 January 2004 :  16:48:31  Show Profile  Visit D3mon's Homepage
Oh yes, I'm using the Jet drivers. Yeah, I suppose an additional query to find the maximum value of BasketID might work, but I'm concerned about the possibility of another basketID having been added in the meantime and messing things up.
Know of any good books which might particularly focus on writing MS Access Queries? Not keen on buying an Access 'Doorstep' only to find 10 pages in 1000 giving instructions on just using the query wizards.


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 06 January 2004 :  18:20:00  Show Profile  Send ruirib a Yahoo! Message
The best book I know is a Sybex edition: Access 2000 Developer's Handbook, by Paul Litwin. There is a 2002 version as well. In fact there are two volumes for each Access version since Access 2000, but you probably need just Volume 1.

I've used SELECT @@IDENTITY with Access 2000 and never had a problem. I usually add an alias at the end, so that I can refer to the retrived field by name:


SELECT @@Identity As MyID


As I wrote, never had a problem using it. What Access version are you using? Have the latest MDACs?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 07 January 2004 :  04:36:06  Show Profile  Visit D3mon's Homepage
My version is Access 2000 (9.0.6926 SP3) and I assume I have the latest version of MDAC as all the Windows 2000 updates are in place. I seem to recall the installation of MDAC version 2.8?


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 07 January 2004 :  05:45:21  Show Profile  Send ruirib a Yahoo! Message
Then it should work. I see no reason not to work. What code are you using?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 07 January 2004 :  12:00:19  Show Profile  Visit D3mon's Homepage
Don't worry, I've used the re-querying max(primary key) method for now. The project is somewhat time-sensitive so I've not got long to play.


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 07 January 2004 :  14:20:46  Show Profile  Send ruirib a Yahoo! Message
It's not a matter of worry, it's just that I get puzzled when something that should work does not work. I know SELECT @@IDENTITY works, so if you want I can take a look at the code.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 07 January 2004 :  15:06:21  Show Profile  Visit D3mon's Homepage
Ah OK thanks.

I have a query (qryNewBasket) that adds data into a field in a table (tblBasket) to create a new row, as follows:
INSERT INTO tblBasket ( CardTypeID ) VALUES ( 5 );

I also have another query (qryBasketIdentity) which simply returns the @@Identity:
SELECT @@Identity AS BasketID;

My ASP code is essentially as follows:

Dim objDB, objRS, strSQL
Const adCmdStoredProc = 4
Const adExecuteNoRecords = 128

Set objDB = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.RecordSet")
objDB.Open strConnect 'strConnect defined elsewhere
strSQL = "qryNewBasket"
objDB.Execute(strSQL, ,adCmdStoredProc + adExecuteNoRecords )
strSQL = "qryBasketIdentity"
set objRS =  objDB.Execute(strSQL, ,adCmdStoredProc)
if not objRS.EOF then
    response.write objRS(0)
end if
objRS.Close
Set objRS = Nothing
objDB.Close
Set objDB = Nothing

The best I've been able to achieve is to get 0 (zero) displayed. The table has in the region of 15 rows, So I was expecting a higher number.


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"

Edited by - D3mon on 07 January 2004 15:09:01
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 07 January 2004 :  19:34:31  Show Profile  Send ruirib a Yahoo! Message
Try this

Dim objDB, objRS, strSQL, objCmd
Const adCmdStoredProc = 4
Const adOpenKeySet = 1
Const adExecuteNoRecords = 128
Const adLockOptimistic = 3

Set objDB = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.RecordSet")
Set objRS = Server.CreateObject("ADODB.Command")
objDB.Open strConnect 'strConnect defined elsewhere

Set objCmd.ActiveConnection = objDB
objCmd.CommandType = adCmdStoredProc
objCmd.Text = "qryNewBasket"

objCmd.Execute 

strSql = "qryBasketIdentity"
objRS.Open strSQL,objDB, adOpenKeySet, adLockOptimistic, adCmdStoredProc

if not objRS.EOF then
    response.write objRS(0)
end if
objRS.Close
Set objRS = Nothing
objDB.Close
Set objCmd = Nothing
Set objDB = Nothing



Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Steve D.
Average Member

USA
640 Posts

Posted - 09 January 2004 :  01:53:05  Show Profile  Visit Steve D.'s Homepage  Send Steve D. a Yahoo! Message
I found another article that showed a quick and easy way to do just that...

http://www.drdev.net/article06.htm

Swing Dancing Video Clips - It's All Swing! Forum
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.4 seconds. Powered By: Snitz Forums 2000 Version 3.4.07