It's probably a poor example (in truth, it seems to me that good Access developers don't script all their database transactions with VBA). Nevertheless, if you are interested in a sample of inserting a value and then using the ID for a second insert here is one way (among what must be a dozen other possibilities).
What I've done here is simply:
- create a POHeader table (with a vendor and a date)
- create a PODetail table (with items that belong to the PO)
We update both tables with some dummy data (hardcoded into the routine). As soon as the PO Header is created, we get the PONumber just assigned, and use it as a foreign key when we update the PODetail table. If any error occurs after the transaction is begun, we roll it back. In reality, you don't really even need to put it in a transaction - just make the PONumber required and if you have the PO Number all is well, and if you don't, it the PODetail record can't be inserted (if the POHeader exists we can still use it, and if not we can try again).
Sample Database with the code module and two tables:
<a href="http://northernocean.net/etc/mrexcel/20110713_db112.zip">Sample Database</a>
sha256sum (zip file): cb206eb80ebf72cebddcdb66c37008525d2fcb3222f47319096749c52c0f6234
As a matter of interest in transactions, you can step through the code and, as soon as you've updated the POHeader table, open it in datasheet view and take a peek - you won't see the transaction. Then continue stepping through the code and check it after the commit transaction statement - now it's visible. For a further exercise, it would be well to do the same thing with a form and no code (I'll see if I can come up with anything that works though I'm short on time this week).
Code (assuming the tables described):
Code:
[COLOR="SeaGreen"]'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR]
[COLOR="SeaGreen"]'SAMPLE CODE - goes in a standard module[/COLOR]
[COLOR="SeaGreen"]'Assumes a database with two tables:[/COLOR]
[COLOR="SeaGreen"]'POHeader (PONumber {long}, SupplierID {long}, PODate {date})[/COLOR]
[COLOR="SeaGreen"]'PODetail (POLineNumber {long}, ItemNumber {long}, Qty {double}, PONumber {long|foreign key})[/COLOR]
[COLOR="Navy"]Sub[/COLOR] DoInsert()
[COLOR="Navy"]Dim[/COLOR] rs [COLOR="Navy"]As[/COLOR] DAO.Recordset
[COLOR="SeaGreen"]'--------------------------------------[/COLOR]
[COLOR="SeaGreen"]'Dummy data variables[/COLOR]
[COLOR="Navy"]Dim[/COLOR] mySupplierID [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] myPODate [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]Dim[/COLOR] myPONumber [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] myItemNumber [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] myQuantity [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Double[/COLOR]
[COLOR="SeaGreen"]'Flag variable for commit[/COLOR]
[COLOR="Navy"]Dim[/COLOR] intTransactionCommitted [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]GoTo[/COLOR] ErrHandler:
[COLOR="SeaGreen"]'//Sample data[/COLOR]
mySupplierID = 1
myPODate = [COLOR="Navy"]Date[/COLOR]
myItemNumber = 1
myQuantity = 10
[COLOR="SeaGreen"]'-------------------------------------------------------[/COLOR]
[COLOR="SeaGreen"]'BEGIN TRANSACTION[/COLOR]
intTransactionCommitted = -1
DBEngine.Workspaces(0).BeginTrans
[COLOR="SeaGreen"]'//Create a PO[/COLOR]
[COLOR="Navy"]Set[/COLOR] rs = CurrentDb.OpenRecordset("POHeader", dbOpenTable)
[COLOR="Navy"]With[/COLOR] rs
.AddNew
![SupplierID] = mySupplierID
![PODate] = myPODate
[COLOR="SeaGreen"]'//Save PO[/COLOR]
.Update
[COLOR="SeaGreen"]'//Get PO Number just created[/COLOR]
.Bookmark = .LastModified
myPONumber = ![PONumber]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
rs.Close
[COLOR="SeaGreen"]'//Create a PO Line[/COLOR]
[COLOR="Navy"]Set[/COLOR] rs = CurrentDb.OpenRecordset("PODetail", dbOpenTable)
[COLOR="Navy"]With[/COLOR] rs
.AddNew
![ItemNumber] = myItemNumber
![Qty] = myQuantity
![PONumber] = myPONumber
[COLOR="SeaGreen"]'//Save PO Line[/COLOR]
.Update
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
rs.Close
[COLOR="SeaGreen"]'//No errors - so commit transactions[/COLOR]
DBEngine.Workspaces(0).CommitTrans
intTransactionCommitted = 0
[COLOR="Navy"]Set[/COLOR] rs = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="SeaGreen"]'END TRANSACTION[/COLOR]
[COLOR="SeaGreen"]'-------------------------------------------------------[/COLOR]
My_Exit:
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] rs [COLOR="Navy"]Is[/COLOR] [COLOR="Navy"]Nothing[/COLOR] [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]If[/COLOR] rs.EditMode <> dbEditNone [COLOR="Navy"]Then[/COLOR]
rs.CancelUpdate
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
rs.Close
[COLOR="Navy"]Set[/COLOR] rs = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
ErrHandler:
[COLOR="Navy"]If[/COLOR] intTransactionCommitted <> 0 [COLOR="Navy"]Then[/COLOR]
DBEngine.Workspaces(0).Rollback
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
MsgBox "An error occurred: " & Err.Number & " " & Err.Description _
& vbNewLine & "Transaction was cancelled."
[COLOR="Navy"]Resume[/COLOR] My_Exit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]