473,508 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Auto Generate Custome Number While Saving (Vb 6.0 and MS Access)

Hiren Joshi
30 New Member
Hi All,

VB 6.0 and MS Access

While Saving Record, I want Number to be Generated in following Format

140208-001, 140208-002, 140208-003 . . . etc...

You must have noticed that 140208 is today's Date and After the - Last 3 digits are number.

Now what I want is, lets say If i save 87 Records today it should be 140208-087 and next day (tomorrow) numbers should be like 150208-001, 150208-002 etc.....

Please let me know if it possible then how? I m using the following code for Auto Generation of the Number but in different format.

=========My Code Goes Here =========

Dim C, E As String
Dim D As Integer
C = "ABCDEF"

If Xrs.RecordCount = 0 Then

D = 1

Do While Not Xrs.EOF
If Xrs("QNo") > D Then

End If

Xrs.MoveNext

D = D + 1

Loop

If D >= 1 And D < 10 Then
E = C & "0000" & D
Text1.Text = E
Else
If D >= 10 And D < 100 Then
E = C & "000" & D
Text1.Text = E
Else
If D >= 100 And D < 1000 Then
E = C & "00" & D
Text1.Text = E
Else
If D >= 1000 And D < 10000 Then
E = C & "0" & D
Text1.Text = E
Else
E = C & D
Text1.Text = E
End If
End If
End If
End If

Xrs.AddNew
Xrs("QNo").Value = E

Text1.Text = Xrs.Fields("QNo")

====== Code Ends Here ===========

Regards,

Hiren
Feb 14 '08 #1
13 12027
debasisdas
8,127 Recognized Expert Expert
For the first part use system date . You just need to format the date.

For the second part findout the number of records inserted in that day .You number should be +1.

Next you need to format it as desired.

You need to Store a date field in your table.
Feb 14 '08 #2
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Code Something like this :

Expand|Select|Wrap|Line Numbers
  1. Dim NextNum As String
  2. Dim TNo As Integer
  3. Dim RST As New ADODB.RecordSet
  4. Dim sSQL As String
  5. NextNum = Format(Date, "ddmmyy")
  6. sSQL = "Select Max(MyField) From MyTable Where MyField Like '"  _
  7.     & NextNum & "*'"
  8. RST.Open sSQL,Conn,adOpenStatic,adOpenLockReadOnly
  9. TNo = 0
  10. If Not RST.EOF Then
  11.     RST.MoveFirst
  12.     sSQL = Trim(RST(0) & "")
  13.     If Trim(sSQL) <> "" Then
  14.         TNo = Val(Right(sSQL,3))
  15.     End If
  16.     RST.Close
  17. End If
  18. TNo = TNo +1
  19. NextNum = NextNum & "-" & Format(TNo,"000")
  20.  
Regards
Veena
Feb 14 '08 #3
Hiren Joshi
30 New Member
Yes I thought of the same thing, but having confusion of how to Increment Daily Records... I mean how to check If today it is the first record or 5th or 87th?

I am going to try it today, if you can give me any suggestion on above will be helpful.

Thanks
Feb 14 '08 #4
Hiren Joshi
30 New Member
Thanks Veena,

I will try this as well today and Let both of you and Debasis know the outcome tomorrow....

Thanks Once again

Hiren
Feb 14 '08 #5
Hiren Joshi
30 New Member
Ok I tried it, but what's happening is I am able to save first record of the day, then while saving second record it gives duplicate value error as this no. in the table is a primary key.

I managed to save 1 record yesterday 140208-001 and 1 record today 150208-001, but I am not able to save second record in the same day.

Here is my code

======== My Code (VB) ===========

Dim NextNum As String
Dim TNo As Integer
Dim RST As New ADODB.Recordset
Dim sSQL As String
NextNum = Format(Date, "ddmmyy")

sSQL = "Select Max(TestNo) From Test Where TestNo Like '" _
& NextNum & "*'"
RST.Open sSQL, Con, adOpenStatic, adLockOptimistic

If Not RST.EOF Then
RST.MoveFirst
sSQL = Trim(RST(0) & "")
If Trim(sSQL) <> "" Then
TNo = Val(Right(sSQL, 3))
End If
RST.Close
End If
TNo = TNo + 1
NextNum = NextNum & "-" & Format(TNo, "000")

======== Code Ends =============

I am getting Following Error:

"The Changes You requested to the table were not successful because they would create duplicate values in the index, Primary key or relationship....."
Feb 15 '08 #6
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Keep a BreakPoint and check at the end of the above Code what is the value of variable "NextNum "....? and "TNo" ...?
also check what is RST(0)...

What is your Backend Database...?
If SQLServer or Oracle , Replace * With % in SQL Statement..

Regards
Veena
Feb 15 '08 #7
Hiren Joshi
30 New Member
TNO = 1
NextNum = "-001"

its not increasing TNO
Feb 15 '08 #8
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Keep a Break point and check from the beginnig.. What Database?

REgards
Veena
Feb 15 '08 #9
Hiren Joshi
30 New Member
Database is MS Access 2003... Whats happening is, For Every record of the same day it doesnt pass No.2, TNO is always 1 and thats why its giving problem.

I tried through Break Point using stop command.

It doesnt increment TNO.... Always Makes TNO 0+1 = 1

Regards

Hiren
Feb 15 '08 #10
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

VB6 or VBA...?
My Code is for VB6.. not sure it will work for VBA or not..
what is RST(0)..?

REgards
Veena
Feb 15 '08 #11
Hiren Joshi
30 New Member
I am using VB 6 as well...

There is something very small we are missing out.... I have tried all possibilities...

if you are on Yahoo or MSN, Can you please add me on your messanger so that it will be faster for us to communicate... My ID ismail id removedin both.

Regards

Hiren
Feb 15 '08 #12
Hiren Joshi
30 New Member
Veena,

I have mailed u the code and table please check your mail

Hiren
Feb 15 '08 #13
debasisdas
8,127 Recognized Expert Expert
Veena,

I have mailed u the code and table please check your mail

Hiren
Now you check your PM.
Feb 15 '08 #14

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

Similar topics

2
8273
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
6
3805
by: Sebastien | last post by:
Hi, I am building a products database, linking sales and production. Each part has a unique sales Stock Code and Production Number. The sales stock code is a combination of letters and numbers...
2
2265
by: SalimShahzad | last post by:
Dear Gurus, i had written following codes to auto generate the next claim no Private Const strC = "GCT/02/J/" Private Sub Command1_Click() Dim stra, stre As String Dim intb, intd As Integer...
8
5795
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they...
1
2596
by: gwhite1 | last post by:
I am using the auto-generate fields option because my query can be different based on what the user selects. But I would still like to set the column alignment but I can;t seem to access the...
2
5809
by: G | last post by:
Hello Firends I want a sample code for developing an " auto genearte number " and want to store in database. Please if you find any code please send Thanks in Advance G
1
2118
by: ganesh22 | last post by:
hi... I am doing a project asp.net with C#.my requirement I want to do auto generate password for my application that means when user forget his password when he clicks a link it will auto...
1
1951
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the...
2
4945
by: Jagdish Patil | last post by:
I have project in asp.net with sql server backend. i want to auto generate a number for particular column. with the auto generated number i want to insert some other data in the same row same...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
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...
0
7502
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...
1
5057
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...
0
4716
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
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 ...
0
428
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...

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.