B
BruceM
I have a main table for EquipmentType, with a child table for EquipmentItem.
The equipment is identified in EquipmentType by department code and an
incremented number. Equipment for the machining department is numbered
M-0001, M-0002, etc. For welding, W-0001, etc.
The related EquipmentItem table contains a letter. Let's say that M-0001 is
a clamp, and that there are three of them. They are numbered M-0001-A,
M-0001-B, and M-0001-C. When a new clamp is needed, it will be M-0001-D.
In order that the incrementing may be done automatically, the letter is
actually stored as a number, and converted to a letter for display. The
outline of how I go about this is as follows:
varSeqLetter and varSeqCount are declared as variants in the subform's code
module Declarations. strSeqNumber and strDeptCode are declared as strings.
In the subform's Current event:
' Find the highest SeqLetter for this equipment
varSeqLetter = DMax("SeqLetter", "tblEquipItem", _
"EI_EquipID = " & Me.Parent.EquipID)
' Determine whether this is the first record using this EquipID
varSeqCount = DCount("*", "tblEquipItem", _
"EI_EquipID = " & Me.Parent.EquipID)
strSeqNumber = Format(Me.Parent.SeqNumber, "0000")
strDeptCode = Me.Parent.T_DeptCode
' User-defined function in the subform's module
SequenceLetter
Public Function SequenceLetter()
' Chr(65) = "A", so 1 is rendered as "A", 2 as "B", etc.
SequenceLetter = Chr(varSeqLetter + 64)
' Place the full number into an unbound text box on the main form
Me.Parent.txtEquipNumber = "Equipment ID #" & _
strDeptCode & "-" & strSeqNumber & "-" & _
SequenceLetter
End Function
Perhaps I should explain that the actual code is more involved. For
instance, SequenceLetter contains provisions to go from "Z" to "AA", then
all the way to "ZZ"; otherwise I would just use the expression
Chr(varSeqLetter + 64) rather than creating the function.
This works as intended in this situation. However, this number will need to
appear in many different forms and reports. The form/subform structure as
described presents the information in this way:
M-0001
A
B
C
M-0002
A
W-0001
A
Another way of presenting the information is to combine the two tables into
one query (I will call it qryListing) so that there is no distinction
between main records and related records:
M-0001-A
M-0001-B
etc.
I could create an expression in the query to convert M, 1, and 1 to
M-0001-A, etc., but I wonder if it is the best way, or whether instead I
should use a user-defined function to do the combining. If I use a
user-defined function I suppose it would include arguments along the lines
of:
FullNumber(DeptCode as String, SeqNumber as Long, SeqLetter as Long) as
String
but I am wandering into rather new territory with this approach. I have
been experimenting, and can pass the variables to the function in a
particular situation such as in the subform code, but I am having trouble
coming up with a way to pass the variables in a variety of situations, such
as in the query qryListing as described above.
The equipment is identified in EquipmentType by department code and an
incremented number. Equipment for the machining department is numbered
M-0001, M-0002, etc. For welding, W-0001, etc.
The related EquipmentItem table contains a letter. Let's say that M-0001 is
a clamp, and that there are three of them. They are numbered M-0001-A,
M-0001-B, and M-0001-C. When a new clamp is needed, it will be M-0001-D.
In order that the incrementing may be done automatically, the letter is
actually stored as a number, and converted to a letter for display. The
outline of how I go about this is as follows:
varSeqLetter and varSeqCount are declared as variants in the subform's code
module Declarations. strSeqNumber and strDeptCode are declared as strings.
In the subform's Current event:
' Find the highest SeqLetter for this equipment
varSeqLetter = DMax("SeqLetter", "tblEquipItem", _
"EI_EquipID = " & Me.Parent.EquipID)
' Determine whether this is the first record using this EquipID
varSeqCount = DCount("*", "tblEquipItem", _
"EI_EquipID = " & Me.Parent.EquipID)
strSeqNumber = Format(Me.Parent.SeqNumber, "0000")
strDeptCode = Me.Parent.T_DeptCode
' User-defined function in the subform's module
SequenceLetter
Public Function SequenceLetter()
' Chr(65) = "A", so 1 is rendered as "A", 2 as "B", etc.
SequenceLetter = Chr(varSeqLetter + 64)
' Place the full number into an unbound text box on the main form
Me.Parent.txtEquipNumber = "Equipment ID #" & _
strDeptCode & "-" & strSeqNumber & "-" & _
SequenceLetter
End Function
Perhaps I should explain that the actual code is more involved. For
instance, SequenceLetter contains provisions to go from "Z" to "AA", then
all the way to "ZZ"; otherwise I would just use the expression
Chr(varSeqLetter + 64) rather than creating the function.
This works as intended in this situation. However, this number will need to
appear in many different forms and reports. The form/subform structure as
described presents the information in this way:
M-0001
A
B
C
M-0002
A
W-0001
A
Another way of presenting the information is to combine the two tables into
one query (I will call it qryListing) so that there is no distinction
between main records and related records:
M-0001-A
M-0001-B
etc.
I could create an expression in the query to convert M, 1, and 1 to
M-0001-A, etc., but I wonder if it is the best way, or whether instead I
should use a user-defined function to do the combining. If I use a
user-defined function I suppose it would include arguments along the lines
of:
FullNumber(DeptCode as String, SeqNumber as Long, SeqLetter as Long) as
String
but I am wandering into rather new territory with this approach. I have
been experimenting, and can pass the variables to the function in a
particular situation such as in the subform code, but I am having trouble
coming up with a way to pass the variables in a variety of situations, such
as in the query qryListing as described above.