Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-11
17
Medium Priority
?
658 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

885 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