Populate new record with default values from previous - Continuous form

  • Thread starter Thread starter Trey Shaffer
  • Start date Start date
T

Trey Shaffer

I have a form ( continuous form ) which is called with a filter to display
data for a particular key value, call it AnimalID.

This works fine for displaying existing data, but I would like to be able to
add a record from within the same form. As it is if I go to the new record
and attempt to save or exit, it fails because the AnimalID has not been
set..I do not want to display the AnimalID field in the detail section as an
input field. I want to be able to populate the value automatically with the
AnimalID value that controls the filter.

I thought of one approach trying to capture the AnimalID value from a
previous record and initialize that in the hidden AnimalID field in the new
record.

Also, I would like to be able to add a record, even if none already exist,
so somehow I need to capture the value of the filter that opens the form.
It is launched from another form with the following:
stLinkCriteria = "[AnimalID]=" & Me![AnimalID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

I would appreciate any help here. I'm not a strong Access coder...

I need to:
Identify a reference to the AnimalID value used for the filter.
Identify the right event to use to manipulate the AnimalID value in a
new record.
Generate the code to set the value of the AnimalID field in the new
record field, based on its being empty or null.
 
you can set the value of the OpenArgs property in the calling code, and then
use that property's value when you add a new record. first, change the
calling code to

stLinkCriteria = "AnimalID=" & Me!AnimalID
DoCmd.OpenForm stDocName, , , stLinkCriteria, , , Me!AnimalID

the above passes the value of AnimalID in the calling form, to the OpenArgs
property in the called form, as a string value. suggest you read up on the
OpenForm action in VBA Help, for more information about the arguments. next,
add code to the called form's BeforeUpdate event, as

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.NewRecord Then
Me!AnimalID = Me.OpenArgs
End If

End Sub

this code sets the value of the AnimalID field in the new record, just
before the record is saved to the table.

hth
 
Back
Top