Solved

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

Posted on 2011-02-11
17
445 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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now