BerthNerd
BerthNerd

Reputation: 1

VBA Access form: Submit Button to create new record but keep some values in the form

I have a form to enter all necessary data. With the wizard I created a button that saves the data as a new record, but this button clears the form.

I want this button to do exactly that, but keep some of the entered values in the form, because those values will stay the same for a certain amount of records.

Currently my button runs this code generated by the button creation wizard:

Private Sub submit_btn_Click()
On Error GoTo submit_btn_Click_Err

    On Error Resume Next
    DoCmd.GoToRecord , "", acNewRec
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If


submit_btn_Click_Exit:
    Exit Sub

submit_btn_Click_Err:
    MsgBox Error$
    Resume submit_btn_Click_Exit

End Sub

When the button is clicked I want to clear all values in the form, except for the date and a group field. Can I easily do this in this code or is there a way to do this via the default value property of these fields?

Upvotes: 0

Views: 2239

Answers (1)

Shazu
Shazu

Reputation: 587

Try the following:

Go to the properties of your form and put the following code in the BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)
  entry_date.Tag = CLng(entry_date.Value)
  acc_value.Tag = acc_value.Value
End Sub

In the Current event of the form, put the following code:

Private Sub Form_Current()
  If Me.NewRecord Then
    entry_date = CDate(entry_date.Tag)
    acc_value = acc_value.Tag
  End If
End Sub

That is all you need to do. If you have any questions please let me know

Upvotes: 0

Related Questions