Solved

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

Posted on 2011-02-11
17
513 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:rmitwiz
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 2

Expert Comment

by:PAXSoftware
ID: 34875128
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
 
LVL 1

Author Comment

by:rmitwiz
ID: 34875229
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
 
LVL 2

Expert Comment

by:PAXSoftware
ID: 34875530
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:rmitwiz
ID: 34875687
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
 
LVL 1

Author Comment

by:rmitwiz
ID: 34875733
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
 
LVL 29

Expert Comment

by:Badotz
ID: 34878027
After you insert into your main table, query it for the @@identity value (the value of the last-inserted id).
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34878236
what is the relevance of sql server (2008) to this?
0
 
LVL 1

Author Comment

by:rmitwiz
ID: 34889929
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
 
LVL 29

Expert Comment

by:Badotz
ID: 34890014
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
 
LVL 1

Author Comment

by:rmitwiz
ID: 34890110
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
 
LVL 29

Expert Comment

by:Badotz
ID: 34890138
Did you actually run the SQL against the DB?
0
 
LVL 1

Author Comment

by:rmitwiz
ID: 34890254
I just added it to the vb.net code like mentioned above.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 34890525
That won't do it, I'm afraid - you have to actually execute the SQL agains the DB.
0
 
LVL 1

Author Comment

by:rmitwiz
ID: 34892083
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
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 34892687
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
 
LVL 1

Author Closing Comment

by:rmitwiz
ID: 35441847
Thanks, not exactly copy and paste like I'll like though
0
 
LVL 29

Expert Comment

by:Badotz
ID: 35442520
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question