Hi All
Access 2002/2007
WinXP Pro SP2
I'm struggling to add a record into a recordset, or more specifically, copy a record within it.
I have a form with an unlinked subform. The subform is populated by the main form using an ADO recordset and within a begintrans/endtrans. I use an ADO so that a sort can be applied to it.
It's built like this:-
I then want to copy a record within it. There is a Primary Key, AutoNum, named 'Unique_No'. Here's my latest effort at a copy:-
My effort fails at the AddNew with:-
Any Ideas anyone?
Thanks
Access 2002/2007
WinXP Pro SP2
I'm struggling to add a record into a recordset, or more specifically, copy a record within it.
I have a form with an unlinked subform. The subform is populated by the main form using an ADO recordset and within a begintrans/endtrans. I use an ADO so that a sort can be applied to it.
It's built like this:-
Code:
'Module declarations
Public Edit_rstADO As ADODB.Recordset
Public Edit_Cnn As ADODB.Connection
Private Sub Form_Open(Cancel As Integer)
Dim SQLLine As String
Dim Cntrlbx As Control
etc etc
Set Cntrlbx = Me.My_SubForm
'Build req'd SQL
SQLLINE = "SELECT TBL1.*, TBL2.* FROM ((User_Preferences AS TBL1)
INNER JOIN Captions AS TBL2
ON TBL1.Captions_Unique_No=TBL2.Unique_No)
WHERE blah AND blah"
'Set up Transaction resources etc
Set Edit_Cnn = CurrentProject.AccessConnection
Edit_Cnn.BeginTrans
Edit_Transaction_Begun = True
'Create an instance of the ADO Recordset class and set its properties
Set Edit_rstADO = New ADODB.Recordset
With Edit_rstADO
Set .ActiveConnection = Edit_Cnn
.Source = SQLLine
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With
'Set the form's Recordset property to the ADO recordset
Set Cntrlbx.Form.Recordset = Edit_rstADO
Cntrlbx.Form.OrderBy = "[Field_Selected] DESC,[TBL1.List_Order]"
Cntrlbx.Form.OrderByOn = True
End Sub
Code:
Private Sub Copy_Record_Button_Click()
Dim Cntrlbx As Control
Dim Rec_To_Copy_Unqe_No As Long
Dim rst As New ADODB.Recordset
Dim Num_Fields As Integer
'Extract selected record num
Set Cntrlbx = Me.My_SubForm
Rec_To_Copy_Unqe_No = Cntrlbx.Form.Recordset![TBL1.Unique_No]
Set rst = Cntrlbx.Form.RecordsetClone
'Navigate to chosen record
rst.Find "[TBL1.Unique_No]=" & Rec_To_Copy_Unqe_No
'Count it's fields
For Each fld In rst.Fields
Num_Fields = Num_Fields + 1
Next fld
'Disconnect RS from Subform
Set Cntrlbx.Form.Recordset = nothing
'Add the new record
Edit_rstADO.AddNew
'Copy Fields from old record to new record, avoiding PK
For i = 1 To Num_Fields
If Edit_rstADO(i).Name <> "Unique_No" Then
Edit_rstADO(i).Value = rst(i).Value
End If
Next i
'Save the changes
Edit_rstADO.Update
'Re-connect Rs to SubForm
Set Cntrlbx.Form.Recordset = Edit_rst_ADO
End sub
But looking at the connection setup this should be OK?Error No: 3251 - Error Decription: Object or provider is not capable of performing requested operation.
Any Ideas anyone?
Thanks