I made yet another new test database and gave that field the name DoE_ID from the get-go, and it worked fine. I then made another test database and tried DoE ID (the original name of the field in the existing database that I ultimately want to apply this to) and it DID NOT work. I assume because of the space. I didn't realize the space would be such a big problem when I named the table in the original database.
Yes, it was probably due to the space. Part of the naming convention I follow is: Object names in Access are only letters, numbers and the underscore. NO spaces, punctuation or special characters. Object names do not begin with a number. Object names are fields, controls, tables, forms, queries, reports and modules.
I guess my question now is, is there a way to apply this code to a database with objects that were not named appropriately to be compatible with code? Especially since renaming the fields seems to be a no-go?
While an object name should not have a space in it, if you DO have one or more spaces, that name must be delimited with brackets. For example, "DoE ID" (with a space) could be used if you use delimiting brackets :
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.[DoE ID] = Nz(DMax("[DoE ID]", "Table1")) + 1 ' the "ME." refers to the form name.
End Sub
You have a field name in a table that is "SHPO #". A space and the hash mark "#", a date delimiter. ("#" is a special character...)
You have a field name in a table that is "Report Title/key words".
The field name has spaces and the slash (a special character...).
2) I don't know what a control is.
A control is anything you put on a form. In the last attached picture, look at the ribbon. See the grouping "CONTROLS"? You click on a control, then click in the form, and the control you selected (clicked on) in the ribbon is placed on the form. A text box is a control. The buttons are controls. The check boxes are controls.......
Back to the code. My code was a little different because I used a table to store the max number in a field. Since you are looking at the DoE_ID field in table DoE, here is an updated version of the code:
(No spaces)
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.DoE_ID) Then
Me.DoE_ID = Nz(DMax("DoE_ID", "DoE"), 0) + 1
End If
End Sub
(With a space in the name)
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.[DoE ID]) Then
Me.[DoE ID] = Nz(DMax("[DoE ID]", "DoE"), 0) + 1 'if you have spaces/ special characters in the field name
End If
End Sub
Note the line "If IsNull(Me.DoE_ID) Then" .
This check is to see if there is already an entry in the field for the current record. If the field is NULL, then enter a number, else don't change the entry.
This is necessary because if you edited the record, the before update code would execute and change the current number to the max+1. Not what you want, I'm thinking...
Note: Access names controls the same name as the field it is bound to. It is better to take the time to rename the control to a different name.
A text box bound to the field "DoE ID" would initially be named "DoE ID" by Access. I might rename it "tbDoE_ID". (tb = text box and the space replaced with the underscore)
This gets rid of the space and isn't the same as the field it is bound to. Less confusing...... and Access does/can get confused!