469,306 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

How to create unique serial number based on todays date in format MMDDYY-XXX in VB?

I need to identify parts we buy at our plant. I want to give each a unique serial number that is based on the date. We may have multiple parts on a given date so I want to have the unique serial number in the format:
MMDDYY-XXX where XXX is a unique number that start with 001 for the first item of the day. 002 for the second, etc. but starting over the next day.

When a user enters info in the database, it may already have a number. If not, I want them to push a button and it takes today's date, serializes it based on the number of parts already existing for that date, and place that unique serial number in the PartID box for them. Then they can proceed to enter the other info.

I am learning Access 2007 and have been doing some Macros but at this point I tend to find things others have done and then learn them enough to modify them. I can figure this out eventually but I don't use Access enough to be efficient and I don't have the time.

Ideally, a VB macro done for me...at least enough to query the existing data and create the number would be great.

Does anyone already have somethign like this or can someone get me started???

Thanks!
Dec 17 '10 #1
13 7351
ADezii
8,800 Expert 8TB
Just subscribing for now...

Are you always evaluating against the Current Date, or are you reading a Date Value from elsewhere?
Dec 17 '10 #2
Well..the way I had it set up on the form was that the form field had the date. I am currently setting the date by making it TODAY's date. So I have a macro getting the date from this field so it really shouldn't matter if it is TODAY or another date.

If for some reason it matters, I would go with TODAY's date.

Example:
1. John has a new tool that needs a unique ID number
2. It queries the database to determine if there are any tools logged into the database on this same date
3. Returns the highest unique number in MMDDYY-XXX
4. Assigns a new serial number with todays date and the next serial number.
Dec 17 '10 #3
OK - let me preface this by saying that this is a work in progress so ignore how it is formatted etc

Here is my code. The code is bold is where I am having problems. I need to either create a query that only extracts Serial Numbers with todays date OR it just looks for the last unique serial number that starts with Todays date. Not sure of the best approach.

Private Sub Command0_Click()
Dim strCurrentYear As String
Dim strCurrentDay As String
Dim strCurrentDate As String
Dim strStaticValue As String
Dim strSequentialNo As String
Dim strLastSerialNo As String
Dim strLastSequentialNo As String
Dim strNextSequentialNo As String
Dim fGenerateNextSerialNumber As String
Dim strCurrentDateTest As String

strCurrentDate = Format$(Now(), "mmddyy")

strLastSerialNo = DLookup("[SerialNo]", "tblTest", "[SerialNo] = " & strCurrentDate & "*")
'get ready to extract the Sequential Number
MsgBox strLastSerialNo
strLastSequentialNo = Right(strLastSerialNo, 3) 'extracts serial number

'Generate the Next Sequential Number
strNextSequentialNo = Format(Val(strLastSequentialNo) + 1, "000") 'produces next serial number

