|
-
Mar 28th, 2007, 09:46 AM
#1
Thread Starter
Lively Member
[RESOLVED] Primary Key Auto-increment
Hello
I am trying to insert records in a table using recordset (ADO).
Does anyone have handy codes that will cause my new record's primary key to auto-increment? That would help me loads.
Thanks
-
Mar 28th, 2007, 09:51 AM
#2
Re: Primary Key Auto-increment
In the table design set the data type of the field to AutoNumber (or Identity), and simply "ignore" that field when adding a record.
-
Mar 28th, 2007, 10:19 AM
#3
Thread Starter
Lively Member
Re: Primary Key Auto-increment
Great if it works like this. Thanks
-
Mar 28th, 2007, 10:46 AM
#4
Re: Primary Key Auto-increment
Just keep in mind that if you delete a record, you won't get the deleted record id back.
-
Mar 29th, 2007, 03:43 AM
#5
Thread Starter
Lively Member
Re: Primary Key Auto-increment
I have another problem now..
I now have to save that primary key (which you tell me will automatically be generated by the db) as a secondary key in another table.
(Using recordset in vba)
How to get this value? How to go about this?
Do I have to keep my first recordset open and go to last record?
-
Mar 29th, 2007, 05:50 AM
#6
Addicted Member
Re: Primary Key Auto-increment
Most versions of SQL, allow you to Select @@Identity straight after the Insert, which will tell you the value of the last insert.
If using SQL Server, you can also use IDENT_CURRENT, AND IDENT_SCOPE. IDENT_SCOPE is good if multiple people could insert records into your table.
HTH
JP
Please rate the postings 
-
Mar 29th, 2007, 06:50 AM
#7
Re: Primary Key Auto-increment
IDENT_SCOPE? Or did you mean SCOPE_IDENTITY?
-tg
-
Mar 29th, 2007, 07:30 AM
#8
Thread Starter
Lively Member
Re: Primary Key Auto-increment
Using Ms Access folks..
I was wondering if, since relationships have been created for the secondary keys, wouldnt it automatically fill them in if i created a record in the table to which it is related?
Illustrating this:
tblProject and tblTeam are related: ProjectID is found in tblTeam and related to the primary key (ProjectID) of tblProject
(Normally, my ProjectID will be autom. incremented coz it's an Autonumber field - see Post #2)
Now, if I created a record in tblTeam and did not specify anything for field ProjectID, wouldnt it be automatically be filled in by the db? (since it will detect the relationship)
i don't know...........
-
Mar 29th, 2007, 07:57 AM
#9
Re: Primary Key Auto-increment
No it will not just automaticly fill in for you. You must explictly post the ProjectID into the Team table. The DB can't just assume that what Project the team will be working on. If you don't supply it the fields will be left blank (if allowing nulls is set for the field in the DB).
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 29th, 2007, 08:50 AM
#10
Thread Starter
Lively Member
Re: Primary Key Auto-increment
 Originally Posted by GaryMazzone
No it will not just automaticly fill in for you. You must explictly post the ProjectID into the Team table. The DB can't just assume that what Project the team will be working on. If you don't supply it the fields will be left blank (if allowing nulls is set for the field in the DB).
Plz, then how should I go about that?
-
Mar 29th, 2007, 08:59 AM
#11
Re: Primary Key Auto-increment
There are many ways to do what you want to do. First question is How do you reate the two Items (Project and Team) are they created together or seperatly? Do you have a table of personnel that you draw on to make teams?
If you make the Project first (which I would assume you have to too allow the project to be a Foregin Key into the Teams table) Save the Project. Requery the Database and get back that last inserted ProjectID (the project you just added) then use that to place into the Teams table.
I saw from the posting above that you use recordset add functionallity that I never have used. I always write the SQL statement my self and perform an Execute on the connection object (strictly ADO code only).
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 29th, 2007, 12:12 PM
#12
Re: Primary Key Auto-increment
Same here.. but you may be able to read the value from the recordset immediately after the add.
If not, open a recordset with this SQL statement:
..this will return a single field containing the last auto-generated value (for ProjectID in this case).
-
Mar 30th, 2007, 03:14 AM
#13
Thread Starter
Lively Member
Re: Primary Key Auto-increment
'SAVE TO TBLTEAMBUILDER
Dim cnt as Long
'cnt = size of array StaffList
Set MyRSTeamBuilder = New ADODB.Recordset
' open the recordset.
MyRSTeam.Open "tblTeamBuilder", conn, adOpenDynamic, adLockOptimistic
With MyRSTeamBuilder
' For each staff in array StaffList
.AddNew
' set the value for the fields
'.Fields("ProjectID").Value = get value from last record in tblProject
'.Fields("StaffCode").Value = StaffList (x)
'.Fields("TeamID") = get value from last record in tblTeam
.Update
' Move Next until end of StaffList
End With
MyRSTeamBuilder.Close
Set MyRSTeamBuilder = Nothing
Set conn = Nothing
Thanks a lot for all your helpful posts. If possible would u ppl be so kind to help me write my codes correctly with all the new syntax u suggested to me.
-
Mar 30th, 2007, 10:26 AM
#14
Re: Primary Key Auto-increment
Reading from the recordset is simple enough, so you can probably work that out yourself.. so I'll explain how to use the SQL I posted. You need to run it as soon as possible after adding the record, and I would recommend storing it to a variable, eg:
Code:
Dim RSTemp as ADODB.Recordset
Dim lngProjectID as Long
Set RSTemp = New ADODB.Recordset
RSTemp.Open "Select @@Identity", conn, adOpenForwardOnly, adLockReadOnly
lngProjectID = RSTemp.Fields(0).Value
RSTemp.Close
Set RSTemp = Nothing
'lngProjectID now contains the ID
-
Apr 2nd, 2007, 09:33 AM
#15
Thread Starter
Lively Member
Re: Primary Key Auto-increment
Hi
thanks it's working.. It's great for ppl caring to help others in this forum.
Cheers to them.
-
Apr 2nd, 2007, 09:43 AM
#16
Re: Primary Key Auto-increment
If you make the Project first (which I would assume you have to too allow the project to be a Foregin Key into the Teams table) Save the Project. Requery the Database and get back that last inserted ProjectID (the project you just added) then use that to place into the Teams table.
I see you've gone with @@identity which is the right thing to do but I just wanted to warn against the above technique. It's fine in a single user enviroment but you should be very careful using it in a multi user enviroment. If your inserts interleave with another users inserts it can pick up the wrong project and assign your team to the other users project in error.
-
Apr 2nd, 2007, 11:02 AM
#17
Re: Primary Key Auto-increment
I think it's actually ok, as MintFlavor is using Access.
I can't remember who told me (perhaps TG?), but apparently Access treats @@identity in the same way that SQL Server treats @@scope_identity
-
Apr 3rd, 2007, 03:56 AM
#18
Thread Starter
Lively Member
Re: Primary Key Auto-increment
Just to clear a doubt: Can't an applic created with ms access be deployed for a multi-user access? I guess it can right?
I assume the program proceeds by opening a table exclusively when a user adds in a new record. hence @@identity should work correctly isnt it?
-
Apr 3rd, 2007, 04:43 AM
#19
Re: Primary Key Auto-increment
 Originally Posted by MintFlavor
Just to clear a doubt: Can't an applic created with ms access be deployed for a multi-user access? I guess it can right?
It can indeed.. but you don't want to be using Access if you are expecting lots of users (generally if you have more than 5 users, you should use a different database system).
I assume the program proceeds by opening a table exclusively when a user adds in a new record. hence @@identity should work correctly isnt it?
Not really.. the program should not open the table exclusively, it should only block other users from editing exactly the same record (and only if you have specified that).
I don't know the details of how @@identity works in Access, but I presume there is a separate copy for each user, rather than one copy for the whole database.
-
Apr 5th, 2007, 03:15 PM
#20
Addicted Member
Re: [RESOLVED] Primary Key Auto-increment
MyRSTeam.Open "tblTeamBuilder", conn, adOpenDynamic, adLockOptimistic
If you replace the adOpenDynamic with adOpenKeyset, you can get the value from the identity field immediately after the Update statement, by just referring to it.
Code:
eg Identity = MyRsTeam..Fields("ProjectID").Value
HTH
JP
Please rate the postings 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|