How to get autonumber in to match number field in related tables in Access?

I am inserting data from a VB.Net web application form into an Access Database. My primary table has an ID field which is an autonumber as well. All my other tables also have an ID field. The ID field in the other tables are set to number. I have all the ID fields in each table of the database relating to the ID field in the primary table. The ID field in the primary table is also the primary key. The problem is when data is inserted into the fields in each table from the web application and a new row is created in each table, only the primary table with the autonumber ID has a new number. I would like for the autonumber that's created in the primary table to assign itself to the ID field of the other tables as well, as new rows are created. Currently the other tables create a new row with a blank ID field.
LVL 1
rmitwizAsked:
Who is Participating?
 
BadotzConnect With a Mentor Commented:
This is as clear as I can make it:

1. The IDENTITY query must be a separate query. It will return the last-inserted PK for the table specified.
2. After the INSERT into your MAIN table, before you do anything else, you must run the IDENTITY query to retrieve the PK.
3. The remaining CRUD can be run against the DB with the shiny new PK from your MAIN table.

If nothing happened, then one of the above was omitted or not performed properly.

OR...

you might try osomething like this:

Assume "my_rs" is the recordset for the main insert. After the fields are populated, a new row is added to the table:

    my_rs.insert

Immediately following the insert command, see if this works:

    new_pk = my_rs.Fields("@@identity").Value

I have no means to est this right now, sorry.
0
 
PAXSoftwareCommented:
Have you created a "Relationship" between the tables?  Under "Database Tools" you can choose "Relationships" and then link tables so that the Primary ID from one table is the Secondary ID on another table.  You can also define that you want updates and deletes to be cascaded from one table to another.
0
 
rmitwizAuthor Commented:
Yes I have created relationships. I related the ID of the primary table to the id in each secondary table. I just tried to slect the cascade options as well and it still inserted new rows in each table and left the ID column blank in all tables except for the primary table because it has the autonumber.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
PAXSoftwareCommented:
hhmmm....

I assume this is being done in code (as opposed to someone just cutting and pasting).  So you are looping through the records in the main source table(s) and adding information into your Access tables accordingly.

Something like:

dim IDMainDestination as Long

Do While  rcdSource.EOF = false
    rcdMainDestination.Addnew

    rcdMainDestination.Fields("Field2") = rcdSource.Fields("Field2")
    etc.

    IDMainDestination = rcdMainDestination.Fields("PrimaryIDField")
    rcdMainDestination.Update

    rcdSecondDestination.Addnew
    rcdSecondDestination.Fields("ID") = IDMainDestination
    rcdSecnodDestination.Fields("Field3") = rcdSource.Fields("Field3")
    rcdSecondDestination.Update

    rcdSource.MoveNext
Loop

You have to grab the value of the Autonumber field in your main table BEFORE it is actually saved to the database.

If you are just doing this by cutting and pasting, I am not sure how or if it is possible.
0
 
rmitwizAuthor Commented:
No, its not done by looping I am using the insert string codes in vb.net

Example:

Dim str
str = "insert into entranceform ([Bureau], [Division], [Firstname], [Lastname], [Phone]) values('" & ddlBureau.Text & "','" & ddlDivision.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "' ,'" & CInt(txtPhone.Text) & "')"
0
 
rmitwizAuthor Commented:
I meant

Dim str As String
str = "insert into entranceform ([Bureau], [Division], [Firstname], [Lastname], [Phone]) values('" & ddlBureau.Text & "','" & ddlDivision.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "' ,'" & CInt(txtPhone.Text) & "')"
0
 
BadotzCommented:
After you insert into your main table, query it for the @@identity value (the value of the last-inserted id).
0
 
LowfatspreadCommented:
what is the relevance of sql server (2008) to this?
0
 
rmitwizAuthor Commented:
How is the @@identity value querying used? Where do I put it at in the VB.Net code or where do I put it within Access 2007?


SQL server (2008) is relevant because it also deals with working with databases such as what I'm doing, and I may even transfer this to SQL Server 2008 once I get it working in Access.
0
 
BadotzCommented:
Create a query:

select @@identity from YOUR_TABLE_NAME_HERE;

and run it after you insert into your main table. It will return the last-inserted primary key for the table you specify.

Your primary keys are named "id", right? And they are AutoNumber or Long datatypes, right?
0
 
rmitwizAuthor Commented:
In my vb.net code after my insert statment I put the codes:

Dim selectform As String
selectform = "select @@identity from entranceform"

No error was returned but when I checked the database the other tables still placed null in the id column rather than the autonumber generated from the primary table.
0
 
BadotzCommented:
Did you actually run the SQL against the DB?
0
 
rmitwizAuthor Commented:
I just added it to the vb.net code like mentioned above.
0
 
BadotzCommented:
That won't do it, I'm afraid - you have to actually execute the SQL agains the DB.
0
 
rmitwizAuthor Commented:
So are you saying I should put the sql code: select @@identity from entranceform
in the sql of the access database. query? If so, I did that and nothing happened.
0
 
rmitwizAuthor Commented:
Thanks, not exactly copy and paste like I'll like though
0
 
BadotzCommented:
Sometimes learning is not easy; I'm sorry you were disppointed.

Perhaps next time you will state exactly what your expectations are; that way we will all know how to respond.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.