Let's get everything into the same discussion. There is a requirement that
the 5-digit barcode number not end with 0. To that end, I suggested this
code:
Private Sub Form_Current()
If Me.NewRecord Then
If Right(DMax("BarcodeID","YourTable"),1) = 9 Then
Me.BarcodeID = Format(DMax("BarcodeID","YourTable") +
2,"00000")
Else
Me.BarcodeID = Format(DMax("BarcodeID","YourTable") +
1,"00000")
End If
End If
Me.txtCompleteNumber = "159" & BarcodeID & "0000000000"
End Sub
The trailing zero is a nuisance, but maybe something like this would take
care of it:
Private Sub Form_Current()
If Me.NewRecord Then
If Right(DMax("BarcodeID","YourTable"),2) = 9 Then
Me.BarcodeID = Format(Left(DMax("BarcodeID","YourTable"),5) +
2,"00000") & "0"
Else
Me.BarcodeID = Format(Left(DMax("BarcodeID","YourTable"),5)
+ 1,"00000") & "0"
End If
End If
Me.txtCompleteNumber = "159" & BarcodeID & "0000000000"
End Sub
The idea is that BarcodeID will be a sequential five-digit text number (with
leading zeros) with a 0 appended, making it a six-digit number.
You said that the user would be duplicating the record and adding a 1 in
place of the trailing zero. That's not all that difficult, except I'm not
sure how it would work together with the code in the form's Current event.
In other words, if you are creating a new record, the form's Current event
code may override the attempt to duplicate the record, and would assign a
new number based on the incrementing system you have set up so far. One
option is to require the user to click a command button that would assign
the number (instead of using the form's Current event) and save the record.
You could do this by some means such as having a single visible text box for
user input, and a command button. The command button would, along with
assigning the number and saving the record, unhide other text boxes.
All of this assumes that my modified code will work as intended. Rick B, do
you have any comments, observations, etc.? Is this the long way around?
As Rick said, sort order in the table is not relevant. You can use a query
for that. You could also use report sorting, or Order By in a form, but the
general idea is the same.
Also, is this a multi-user environment? If so, you will need some error
handling or some way of dealing with the situation when another user start a
record before the first person is finished. Both records will attempt to
grab the same number, and there will be a conflict. Using the command
button to assign the number will minimize this by narrowing the time frame
between when the record is started and when it is saved, but will not
necessarily eliminate it entirely.
dl said:
I appologize, I should have given more background first. This database is
needed to generate and track barcode numbers being used by my employer.
Historically, this was all done by first generating the barcode number in
excel and then tracking them in access for reporting at a later date.
What
I'm trying to do is get rid of the excel sheet and allow access to assign
the
barcode number automatically. The reason I need the table to store the
leading zeros is that our reporting department pulls sales based on the
barcodes used for promotions. Since the point of sale systems use a
specific
format to trigger specific screens at the checkout, the number has to stay
in
this format: 159#####+(10zeros) thus my delema. (of course the only
number
being stored would be the ##### not the 159 since it's static)
Another complication is that they'd like to, in the next few months, start
to use a 6th digit on SOME barcodes instead of the 5 being used now, which
would mean that these numbers would have to be generated with an ending
zero
and that ending zero would need to be editable - like this
record 1: 159+001230
record 2: 159+001240
record 3: 159+001241 - duplicating record 2 and allowing the user to
change
the 0 to a 1
This is seeming like a pie in the sky, but I figured if anyone would be
able
to help it'd be this newsgroup.
thanks for any help - again.
dl
Rick B said:
It dropped the zeros? If it is stored as a number, then there are no
leading zeros. You must have some kind of formatting set up to force
extra
zero's in your display.
You might first update a new text field with the "formatted" version of
the
entry. Then change your original field to text, then update that field
with
the field from that other new field. Then delete the new field.
As far as sorting, the order in your table is irrelevant. You should not
be
looking at your tables, You should be working in the queries, reports,
and
forms. All of those objects allow you to sort.
--
Rick B
Hi Rick, thanks for the reply
I actually tried that. Problem is that these need to be stored in
the
table sequentially, as they are also my primary key and are being
automatically generated by the If/else statement. When I changed it to
text
it dropped all the leading zeros and became sequenced: 1, 11, 12, 2,
etc...
any other ideas?
I appreciate it!
:
One way is to store these items in a text field in the table, not a
number.
Since these are not items you will be performing math on, and since
they
will not need to use normal number formats, I'd store them as a text
item.
--
Rick B
First of all, thanks to BruceM for the help with an IF/Else
statement
to
generate specific autonumbers, works beautifully.
My problem now is this: I have formatted a number field in my table
[barcodeID] as "00000" so that when a new record is created the
value
is
displayed as "00001". From there I have created an unbound textbox
on
the
form that needs to display the number as ="159" & "00001" &
"00000000"
in
order to display a barcode number that looks like:
159000010000000000
(18digits in total). Problem is that when I set the control source
of
the
unbound textbox to: ="159" & [barcodeID] & "00000000" it doesn't
display
the
leading zeros of the barcodeID field and instead displays it as:
15910000000000 - leaving out the 4 leading zeros that the barcodeID
is
formatted to show. If I have a bound textbox display the contents
of
barcodeID, it does show the leading zeros, what am I doing wrong?
any help is MUCH appreicated!!
dl