Detect new record selected ?

  • Thread starter Thread starter TonyB
  • Start date Start date
T

TonyB

Hi,
I have created a form based upon a table, and I'm using the standard
navigation buttons toolbar to move among the records. I'm trying to find out
what event can detect that the new(*) button has been clicked and the form
is now displaying a new record awaiting entry.
When this occurs I want to write the current date into one field in the new
record, and a calculated value into another field, all done in a vba module
called by that event. I'm stuck as to how to detect that the * button has
been clicked and the form is showing a new record ?
Can anyone give me any pointers ?
TIA
Tony
 
Private Sub Form_Current()

If Me.NewRecord Then
'new record
Else
'not new record
End If

End Sub
 
Tony,

Use the form's On Current event, which fires everytime you go to another
record (new or old). To check if it's a new record and act accordingly,
the code should look something like:

If Me.NewRecord Then
Me.ctlDate = Date
Me.ctlSomething = *some expression*
End If

HTH,
Nikos
 
Hi,
I have created a form based upon a table, and I'm using the standard
navigation buttons toolbar to move among the records. I'm trying to find out
what event can detect that the new(*) button has been clicked and the form
is now displaying a new record awaiting entry.
When this occurs I want to write the current date into one field in the new
record, and a calculated value into another field, all done in a vba module
called by that event. I'm stuck as to how to detect that the * button has
been clicked and the form is showing a new record ?
Can anyone give me any pointers ?
TIA
Tony
why you are not using the default value of this controls or fields?
 
Just use the default values for the controls. In the control that you want
the current date use =Date(). If the date is a form of date-stamping the
record you can lock the control. In the control that you want the calculated
value use =YourExpression. Don't store the calculated value.
 
Hi,
Thanks for all the help. The Me.NewRecord explanation should help me sort it
out.
As to why I'm doing this I think I didn't explain what I was trying to do
clearly. When I create a new record I want to store the date when the record
was created in one field of the new record, and then make sure it is not
changed afterwards. Other fields can be edited, but I want this field to
stay locked afterwards. I assumed this would need some vba to do this ?

The other field is a strange format which is a mixture of the locked stored
date described above and of the number of records created today, so that was
why I was going to use VBA to write this into the record. I could calculate
it from the stored date, and by counting the number of records created that
day prior to this record and display the result. I figured it was better to
do this once when the record is created, rather than doing it everytime the
record is viewed since it will never change after the record is created.

Thanks
Tony
 
Use the forms on insert event, as that ONLY fires when the new record is
actually added.

If the user does NOT start to enter anything,a nd bails out, then you will
NOT have run any code, or dirty the record.

Failure to follow the above advices will cause zillions of blank, or
un-filled records to be created.

And, the nice thing about using the on-insert event is you don't have to
test if this is a new record, since it ONLY can run once!!

And, you don't have to put a text box containing the field on the screen
(and you have clarified this issue also).

So, in the on-insert event, just go:

me!MyDateField = date()

And, that date field does NOT have to be displaying in a text box control on
the form...
 
Back
Top