473,797 Members | 2,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

9 New Member
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 8111
ADezii
8,834 Recognized Expert Expert
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
Scott Kaempfe
9 New Member
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
Scott Kaempfe
9 New Member
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 strLastSequenti alNo As String
Dim strNextSequenti alNo As String
Dim fGenerateNextSe rialNumber As String
Dim strCurrentDateT est As String

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

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

'Generate the Next Sequential Number
strNextSequenti alNo = Format(Val(strL astSequentialNo ) + 1, "000") 'produces next serial number

'Generate the next, Unique, Serial #
fGenerateNextSe rialNumber = strCurrentDate & "-" & strNextSequenti alNo
MsgBox fGenerateNextSe rialNumber
End Sub[/indent][/indent]
Dec 17 '10 #4
ADezii
8,834 Recognized Expert Expert
I have a work in progress, and will probably Post it sometime this evening...
Dec 17 '10 #5
Scott Kaempfe
9 New Member
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,834 Recognized Expert Expert
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
Scott Kaempfe
9 New Member
I will try it soon.

Thank you very much. I really appreciate it!
Dec 18 '10 #8
Scott Kaempfe
9 New Member
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.OpenR ecordset(strSQL , dbOpenDynaset)
Dec 21 '10 #9
ADezii
8,834 Recognized Expert Expert
Post what you have for strSQL.
Dec 21 '10 #10

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

Similar topics

1
2420
by: weiwei | last post by:
hi, is there anyway that I can validate my date format in asp?? I have tried like date=now(), that gives me like 5/14/2004, the format should be mmddyy, no space, no dash, and has to include 0 if month is from 1-9, for example 01, 02, like that???? please help me out thanks in advance Wei
1
3366
by: Paul | last post by:
Hi Guys, I have written an application in Visual C#, I wish to add serial number validation dll to the installer, how do I go about doing this. Thanks in advance. Paul Mathebula
6
5246
by: Arne Beruldsen | last post by:
I have a windows app using vb.net 2005. I would like to create a serial number based on a couple of characteristics of the customers computer (computer name, HD number..etc). I don't need anything complicated just a way to generate a 16 character number. Is there an easy way to do this...? Thanks
1
2661
by: Beckster6701 | last post by:
I have a MSAccess database that is linked to a SQL server. I cannot change the SQL server datatype as I am not the only one that uses the database. I'm trying to convert the text field to a valid date or number format so that the field will report correctly. Currently the text field is appears as follows: Test Date: YYYYMMDD (i.e. 20061206) I have used a CDate format, but am getting a type mismatch error when I attempt to run the...
5
6919
by: Jassim Rahma | last post by:
my CPU mac address is BFEBFBFF000006F6 and my software name is : shefa is there any function or encryption way in C# to generate a 24 digits (alpha numeric) from both CPu MAC address and software name?
16
18272
by: malteselemon | last post by:
Using Access 2003, I need to create an automatic Serial Number. Example "7235-E001" "7" is current year "235" is day "-E" is static "001" is sequencial, restarting at "001" each day I set a table field with "=right(Format(Now(),"yy") & Format(Format(Now(),"y"),"000"),4)" to create the year/day number.----I should be able to add &"-E"& to add the static "-E"----I tried to add this all together with the "autonumber", but that obviously...
2
12679
by: sheperson | last post by:
Hi, I have been working for several months on a sofware and now it is finished. I have a problem now and it is this: Because I live in a country which there is no copyright law in it (Iran!!!!). As soon as I give the software to some customers, the software will be copied illegally and I don't want this happen. Now I wonder if anyone could help me on how to create a lock or a serial number for the software. I want my software to...
4
3059
by: naaniibabu | last post by:
I have one number i want to convert in to a date format the size of the value is =8 Char 20091212 i ant to conver it in to a date format my sysyem doent understand wether its adate or not its taking as a number Thanks Nani
1
2106
by: Keshia | last post by:
I have a database that is for keeping track of inventory. I need to have my form for entering new inventory to automatically make a new serial number based on the category of the new item. The serial numbers are currnently like this: AA###### "AA" = Category code "######" = the next number I currently have a category combobox and an item combobox and the serial number txt field. The options in the item combobox change based on the...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.