473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

How can I do this please

Hi,
Please can anyone tell me how to achieve multiple inserts using the key
value from a newly inserted record of one table into several other tables.

I have a company table, an address table, a contact table.

when I insert into the company table, I also want to insert the new id
into the address and contact tables under companyID field.

The primary key in the company table is auto number, so the new key is
not known until it is inserted.

is it possible to retrieve the new key as a resultset of the insert
statement so I can then use this to insert into the other tables?

Any help appreciated
Yeric
Aug 11 '06 #1
6 4270
eric wrote:
when I insert into the company table, I also want to insert the new id
into the address and contact tables under companyID field.

The primary key in the company table is auto number, so the new key is
not known until it is inserted.

is it possible to retrieve the new key as a resultset of the insert
statement so I can then use this to insert into the other tables?
Dim c As ADODB.Connection
Set c = New ADODB.Connection
With c
..ConnectionString = CurrentProject.BaseConnectionString
..Open
..Execute "INSERT INTO [Order Details] " _
& "(OrderID, ProductID, UnitPrice, Quantity, Discount) " _
& "Values (12, 23, 15.5, 12, 3/1000)"
Debug.Print .Execute("SELECT @@Identity").Collect(0)
' prints the autonumber of the newly inserted record
..Close
End With
Set c = Nothing
End Sub

Aug 11 '06 #2
Dim c As ADODB.Connection
Set c = New ADODB.Connection
With c
.ConnectionString = CurrentProject.BaseConnectionString
.Open
.Execute "INSERT INTO [Order Details] " _
& "(OrderID, ProductID, UnitPrice, Quantity, Discount) " _
& "Values (12, 23, 15.5, 12, 3/1000)"
Debug.Print .Execute("SELECT @@Identity").Collect(0)
' prints the autonumber of the newly inserted record
.Close
End With
Set c = Nothing
End Sub
Sorry should have mentioned that I am only using access and vba not vb6,
there is no connection string it opens, and if I try and create a new
connection string I get an error, I can do most of what you said with
the docmd.runsql except the SELECT @@IDENTITY I am unable to assign this
to a variable.

I have tried intNewID = docmd.runsql("SELECT @@Identity) but I get an
error expected function or variable error

Thanks for help so far
Eric
Aug 11 '06 #3
eric wrote:
Dim c As ADODB.Connection
Set c = New ADODB.Connection
With c
.ConnectionString = CurrentProject.BaseConnectionString
.Open
.Execute "INSERT INTO [Order Details] " _
& "(OrderID, ProductID, UnitPrice, Quantity, Discount) " _
& "Values (12, 23, 15.5, 12, 3/1000)"
Debug.Print .Execute("SELECT @@Identity").Collect(0)
' prints the autonumber of the newly inserted record
.Close
End With
Set c = Nothing
End Sub

Sorry should have mentioned that I am only using access and vba not vb6,
there is no connection string it opens, and if I try and create a new
connection string I get an error, I can do most of what you said with
the docmd.runsql except the SELECT @@IDENTITY I am unable to assign this
to a variable.

I have tried intNewID = docmd.runsql("SELECT @@Identity) but I get an
error expected function or variable error
This code should run in any version of Access from 2000 on. It is NOT
VB6 code.

Aug 11 '06 #4
On Fri, 11 Aug 2006 13:06:35 +0100, eric <er**@NoSpamForMe.comwrote:

You did set a reference to ADO, right?
-Tom.

>Dim c As ADODB.Connection
Set c = New ADODB.Connection
With c
.ConnectionString = CurrentProject.BaseConnectionString
.Open
.Execute "INSERT INTO [Order Details] " _
& "(OrderID, ProductID, UnitPrice, Quantity, Discount) " _
& "Values (12, 23, 15.5, 12, 3/1000)"
Debug.Print .Execute("SELECT @@Identity").Collect(0)
' prints the autonumber of the newly inserted record
.Close
End With
Set c = Nothing
End Sub

Sorry should have mentioned that I am only using access and vba not vb6,
there is no connection string it opens, and if I try and create a new
connection string I get an error, I can do most of what you said with
the docmd.runsql except the SELECT @@IDENTITY I am unable to assign this
to a variable.

I have tried intNewID = docmd.runsql("SELECT @@Identity) but I get an
error expected function or variable error

Thanks for help so far
Eric
Aug 11 '06 #5
You did set a reference to ADO, right?
-Tom.
Where would I set this reference?
sorry but I have very little experience with VB or VBA and databases, I
have been trying to read a ton of books to get upto speed, wish I could
speed read

The error I get is

-2147467259 The database has been placed in a state by user 'Admin' on
machine 'XYZ' that prevents it from being opened or locked.

Using Access 2003

I get this error when I the code hits .open

I checked properties and default open mode is set to Shared and default
record locking is set to no locks

and the open database using record level locking option is ticked

Any ideas?
Yeric
Aug 15 '06 #6

Try this in the After Update event of your main form
Dim db As DAO.Database
Dim rst As DAO.Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("NameofAddresstable")
rst.AddNew
rst!CompanyID = Forms!NameofMainForm!CompanyID
rst.Update

Set rst = db.OpenRecordset("Nameofcontactstable")
rst.AddNew
rst!CompanyID = Forms!NameofMainForm!CompanyID
rst.Update

Set rst = Nothing
Set db = Nothing

Aug 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sam | last post by:
perl -MCPAN -e 'install Time::HiRes' CPAN: Storable loaded ok Going to read /usr/local/cpan/Metadata Database was generated on Mon, 13 Oct 2003 09:46:36 GMT Running install for module Time::HiRes...
2
by: KK | last post by:
Hi Peoples, Can anyone give me any learning materials for learning C++. i.e. can you give me any docs or give me the link to any website that has info on programming in general or C++. (ok...
1
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my...
10
by: Nathaniel Branden | last post by:
Hello. This isn't really an Access question. I just want to know whether Tony Toews is retarded. In that vain, I have pasted down his last thirty or so posts for someone out there to respond. ...
3
by: Mark Broadbent | last post by:
could somebody please recommend a good book to prepare for exam Exam 70-320*: Developing XML Web Services and Server Components with Microsoft Visual C# and the Microsoft .NET Framework . One that...
5
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. Here is a .php script. Right now, the minimum amount of time (i.e. duration) allowed is 1 month. However, I want to be able to reduce the minimum amount of time to...
31
by: Simply_Red | last post by:
i'm sorry i posted this in other groupes, and i didn't see it, and as this group is most actif, i repost it here, and sorry for mutliposting: Hi, i'm using VC6, i have this declaration: ...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
2
by: Unpopular | last post by:
void directory::modification()//??????????? { clrscr(); cout<< "\n\t @@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@ @ @ @@@@@@ "; cout<< "\n\t=====@ @ @ @ @ @ @@...
4
by: ssniit | last post by:
Please can anyone help me with d programs in C for treat the matter most urgent please please please please .................................! CPU scheduling: WAP to show FCFS scheduling...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.