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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.