Increment field in form not working

I'm trying to get a form to increment a field when someone enters a new record for a property. So for below, on the last one I want the code to fill in "3" for the UnitID when a person enters that record.

Ppty_ID      UnitID
1                      1
1                       2

I tried using the code suggested in an earlier answer about how to do this on the "BeforeInsert".  But it keeps bombing.  I know it's something simple, but I'm not seeing the problem.

Here's the code I used:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me!UnitId = Nz(DMax("[UnitID]", "tblVacyUnits", "[UnitID]=" & Me!UnitId), 0) + 1
Me.Dirty = False
End Sub

Got the error: Syntax error (missing operator in query exression "[UnitID]="
when I do the above.

Please let me know what I'm doing wrong.
Thanks,  Patty
Who is Participating?
Patty01AccessConnect With a Mentor Author Commented:
Finally got it.  It was in the wrong event place on the subform.

I removed it from "Before Insert"  and put it in "Before Update"

If IsNull(Me.UnitId) Then
    Me!UnitId = Nz(DMax("[UnitID]", "[tblVacyUnits]", "[Ppty_ID]=" & Me!PPTY_ID), 0) + 1
    Cancel = True
End If

Works fine now.
You should be looking for the propertyid.

Me!UnitId = Nz(DMax("[UnitID]", "tblVacyUnits", "[ppty_ID]=" & Me!ppty_Id), 0) + 1
Patty01AccessAuthor Commented:
Thanks Peter.  

But I get the exact same error when trying to do that, except it says "Ppty_ID" vrs UnitID
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Where exactly is propertyid coming from?  I don't quite see how it is available to be used in the beforeinsert event.

You can't look up the max UnitId until the propertyid has been entered.
Patty01AccessAuthor Commented:
Color me stupid on this one Peter.

I don't know how, but prior to entering that code, when I would add a record, the ppty_ID from the Property form would automatically be inserted into the tblVacyUnits along with the other info.

Do I need to go about this another way?
Patty01AccessAuthor Commented:
Sorry, I screwed up the last attachment. This one is hopefully correct.

I see that I should have had the Ppty_ID in the code.

I've revised it to show that, and it's in the "Before Insert" in the subfrmVacyUnits.

The main form for inputting the data is the frmProperty.

Now when I go to add a new record, same error.

So should the code go somewhere else?

Thanks again.
Patty01AccessAuthor Commented:
I didn't understand what Peter was saying about the Ppty_Id, so kept trying to put the code in different places until it finally worked.

This may not be the most elegant solution, but it worked for me.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.