Hi All
Access 2002/2007
WinXPPro
I've been experimenting with BeginTrans, CommitTrans, RollbackTrans. I have successfully created a short but sweet copy procedure but I wondered if one of you, more experienced, chaps could have a quick proof read?? I'm particularly keen to ensure that the ID of a new record number is correctly returned when in a multi-user environment?
The proc copies a record within my 'Addrs' table, recovers the ID of the new record and then writes a record to my history table, 'Adt_Recs'. It does all this within one transaction so as to maintain the integrity of the history table... i.e. copy fails, no history record gets written and the newly created record gets discarded.
Please note: My 'Addrs' Autonumbered field is called 'Unique_No' rather then the more usual 'ID'. Also I pass variables to ny proc but I've hardcoded them below to aid readability...
Any tips much appreciated!!
Thanks
Access 2002/2007
WinXPPro
I've been experimenting with BeginTrans, CommitTrans, RollbackTrans. I have successfully created a short but sweet copy procedure but I wondered if one of you, more experienced, chaps could have a quick proof read?? I'm particularly keen to ensure that the ID of a new record number is correctly returned when in a multi-user environment?
The proc copies a record within my 'Addrs' table, recovers the ID of the new record and then writes a record to my history table, 'Adt_Recs'. It does all this within one transaction so as to maintain the integrity of the history table... i.e. copy fails, no history record gets written and the newly created record gets discarded.
Please note: My 'Addrs' Autonumbered field is called 'Unique_No' rather then the more usual 'ID'. Also I pass variables to ny proc but I've hardcoded them below to aid readability...
Any tips much appreciated!!
Code:
Public Function My_Copy_Record() As Long
On Error GoTo Err_My_Copy_Record
Dim ADODBconn As ADODB.Connection
Dim rsRead As New ADODB.Recordset
Dim SQLLINE As String
Dim Fld_Str As String
Dim Extg_Record_Unique_No As Long
Dim New_Record_Unique_No As Long
Dim Curr_User_Unique_No As Long
'Set Up Connection and begin SQL Transaction
Set ADODBconn = CurrentProject.AccessConnection
ADODBconn.BeginTrans
'Build COPY SQL
SQLLINE = " INSERT INTO Addrs (Name, Address_Line1, Address_Line2, Address_Line3, Added_by_Unique_No)" _
& " SELECT 'TestRecord', Address_Line1, Address_Line2, Address_Line3, 7 FROM Addrs WHERE Unique_No = 15"
'create new copy of record
ADODBconn.Execute SQLLINE, dbFailOnError
'Build new ID extract SQL
SQLLINE = "SELECT @@identity AS New_Record_Unique_No FROM Addrs WHERE Added_By_Unique_No=7"
rsRead.Open SQLLINE, ADODBconn, adOpenStatic, adLockReadOnly
If rsRead.EOF Then
MsgBox "COPY FAILED!!"
GoTo Err_My_Copy_Record
Else
New_Record_Unique_No = rsRead!New_Record_Unique_No
End If
rsRead.Close
'Build History Record SQL
SQLLINE = " INSERT INTO Adt_Recs (Description,Added_By_Unique_No)" _
& " VALUEs('New Record Created =" & New_Record_Unique_No & "',7)"
'Create History Record
ADODBconn.Execute SQLLINE, dbFailOnError
ADODBconn.CommitTrans
ADODBconn.Close
Exit_Here:
Set ADODBconn = Nothing
Exit Function
Err_My_Copy_Record:
If Err.Number = -2147467259 Then
MsgBox Err.Description
Resume Exit_Here
Else
MsgBox Err.Description
ADODBconn.RollbackTrans
ADODBconn.Close
Set ADODBconn = Nothing
Exit Function
End If
End Function
Thanks