Link to home
Start Free TrialLog in
Avatar of rmitwiz
rmitwizFlag for United States of America

asked on

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.
Avatar of PAXSoftware
Flag of Canada image

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.
Avatar of rmitwiz


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.

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.Fields("Field2") = rcdSource.Fields("Field2")

    IDMainDestination = rcdMainDestination.Fields("PrimaryIDField")

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


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.
Avatar of rmitwiz


No, its not done by looping I am using the insert string codes in


Dim str
str = "insert into entranceform ([Bureau], [Division], [Firstname], [Lastname], [Phone]) values('" & ddlBureau.Text & "','" & ddlDivision.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "' ,'" & CInt(txtPhone.Text) & "')"
Avatar of rmitwiz


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) & "')"
After you insert into your main table, query it for the @@identity value (the value of the last-inserted id).
Avatar of Lowfatspread
what is the relevance of sql server (2008) to this?
Avatar of rmitwiz


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.
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?
Avatar of rmitwiz


In my 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.
Did you actually run the SQL against the DB?
Avatar of rmitwiz


I just added it to the code like mentioned above.
That won't do it, I'm afraid - you have to actually execute the SQL agains the DB.
Avatar of rmitwiz


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.
Avatar of Badotz
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmitwiz


Thanks, not exactly copy and paste like I'll like though
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.