Auto Number

  • Thread starter Thread starter tennmo
  • Start date Start date
T

tennmo

I am very much a beginner with Access so don't think to bad of me when you
read my question. My boss wants me to create a drawing number database where
the number includes a class code data field with a seq. auto generated number
for each class code. Here are some examples: BRKT-10000, BRKT-10001,
FAST-10000, FAST-10001.

How do I do this? Might be very easy but when you don't know anything, very
hard.

Thanks in advance for any help you can give.
 
No reason to think bad of you at all; it’s a perfectly reasonable question.

Firstly the new record should be added via a form, which is where data
should be entered anyway, never directly into the datasheet view of the
table. Secondly use two columns in the table, ClassCode of text data type,
and DrawingNumber of (long integer) number data type. Its much easier to
concatenate two values together, than to split them. You should make the two
columns the composite primary key of the table, or otherwise index them
uniquely (in combination, that is, not individually). Having two columns also
allows you easily to look up the last number for the class code in question.
This done by putting code in the AfterUpdate event procedure of the ClassCode
control on the form. The code looks up the last number in the table (called
Drawings in the example below) for that code and adds 1, using the DMax
function to get the last (highest) number. So the code goes like this:

Dim varLastNumber as Variant
Dim strCriteria As String

strCriteria = "[ClassCode] = """ & Me.[ClassCode] & """"
varLastNumber = DMax("[DrawingNumber]", "[Drawings]", strCriteria)

Me.[DrawingNumber] = Nz(varLastNumber,0)+1

The Nz function is used to return a zero if the Dmax function returns a
Null, which would be the case for the first drawing of a class code.

To format the DrawingNumber to 5 digits, with leading zeros where necessary,
set its Format property to:

00000

If you want to combine the class code and drawing number, in a report say,
you can do so by setting the ControlSource property of an unbound text box to:

=[ClassCode] & "-" & Format([Drawing Number],"00000")

Or in a query you can put the following in the 'field' row of a blank column
in query design view:

FullDrawingNumber:[ClassCode] & "-" & Format([Drawing Number],"00000")

This will return a column named FullDrawingNumber in the query, to which you
can bind a text box control on a form or report.

One caveat: in a multi-user environment on a network, if two or more users
try to add a new record for the same class code simultaneously the first one
to save the record will succeed, but the subsequent ones will get an error
because of the key violation. If this could be a problem the error can be
handled in the form's Error event procedure.

As the class codes seem to be in upper case you can make sure that this is
the case even if the user enters them in lower or mixed case by putting the
following code in the ClassCode control's KeyPress event procedure:

Dim strCharacter As String

' Convert ANSI value to character string.
strCharacter = Chr(KeyAscii)
' Convert character to upper case, then to ANSI value.
KeyAscii = Asc(UCase(strCharacter))

If you are unfamiliar with entering code in event procedures this is how its
done:

Select the control in form design view and open its properties sheet if its
not already open. Then select the relevant event property in the properties
sheet. Click on the 'build' button; that's the one on the right with 3 dots.
Select 'Code Builder' in the dialogue, and click OK. The VBA window will
open at the event procedure with the first and last lines already in place.
Enter the lines of code between these two existing lines.

Ken Sheridan
Stafford, England
 
I am very much a beginner with Access so don't think to bad of me when you
read my question. My boss wants me to create a drawing number database where
the number includes a class code data field with a seq. auto generated number
for each class code. Here are some examples: BRKT-10000, BRKT-10001,
FAST-10000, FAST-10001.

How do I do this? Might be very easy but when you don't know anything, very
hard.

Thanks in advance for any help you can give.

I would certainly use two fields for this (ClassCode, ClassSeq let's say), not
one. They can be concatenated for display purposes.

What's the context? At what point do you want these numbers assigned? Are you
entering data from a form, importing records, automatically generating a range
of values, or what? Any of these can be done but the technique will differ.
 
Ken,

Thanks so much for the help. I will give it a try.

Ken Sheridan said:
No reason to think bad of you at all; it’s a perfectly reasonable question.

Firstly the new record should be added via a form, which is where data
should be entered anyway, never directly into the datasheet view of the
table. Secondly use two columns in the table, ClassCode of text data type,
and DrawingNumber of (long integer) number data type. Its much easier to
concatenate two values together, than to split them. You should make the two
columns the composite primary key of the table, or otherwise index them
uniquely (in combination, that is, not individually). Having two columns also
allows you easily to look up the last number for the class code in question.
This done by putting code in the AfterUpdate event procedure of the ClassCode
control on the form. The code looks up the last number in the table (called
Drawings in the example below) for that code and adds 1, using the DMax
function to get the last (highest) number. So the code goes like this:

Dim varLastNumber as Variant
Dim strCriteria As String

strCriteria = "[ClassCode] = """ & Me.[ClassCode] & """"
varLastNumber = DMax("[DrawingNumber]", "[Drawings]", strCriteria)

Me.[DrawingNumber] = Nz(varLastNumber,0)+1

The Nz function is used to return a zero if the Dmax function returns a
Null, which would be the case for the first drawing of a class code.

To format the DrawingNumber to 5 digits, with leading zeros where necessary,
set its Format property to:

00000

If you want to combine the class code and drawing number, in a report say,
you can do so by setting the ControlSource property of an unbound text box to:

=[ClassCode] & "-" & Format([Drawing Number],"00000")

Or in a query you can put the following in the 'field' row of a blank column
in query design view:

FullDrawingNumber:[ClassCode] & "-" & Format([Drawing Number],"00000")

This will return a column named FullDrawingNumber in the query, to which you
can bind a text box control on a form or report.

One caveat: in a multi-user environment on a network, if two or more users
try to add a new record for the same class code simultaneously the first one
to save the record will succeed, but the subsequent ones will get an error
because of the key violation. If this could be a problem the error can be
handled in the form's Error event procedure.

As the class codes seem to be in upper case you can make sure that this is
the case even if the user enters them in lower or mixed case by putting the
following code in the ClassCode control's KeyPress event procedure:

Dim strCharacter As String

' Convert ANSI value to character string.
strCharacter = Chr(KeyAscii)
' Convert character to upper case, then to ANSI value.
KeyAscii = Asc(UCase(strCharacter))

If you are unfamiliar with entering code in event procedures this is how its
done:

Select the control in form design view and open its properties sheet if its
not already open. Then select the relevant event property in the properties
sheet. Click on the 'build' button; that's the one on the right with 3 dots.
Select 'Code Builder' in the dialogue, and click OK. The VBA window will
open at the event procedure with the first and last lines already in place.
Enter the lines of code between these two existing lines.

Ken Sheridan
Stafford, England

tennmo said:
I am very much a beginner with Access so don't think to bad of me when you
read my question. My boss wants me to create a drawing number database where
the number includes a class code data field with a seq. auto generated number
for each class code. Here are some examples: BRKT-10000, BRKT-10001,
FAST-10000, FAST-10001.

How do I do this? Might be very easy but when you don't know anything, very
hard.

Thanks in advance for any help you can give.
 
John,

The numbers will be entered into a form. I would like the number generated
as soon as a class code is selected. I will not be importing anything. This
database will serve as "from this point forward". The numbers generated will
be increased by one each time a new number is generated for a given class
code.

Thanks so much for the reply.
 
John,

The numbers will be entered into a form. I would like the number generated
as soon as a class code is selected. I will not be importing anything. This
database will serve as "from this point forward". The numbers generated will
be increased by one each time a new number is generated for a given class
code.

In that case you could use code like this in the AfterUpdate event of the
Class control. Air code, untested, using my guesses at your table, field, and
control names; adapt as needed:

Private Sub cboClass_AfterUpdate()
' Only assign a seq if you're on the new record
' and the seq has not already been entered
If Me.NewRecord And IsNull(Me.txtClassSeq) Then
Me!txtClassSeq = NZ(DMax("[ClassSeq]", "[MyTable]", _
"[Class] = '" & Me!cboClass & "'")) + 1
End If
End Sub

Test, test, test and adapt as needed.
 
Thanks for the help. Looks like I have a lot to learn.

John W. Vinson said:
John,

The numbers will be entered into a form. I would like the number generated
as soon as a class code is selected. I will not be importing anything. This
database will serve as "from this point forward". The numbers generated will
be increased by one each time a new number is generated for a given class
code.

In that case you could use code like this in the AfterUpdate event of the
Class control. Air code, untested, using my guesses at your table, field, and
control names; adapt as needed:

Private Sub cboClass_AfterUpdate()
' Only assign a seq if you're on the new record
' and the seq has not already been entered
If Me.NewRecord And IsNull(Me.txtClassSeq) Then
Me!txtClassSeq = NZ(DMax("[ClassSeq]", "[MyTable]", _
"[Class] = '" & Me!cboClass & "'")) + 1
End If
End Sub

Test, test, test and adapt as needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro for splitting lines 2
Row Numbering 5
Auto number 3
Looking for a macro or formula to use in excel. 9
Auto picking numbers 5
MS Access Customizing an Auto Number 4
stop auto number 8
Auto Number 3

Back
Top