Your getting it good. But lets consider one last point. If I enter a April 1st and then enter a March 31st. Oops are DMAX is going to return 04060001, but the March date needs the last 0306???? number.
So lets change the DMAX function to this
DMAX( "Ref", "Job", "Ref like '" & codeDate & "*'")
by adding the filter, we can isolate to the month.
Here's another issue, what if maxRef comes back empty because of the new filter.
dim maxRef as variant, maxID As Integer
dim codeDate as string, maxDate as string
codeDate = format( date, "MMYY")
maxRef = DMAX( "Ref", "Job", "Ref like '" & codeDate & "*'")
if( IsNull(maxRef)) then 'test for new month
maxID = 0 'reset id to 0
else
maxDate = left(maxRef,4) 'get date code
maxID = CInt(right(maxRef, 4)) 'convert to int
end if
Me.ref = codeDate & format(maxID + 1, "0000")
Note that we changed the maxRef type to Variant to accept the null if the value is not found, strings error when you try to use a null. We probably should of done this from the first, cause the first record being created would of generated the error. Then we test to see if the table has any records for the month, and if it does, we get the last.
While I am sitting here lets discuss two other things. The tab idea wasn't just if they were entering in front of the computer, I find it annoying to open and close forms over and over again to add some other information. Also the use of a continuous form lets me be sure I am not adding something twice, or to double check I didn't miss anything.
Lastly lets talk about test. Entry is only part of the battle. It is important that you also go back and check the tables to be sure they are right also. Enter some information and check the tables to verify the keys are right, the data is right. Try to think of what the user might do to mess things up. Too many times in the past I have been burnt not checking the tables. Well enough times to want to pass it on to you. Good luck, it sounds like a fun project and you are doing good.