Hristo Dobrev
Hristo Dobrev

Reputation: 107

Save record with default form values

I have a form with subform control. The fields in the main form have calculated default values ( 1 being =Date(), the other doing a DLast() ). People should be able to alter those fields but in most cases the default values are correct so users can skip right to the subform. The subform is connected with the record in the main form and if the user doesn't touch any of the main form controls they can't add records to the subform. They are forced to retype the default value of one of the main form records to be able to continue.

My idea was to add this to the main form module:

Private Sub Form_Open(Cancel As Integer)
    DoCmd.RunCommand acCmdSaveRecord
End Sub

It should create a record using the default values and I should add something to ask if the user wants to save or delete changes on exiting the form. But it doesn't work. No record is created, default values have to be "touched" to create the record.

P.S. changing it to Form_Load breaks the form

Upvotes: 1

Views: 1973

Answers (1)

Fionnuala
Fionnuala

Reputation: 91376

Instead of using a default value for date, use code to write the date to the control. This will force the record to be created as long as the control is bound to a field.

Private Sub Form_Current()
    If Me.NewRecord Then
        Me.ADate = Date
    End If
End Sub

Upvotes: 0

Related Questions