Easy-I want to automaticaly populate a subform with previous entri

J

Jose Sobral

I have a form with a subform on it. I select the parent field in the form
and fill the subform with records. When I move to the next value in the
parent field in the form, the respective subform values apear blank. What I
want is that the subform appear populated with the previous records that met
a certain criteria.
Do i have to create a button with special code to do this??
Please help. I m sorry if this is too obvious.
 
M

Marshall Barton

Jose Sobral said:
I have a form with a subform on it. I select the parent field in the form
and fill the subform with records. When I move to the next value in the
parent field in the form, the respective subform values apear blank. What I
want is that the subform appear populated with the previous records that met
a certain criteria.


Depends on the criteria.

If it's just the usual primary key - foreign key
relationship, then the subform control's Link Master/Child
Fields property should do what you want.
 
J

Jose Sobral

The problem is that when i change the value on the form (master) the next
subform fields are obviously empty waiting to be filled. What I want is that
they appear populated with a copy of the previous subform records of the
previous form field...
 
M

Marshall Barton

I son't understand. Are you saying that you don't want the
subform records to be related to the main form's current
record?

Or, are you saying they are related, but you want a new main
form record to be related to a copy of the records from
the previous main form record?
 
J

Jose Sobral

It is the second option:

I am saying they are related, but I want a new main
form record to be related to a copy of the records from
the previous main form record

Thanks for the interest in my case

if you have an email i can send you screen shots
Thanks again
 
M

Marshall Barton

Jose said:
I am saying they are related, but I want a new main
form record to be related to a copy of the records from
the previous main form record


Sounds more than a little odd and is not at all obvious, but
I think(?) you can do that by saving the value of the main
form record's ID every time it is changed. Then, when the
main form moves to a different record, check to see if the
subform does not have any records. If the subform has no
related records, insert the matching records related to the
main form's previous record.

First, add a hidden text box (named txtPrevious) to the main
form, next to the one you use to select a record (Iet's say
its name is txtID). Then add a line of code to text box
txtID's AfterUpdate event procedure:
Me.txtPrevious = Me.txtID

Code to copy the subform records then might look something
like:

Dim SQL As String
If Me.subform.Form.Recordset.RecordCount = 0 Then
SQL = "INSERT INTO subtable (idfield ,fld1,fld2,...) " _
& "SELECT " & Me.txtID & ",fld1,fld2,... " _
& "FROM subtable " _
& "WHERE idfield = " & Me.txtPrevious
CurrentDb.Execute SQL
End If

Be sure to replace all the field and table names that I had
to guess at and then see how close the result is to what you
want. Post back with a copy of the code you actually used
and an explanation of what happened.
 

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