Increment field in form not working

Posted on 2012-08-12
Last Modified: 2012-08-17
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
Question by:Patty01Access
    LVL 77

    Expert Comment

    You should be looking for the propertyid.

    Me!UnitId = Nz(DMax("[UnitID]", "tblVacyUnits", "[ppty_ID]=" & Me!ppty_Id), 0) + 1

    Author Comment

    Thanks Peter.  

    But I get the exact same error when trying to do that, except it says "Ppty_ID" vrs UnitID
    LVL 77

    Expert Comment

    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.

    Author Comment

    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?

    Author Comment

    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.

    Accepted Solution

    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.

    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now