Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2006, 10:46 AM
Registered User
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Placing a Primary Key in Another Table When Adding

I have two tables - Hoods and Transactions. When an update is made to
the Hoods table, usually the Qty column, the update is written to the
Transaction table. Sometimes a new record is added and recorded in the
Transactions table. The tables are updated from entries the user makes
in textboxes on a form.

The Hoods table has the following columns:
HoodsID - the primary key and an auto number
Aisle
Sect
Box
Fabric
V_Color
Qty


The Transactions table has the following:
TransID
BoxID
OldQty
NewQty
Initials
Comment
Adjustments


The BoxID of the Transactions table should be set to the HoodsID of
the corresponding record in the Hoods table.


The first With block below will update or add a record to the Hoods
table based on the user selections from unbound textboxes. That part works. The second With block below will write the transaction to the Transactions table. It works fine when updating an
existing record. The Transactions table is on a subform with a parent
child relationship of HoodsID = BoxID. The problem is when adding a
new record. How would I capture the HoodsID from the record that was
just added and place it in the BoxID field of the Transactions record?
Obviously, the line of code "!BoxID = HoodsID" will not work for
adding a new record.


I hope I supplied all the relevant information without going overboard.
Thanks for any help.


Code:
Private Sub btnSave_Click() 
With Form_Add_Box.RecordsetClone 
.AddNew 
!Aisle = txtAisle.Value 
!Sect = txtSect.Value 
!Box = txtBox.Value 
!Fabric = txtFabric.Value 
!V_Color = txtV_Color.Value 
!Qty = txtQty.Value 
.Update 
End With 


With Me.Transactions.Form.RecordsetClone 
.AddNew 
!BoxID = HoodsID 
!OldQty = Qty.Value 
!NewQty = Qty.Value 
!Initials = txtInitials.Value 
!Comment = txtComment.Value 
!Adjustments = Qty.Value 
.Update 
End With 
End Sub
 
Old August 10th, 2006, 12:46 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Take your variables first, before updating the recordsets. Then just reuse it as needed. You shouldn't pass non-variables to your recordsets. Take all these as variables first, then pass the appropriate variable to the recordset update. Reuse the HoodsID where ever you are getting it from. If it is not here, make it a global variable, set and take the value, and reset the value to "" when not in use.


Private Sub btnSave_Click()

Dim HoodsID As Integer

HoodsID = Me.HoodsID '?

With Form_Add_Box.RecordsetClone
.AddNew
!Aisle = txtAisle.Value
!Sect = txtSect.Value
!Box = txtBox.Value
!Fabric = txtFabric.Value
!V_Color = txtV_Color.Value
!Qty = txtQty.Value
.Update
End With


With Me.Transactions.Form.RecordsetClone
.AddNew
!BoxID = iHoodsID
!OldQty = Qty.Value
!NewQty = Qty.Value
!Initials = txtInitials.Value
!Comment = txtComment.Value
!Adjustments = Qty.Value
.Update
End With
End Sub


mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL to identify the primary key in a table? dbayona SQL Server 2005 1 October 24th, 2007 02:33 AM
how can i alter table (primary key + identity ) keyvanjan SQL Server 2000 1 July 12th, 2007 06:49 AM
Update Table with dataset without primary key ranakdinesh ASP.NET 2.0 Professional 1 September 28th, 2006 05:30 PM
Get primary Key Columns From Access Table MeSaqi Access VBA 0 July 18th, 2003 08:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.