Access removes the parentheses in VBA code, but they are needed in text box
and query expressions. By the same token, Access will put square brackets
around properties in text box expressions, but they are not used in those
same expressions in VBA. It is not a coding problem, but I do wish
sometimes for more consistency.
A text box Before Update event runs only if the text box has had something
typed into it. Since you are assigning the value via code, the Before
Update event will not run *for that text box*. The form's Before Update
event, however, runs whenever you try to save a record that has been
modified by any means. Access tries to save a record when you explictly
save it by a command button click event or some such, when you attempt to
navigate to another record, when you try to close the database, when you
move to a subform, and probably a few other reasons that do not occur to me
at the moment.
The code looks OK, provided:
1) IncrNum is the name of the field
2) txtIncrNum is the name of the text box bound to IncrNum
3) tblIncidents is the name of the table
4) datefield is the name of a date field in the record
It would help if you choose a different name for the calculated query field
that produces the 09-001 etc. format than for the text box (or anything
else, for that matter). For the query field, maybe FullNumber rather than
txtIncrNum. Save txtIncrNum as the name of the text box. In general, don't
use the same name for two different things. It can lead to confusion both
for you and for Access.
The problem with the code is that it never runs, because the text box Before
Update event does not occur. You can place the code in the form's Current
event, so that the number is created as soon as you move to a new record
(DefaultValue applies only to new records). Or you can use the form's
Before Insert event, which runs when the user starts typing in a new record.
Or you could use the form's Before Update event. This would have an
advantage in a multi-user environment in that it would be rather unlikely
that two people would save a new record at the exact same time, so the
chance of a duplicate number is greatly reduced. The disadvantage is that
the number is not visible when the record is created. There are other
options for the code to create a new number, so if you have a specific need,
describe it and we'll see what's possible.
Another very important thing: The text box txtIncrNum containing the can
contain only the IncidentNumber (or IncrNum). You can make the text box 0"
x 0", but its Visible property must be True. It's only purpose is to
provide a place for the incremented number to be created. To display the
number in the format you wish, create a query field as you have done to
produce the 09-001 number. If that field is named FullNumber (rather than
txtIncrNum as it is now), bind a text box to FullNumber.
http://picasaweb.google.com/matt.kc8bew/AccessHelp?authkey=Gv1sRgCKa3....
I rarely check that e-mail. I took a look just now. For one thing, the
expression you posted shows line breaks in the wrong places. The
newsreader
adds a line break when it runs out of space on a line. You need to keep
that in mind when viewing code, links, and so forth. In VBA a line break
needs to be indicated by a space followed by an underscore:
Me.txtIncrNum.DefaultValue = Nz(DMax("[IncrNum]", "[tblIncidents]", _
"Year([Datefield]) = " & Year(Date())), 0) + 1
Also, there is no quote after the 1, which I see in your screen shot.
The quotation mark was automatically being filled in. Adding the
underscore properly stopped that.
Now I cannot get the parens to stay for the Date(). When I save it
changes Year(Date())), 0) + 1 to
Year(Date)), 0) + 1 without the date parens. It will not take. I get
a code helper box (don't know what else
to call it) that shows Year(date). Screenshot at the link I gave you
titled year_date.jpg.
For the rest the main problem seems to be that the number is not showing
up
on the form. Where are you running the code to set the DefaultValue of
txtIncrNum?
It is under the txtIncrNum event tab for Before Update.
I feel like I am going in circles. I think that we just have it but
little thing is stopping us.
A small setting or something. I have gone round and round with this
and I can't figuire it out.
The bad thing is that this is 1 of at least 3 things that I need to
get done on this. The other
2 are buttons that I want to have do special procedures. 1 at a time
though.
Matt