Patty01Access
asked on
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.
Like:
Ppty_ID UnitID
1 1
1 2
1
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
UnitsDB.accdb
Like:
Ppty_ID UnitID
1 1
1 2
1
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
UnitsDB.accdb
ASKER
Thanks Peter.
But I get the exact same error when trying to do that, except it says "Ppty_ID" vrs UnitID
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.
You can't look up the max UnitId until the propertyid has been entered.
ASKER
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?
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?
ASKER
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.
UnitsDB-Revised.accdb
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.
UnitsDB-Revised.accdb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
This may not be the most elegant solution, but it worked for me.
Me!UnitId = Nz(DMax("[UnitID]", "tblVacyUnits", "[ppty_ID]=" & Me!ppty_Id), 0) + 1