473,763 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding an autonumber which has just been created to a different table

Hi, I have a form which calculates a final cost (frmWorkCosts, data stored
in tblWorkCosts) and once calculated adds this final cost (Which isn't saved
in tblWorkCosts) into a different table (tblLabour) as a new record. Once
it has done this I want it to save the Labour ID (Key Field/Autonumber in
tblLabour) to the Labour ID field in the original calculation table
tblWorkCosts, creating a link between the two tables.
I have tried a variety of methods such as DLast and Last modified but
without success.
At the moment my code looks like this:

Function AddCalculatedLa bourCost(Labour Fun As Variant, Costratehourskg
As Variant)
Dim db As Database
Dim rs As Recordset
Dim WSPrefix, LabourDesc As String
Dim Resp As Variant

Set db = DBEngine.Worksp aces(0).Databas es(0)
Set rs = db.OpenRecordse t("tblLabour" )

rs.AddNew
rs![WSPrefix] = WSPrefix
rs![Labour] = Costratehourskg
rs![RecipeLabourGro up] = LabourFun
rs![Description] = LabourDesc
rs.Update
rs.Close

'Set db = DBEngine.Worksp aces(0).Databas es(0) 'this is my effort to try
and add the autonumber of the adding process above
'Set rs = db.OpenRecordse t("tblWorkCosts ")
'rs.Edit
'rs![Labour ID] = LabourID
'rs.Update

Resp = MsgBox("The labour Cost is now saved", vbOKOnly, "New Labour
Cost")

DoCmd.Close

End Function

Any help would be greatly appreciated

Many thanks

Will

Nov 13 '05 #1
1 1598
If this data is in Access tables, the new autonumber is assigned as soon as
you AddNew, so you can read it then:

Function AddCalculatedLa bourCost(Labour Fun As Variant, _
Costratehourskg As Variant) As Long
Dim db As Database
Dim rs As Recordset
Dim WSPrefix, LabourDesc As String
Dim Resp As Variant
Dim lngNewID As Long

Set db = DBEngine.Worksp aces(0).Databas es(0)
Set rs = db.OpenRecordse t("tblLabour" )

rs.AddNew
'Store the new ID value into our variable.
lngNewID = rs![NameOfYourPrima ryKeyFieldHere]

rs![WSPrefix] = WSPrefix
rs![Labour] = Costratehourskg
rs![RecipeLabourGro up] = LabourFun
rs![Description] = LabourDesc
rs.Update
rs.Close

Resp = MsgBox("The labour Cost is now saved as " & lngNewID, _
vbOKOnly, "New Labour Cost")

'Return the new ID value
AddCalculatedLa bourCost = lngNewID

'Clean up.
Set rs = Nothing
Set db = Nothing
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Will" <wi************ *****@f2s.com> wrote in message
news:cj******** **@news.freedom 2surf.net...
Hi, I have a form which calculates a final cost (frmWorkCosts, data stored
in tblWorkCosts) and once calculated adds this final cost (Which isn't
saved
in tblWorkCosts) into a different table (tblLabour) as a new record. Once
it has done this I want it to save the Labour ID (Key Field/Autonumber in
tblLabour) to the Labour ID field in the original calculation table
tblWorkCosts, creating a link between the two tables.
I have tried a variety of methods such as DLast and Last modified but
without success.
At the moment my code looks like this:

Function AddCalculatedLa bourCost(Labour Fun As Variant, Costratehourskg
As Variant)
Dim db As Database
Dim rs As Recordset
Dim WSPrefix, LabourDesc As String
Dim Resp As Variant

Set db = DBEngine.Worksp aces(0).Databas es(0)
Set rs = db.OpenRecordse t("tblLabour" )

rs.AddNew
rs![WSPrefix] = WSPrefix
rs![Labour] = Costratehourskg
rs![RecipeLabourGro up] = LabourFun
rs![Description] = LabourDesc
rs.Update
rs.Close

'Set db = DBEngine.Worksp aces(0).Databas es(0) 'this is my effort to try
and add the autonumber of the adding process above
'Set rs = db.OpenRecordse t("tblWorkCosts ")
'rs.Edit
'rs![Labour ID] = LabourID
'rs.Update

Resp = MsgBox("The labour Cost is now saved", vbOKOnly, "New Labour
Cost")

DoCmd.Close

End Function

Nov 13 '05 #2

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

Similar topics

3
4773
by: Ilan Sebba | last post by:
I have a 'supertype' table with only one field: autonumber. Call this table the 'parent' table. There are two subtypes, 'androids' and 'martians'. Martian have only one thing in common: they give birth to identical mules. So each android and a martian have primary key which is a foreign key to the parent table. Now, I want to insert a new record in 'android' or 'martian'. This can be done easily using MS-Access forms. But I want to do...
5
3751
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for Office 2000. I am creating a database to track student athletes. I have created the following tables. The table title is to the far left, with fields under each. The common field will be the StudentID field, which is their student number assigned...
33
4307
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and setting default value to a UDF that manages the auto-numbering. Access won't take a UDF as a default value. Okay, I'll use SQL WITHOUT any aggregate functions, for the default value. Access won't do that either. Okay, I create a second...
6
2506
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new products). Tables: tblCategoryDetails CategoryID SpecID
3
1706
by: Megan | last post by:
hello everybody- i'm normalizing a database i inherited. i'm breaking up a huge table named case into several smaller tables. i am creating several many to many relationships between the new case table and the other newly created tables. however, i have run into a problem when trying to create a many to many relationship between 2 of my tables.
35
7277
by: Traci | last post by:
If I have a table with an autonumber primary key and 100 records and I delete the last 50 records, the next record added would have a primary key of 101. Is there any way to have the primary key start at 51 after the last 50 records are deleted? Thanks! Traci
26
3821
by: jimfortune | last post by:
Sometimes I use Autonumber fields for ID fields. Furthermore, sometimes I use those same fields in orderdetail type tables. So it's important in that case that once an autonumber key value is assigned to a record that it doesn't change. Occasionally I find that due to corruption or an accidental deletion and restore of a record from a backup the autonumber field needs to be tidied up. So when I create (through AddNew) the autonumber...
1
4293
by: travismorien | last post by:
I have four tables of different "entities". One table contains information for "people", one for "trusts", one for "companies" and one for "self managed super funds". Each type of entity has an autonumber ID, "Person ID" "Trust ID" "Company ID" and "SMSF ID" A "portfolio" table holds information about what shares, funds and properties everyone owns. But because its organised by "PersonID" it currently only can hold information for...
11
4497
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.