Expression not working

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

Guest

Hello
I have the following expression
Me.ID = Nz(DMax("RcId", "StockInventorylog", "RC ='" & Me.RC & "'"), 0) + 1
It's purpose is to add 1 to the ID for each RC value.
When I put this expression in the Before Update event of another field in
form it works.

When i put this expression in the before insert event of the form , it
doesn't work. except for the first time. ie for Id 1 only.

Please can you help me solve this problem
 
You could set the DefaultValue property of the ID box to this:

=Nz(DMax("RcId", "StockInventorylog", "RC ='" & Me.RC & "'"), 0) + 1

This would automatically make the ID increment when you go to each new
record. You can have the ID field disabled so that the user can see it but
not edit it. Hopefully, this is a single-user application. A problem will
occur, of course, if two users go to a new record concurrently. Because
neither record has been saved yet, both will get the same ID, and whoever
saves last will either win or get an error, depending on the table & code
setup.

The best way is to just use an AutoNumber field for ID, and then it
increments automatically without any code.
 
At Form_BeforeInsert, I think the Value of RC will be Null and I guess
that'ws why the expression won't work.

In the Form_BeforeInsert Event, add the following code:

Debug.Print Me.RC
Debug.Print IsNull(Me.RC)

and check the Immediate window to see what is printed.
 
Back
Top