'Generate the next, Unique, Serial #
fGenerateNextSerialNumber = strCurrentDate & "-" & strNextSequentialNo
MsgBox fGenerateNextSerialNumber
End Sub[/indent][/indent]
Dec 17 '10 #4
ADezii
8,800 Expert 8TB
I have a work in progress, and will probably Post it sometime this evening...
Dec 17 '10 #5
Wow! Really? Thanks! I can send you my whole file if that helps but it is probably a mess in your eyes.
Dec 17 '10 #6
ADezii
8,800 Expert 8TB
The following Function will:
  1. Search a Table (tblTest) and see if the Date Component of any Serial Number ([Serial#]) is equal to Today's Date:
    Expand|Select|Wrap|Line Numbers
    1. 'Are there any Serial Numbers for today's Date?
    2. intCount = DCount("*", "tblTest", "Left$([Serial#],6) = " & Format$(Date, "mmddyy"))
  2. If there is no Serial Number for Today's Date the Function returns:
    Expand|Select|Wrap|Line Numbers
    1. Else                        'No Serial Number for Today's Date, so set it
    2.   fGenerateSerialNumber = Format$(Date, "mmddyy") & "-001"
    3. End If
    Expand|Select|Wrap|Line Numbers
    1. mmddyy-001
    in this case:
    Expand|Select|Wrap|Line Numbers
    1. 121710-001
  3. If a Serial Number(s) exist for Today's Date, then the Code will create a Recordset filtered for Today's Date sorted by the Numeric Component of the Serial Number, namely the XXX in mmddyy-XXX:
    Expand|Select|Wrap|Line Numbers
    1. strSQL = "SELECT [Serial#] FROM tblTest WHERE Left$([Serial#],6) = '" & Format$(Date, "mmddyy") & "'" & _
    2.          " ORDER BY Val(Right$([Serial#],3)) DESC;"
  4. Notice the Sort Order is Descending on this Numeric Value, so the greatest Value is the 1st Record in the Recordset. The Code does not assume that the greatest Numeric Component of the Serial Number will always be the 'Last' Number for a given Date.
  5. The Function now returns a New Serial Number, incrementing the Numeric Component by +1:
    Expand|Select|Wrap|Line Numbers
    1. If intCount > 0 Then        'Yes there is at least 1, so Increment the Last
    2.   Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    3.   fGenerateSerialNumber = Left$(rst![Serial#], 7) & Format$(Val(Right$(rst![Serial#], 3) + 1), "000")
    4. Else 
  6. The Function in its entirety is listed below, any questions, feel free to ask.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fGenerateSerialNumber() As String
    2. Dim intCount As Integer
    3. Dim rst As DAO.Recordset
    4. Dim strSQL As String
    5.  
    6. 'Are there any Serial Numbers for today's Date?
    7. intCount = DCount("*", "tblTest", "Left$([Serial#],6) = " & Format$(Date, "mmddyy"))
    8.  
    9. 'Create a Recordset based on all Records having a Serial Number consisting of Today's Date.
    10. 'Order By the Numeric Component (XXX in mmddyy-XXX) Descending so greatest Value is the 1st
    11. 'Record in the Recordset
    12. strSQL = "SELECT [Serial#] FROM tblTest WHERE Left$([Serial#],6) = '" & Format$(Date, "mmddyy") & "'" & _
    13.          " ORDER BY Val(Right$([Serial#],3)) DESC;"
    14.  
    15. If intCount > 0 Then        'Yes there is at least 1, so Increment the Last
    16.   Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    17.   fGenerateSerialNumber = Left$(rst![Serial#], 7) & Format$(Val(Right$(rst![Serial#], 3) + 1), "000")
    18. Else                        'No Serial Number for Today's Date, so set it
    19.   fGenerateSerialNumber = Format$(Date, "mmddyy") & "-001"
    20. End If
    21.  
    22. 'Clean Up, if required
    23. If Not rst Is Nothing Then
    24.   rst.Close
    25.   Set rst = Nothing
    26. End If
    27. End Function
Dec 18 '10 #7
I will try it soon.

Thank you very much. I really appreciate it!
Dec 18 '10 #8
Hi.

I tried this and it worked fine when there was no record with todays date but when I put a record with today's date into the table, it gave me the following error:
"Run-Time error 3061
Too few parameters. Expected 1."

On the following line:
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
Dec 21 '10 #9
ADezii
8,800 Expert 8TB
Post what you have for strSQL.
Dec 21 '10 #10
Hello. I just got back from vacation and proofed my code and everything seems to be working fine. Thank you.

Now I have a follow-up question. It turns out that some of our old unique serial numbers have a letter in front of the number which represents our vendor.

When I add these numbers to the table, the code to find how many unique serial numbers start with the date crashes.

It says:
"Run-time error '3464':
Data type mismatch in criteria expression"

and it shows that it stops at the line:
intCount = DCount("*", "ToolingID", "Left$([SerialNo],6) = " & Format$(Date, "mmddyy"))



Full code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command17_Click()
  2. Dim intCount As Integer
  3. Dim rst As DAO.Recordset
  4. Dim strSQL As String
  5. Dim fGenerateSerialNumber As String
  6. Dim Counttest
  7.  
  8.  
  9. 'Are there any Serial Numbers for today's Date?
  10. intCount = DCount("*", "ToolingID", "Left$([SerialNo],6) = " & Format$(Date, "mmddyy"))
  11. MsgBox intCount
  12.  
  13. 'Create a Recordset based on all Records having a Serial Number consisting of Today's Date.
  14. 'Order By the Numeric Component (XXX in mmddyy-XXX) Descending so greatest Value is the 1st
  15. 'Record in the Recordset
  16. strSQL = "SELECT [SerialNo] FROM ToolingID WHERE Left$([SerialNo],6) = '" & Format$(Date, "mmddyy") & "'" & _
  17.          " ORDER BY Val(Right$([SerialNo],3)) DESC;"
  18.  
  19. If intCount > 0 Then        'Yes there is at least 1, so Increment the Last
  20.   Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
  21.   fGenerateSerialNumber = Left$(rst![SerialNo], 7) & Format$(Val(Right$(rst![SerialNo], 3) + 1), "000")
  22. Else                        'No Serial Number for Today's Date, so set it
  23.   fGenerateSerialNumber = Format$(Date, "mmddyy") & "-001"
  24. End If
  25. MsgBox fGenerateSerialNumber
  26. Me.inFormat = fGenerateSerialNumber
  27. Me.ToolingID = fGenerateSerialNumber
  28.  
  29. 'Clean Up, if required
  30. If Not rst Is Nothing Then
  31.   rst.Close
  32.   Set rst = Nothing
  33. End If
  34. End Sub
Jan 6 '11 #11
ADezii
8,800 Expert 8TB
Can you strip this Vendor Code from the Serial Number if it is present via an Update Query?
Jan 7 '11 #12
I guess so. But since the code I have is counting the number of times that the first 6 digits of the serial number matches the 6 numbers of teh date in the MMDDYY format, would that mean I need to do an update query that strips the vendor code off of EVERY serial number?

I'm open to it...just not sure how to go about it.

Why am I getting that error? Will it give me the error if the alpha vendor code is at the END of the unique number? I can probably manipulate my codes to do that.
Jan 7 '11 #13
Actually, I just confirmed that if the letter is not in the first 6 digits, it works fine. I will just need to make sure that there are no letters in the first 6 digits.

Is there an easy way to make sure that when an operator enters a unique number that it gives them an error message if the code has a letter in the first 6 digits???

I can make a note on the form but want to mistake proof it.
Jan 7 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by weiwei | last post: by
1 post views Thread by Paul | last post: by
6 posts views Thread by Arne Beruldsen | last post: by
5 posts views Thread by Jassim Rahma | last post: by
2 posts views Thread by sheperson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.