Copying data to subform recordset

B

bhNish

I posted this in ...access.forms without replies so I thought this NG
might be more appropriate.


I have a form containing a subform in which I want to automatically
copy information from the form record to the subform record. The form
shows a record from a query of table1 and the subform shows data from
a query of table2 using the appropriate master/child link.

Within the event procedure for a button click, the code works ok for
the first record that it modifies, but the next record returns the error
"No Current Record" (runtime error 3021)
on the line where I enable Edit mode.

If I close the form and reopen it, the first time I enable Edit,
then make changes and then Update it's OK. The next record that I
try to Edit, I get the error.

I'm stumped... can anybody help me?

Here's the Button click event that calls the function, shown below:

' do for each record
Me.Recordset.MoveFirst
For i = 1 To Me.Recordset.RecordCount

' copy data from form data to subform record
rtnFlag = CompareFormFields(Me, Me.SubFormMemberTo.Form, True)

' goto next record
Me.Recordset.MoveNext

Next i
' enddo - for each record


Function CompareFormFields(form1 As Form, form2 As Form, copyFlag As Boolean) As Boolean

' if not all fields of form2 same as form1 then

... <blah> ...
ctrlSource= "someFieldName"
... <blah> ...

form2.Recordset.Edit ' error occurs here on 2nd attempt at Edit
form2.Recordset.Fields(ctrlSource) = form1.Recordset.Fields(ctrlSource)
form2.Recordset.Update

' end if

... <blah> ...

End Function
 
D

dragos

I posted this in ...access.forms without replies so I thought this NG
might be more appropriate.


I have a form containing a subform in which I want to automatically
copy information from the form record to the subform record. The form
shows a record from a query of table1 and the subform shows data from
a query of table2 using the appropriate master/child link.

Within the event procedure for a button click, the code works ok for
the first record that it modifies, but the next record returns the error
"No Current Record" (runtime error 3021)
on the line where I enable Edit mode.

If I close the form and reopen it, the first time I enable Edit,
then make changes and then Update it's OK. The next record that I
try to Edit, I get the error.

I'm stumped... can anybody help me?

Here's the Button click event that calls the function, shown below:

' do for each record
Me.Recordset.MoveFirst
For i = 1 To Me.Recordset.RecordCount

' copy data from form data to subform record
rtnFlag = CompareFormFields(Me, Me.SubFormMemberTo.Form, True)

' goto next record
Me.Recordset.MoveNext

Next i
' enddo - for each record


Function CompareFormFields(form1 As Form, form2 As Form, copyFlag As Boolean) As Boolean

' if not all fields of form2 same as form1 then

... <blah> ...
ctrlSource= "someFieldName"
... <blah> ...

form2.Recordset.Edit ' error occurs here on 2nd attempt at Edit
form2.Recordset.Fields(ctrlSource) = form1.Recordset.Fields(ctrlSource)
form2.Recordset.Update

' end if

... <blah> ...

End Function


Hi,

try this:
Function CompareFormFields(form1 As Form, form2 As Form, copyFlag As Boolean) As Boolean

' if not all fields of form2 same as form1 then

... <blah> ...
ctrlSource= "someFieldName"
... <blah> ...
x = form2.Recordset.RecordCount

Select Case x
Case Is = 0
form2.Recordset.AddNew
form2.Recordset.Fields(ctrlSource) =
form1.Recordset.Fields(ctrlSource)
form2.Recordset.Fields("child id") = form1.Recordset.Fields("id")
form2.Recordset.Update
Case Else
form2.Recordset.MoveFirst ' or wherever you want to move
form2.Recordset.Edit
form2.Recordset.Fields(ctrlSource) =
frm1.Recordset.Fields(ctrlSource)
form2.Recordset.Update
End Select
' end if

... <blah> ...

End Function

Let me know if it works. If you need more explications just ask.

Dragos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top