Really quick Nz(Dmax question

N

NoviceNana

Trying to create a counter field on my subform:
The field on the subform that will hold the count is called
"CounterNew"
the mainform is called frm_weekly
the subform is called tbl_encounter_subform

This is my code:
Private Sub Form_Current()

If Me.NewRecord Then
Me.CounterNew = 1 + Nz(DMax("CounterNew", "tblEncounters", "IDNumber="
& Me.IDNumber), 0)
End If
End Sub

Nothing is happening.
Can anyone tell me why?
Nana :)
 
N

NoviceNana

I did some more searching and came up with this code... which is also
not working
Private Sub Form_Current()

If Me.NewRecord Then
Me.CounterNew = Nz(DMax("[CounterNew]", "tblEncounters", "[IDNumber]=
" & Me!IDNumber), 0) + 1
End If
End Sub

.....
 
J

John Smith

I would suspect that your DMax is failing. If you are on a new record then
IDNumber will probably be null so your criteria will just say IDNumber= which
would be invalid.

I would also raise two points:

If this database is multi user then this approach will fail if two users start
records at the same time, you would be better off putting the code into
BeforeUpdate.

If the user moves to a new record and then changes their mind you will write a
blank record apart from the CounterNew. If you wish to keep the code where it
is set the counter value as CounterNew.DefaultValue instead, it will then be
ignored if the record is not saved.

HTH
John
##################################
Don't Print - Save trees
I did some more searching and came up with this code... which is also
not working
Private Sub Form_Current()

If Me.NewRecord Then
Me.CounterNew = Nz(DMax("[CounterNew]", "tblEncounters", "[IDNumber]=
" & Me!IDNumber), 0) + 1
End If
End Sub

....

Trying to create a counter field on my subform:
The field on the subform that will hold the count is called
"CounterNew"
the mainform is called frm_weekly
the subform is called tbl_encounter_subform

This is my code:
Private Sub Form_Current()

If Me.NewRecord Then
Me.CounterNew = 1 + Nz(DMax("CounterNew", "tblEncounters", "IDNumber="
& Me.IDNumber), 0)
End If
End Sub

Nothing is happening.
Can anyone tell me why?
Nana :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top