calculated field not being saved in record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a value that should be incrementing automatically (from a query) when
a form opens. However, the new value is not populating the table. So when
a new form is opened, the value is not incrementing.

Here is my code.

Private Sub Form_Open(Cancel As Integer)

txt_Contract_Main_No = 1 + Nz(DMax("Contract_Main_No",
"Qry_Int_Contracts_Main"), 0)

End Sub

Is there something else I need to do to get the new incremented number to
populate my table?

As always, any help is appreciated.
 
Move this code into the BeforeUpdate event of the *form* (not control), and
limit it to only new records:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Me.txt_Contract_Main_No = 1 + Nz(DMax("Contract_Main_No",
"Qry_Int_Contracts_Main"), 0)
End If
End Sub

I suggest BeforeUpdate rather than BeforeInsert, since that leaves it to the
last possible moment. This reduces the chance of a duplicate if multiple
users are entering data.
 
Hi Allen,

I put this code in the beforeUpdate event and even though it is generating a
new contract # it is still no saving the new contract number to my table.
Any other suggestions?
 
Back
Top