Solved How can I (1 Viewer)

yunhsuan

Member
Local time
Today, 07:10
Joined
Sep 10, 2021
Messages
54
Hello
I want to make floating numbers, composed by date and serial number.
I have succeed to make floating numbers, which serial numbers are reset every day.
Actually, the serial numbers need to be reset every month.
How can I revise the code in this access?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:10
Joined
Oct 29, 2018
Messages
21,467
Hi. Welcome to AWF!

Unfortunately, when I downloaded and tried to open your attachment, I got a warning message that it was corrupted. I can't see any of your objects.
 

plog

Banishment Pending
Local time
Yesterday, 18:10
Joined
May 11, 2011
Messages
11,645
I get a warning as well but can see your objects. However their names aren't using the Latin alphabet (abcdef...) so I can't make sense of anything. So I can just give you general advice:

1. Don't do this. Just use an autonumber and be done with it. The ultimate purpose of a serial numbering system is to provide uniqueness. An autonumber is guaranteed to do this. Yes, it won't be "pretty" or conform to any particluar format, but its easy and serves its purpose. Encoding other data into it (date, ordinality), really does nothing in the big picture--you have a database if that other information is needed you can use the autonumber to look it up and get all that data and more from the database.

2. After you disregard the great advice in #1, you need 2 fields in your table to accomodate this. An immutable date field ([SerialDate]) and a number field ([SerialNumber]). Then you need a function to get the "next" [SerialNumber]. You would pass the function the [SerialDate] of a new serial number, the function would use a DMax(https://www.techonthenet.com/access/functions/domain/dmax.php) to find the highest [SerialNumber] in the database for the day/month of the [SerialDate] value. If its NULL the function returns 1, otherwise the function adds 1 to DMax and returns it and that's your new [SerialNumber].

A couple issues that might arise--records get deleted--that means you might get gaps in your [SerialNumber] values for a day/month and there goes your "pretty" system. Typos and data changes happen to--if [SerialDate] gets changed [SerialNumber] won't, meaning it can appear out of order, or even create duplicates if the [SerialDate] is moved backwards to a time period already using that serial number.

Again, though, go with #1.
 

yunhsuan

Member
Local time
Today, 07:10
Joined
Sep 10, 2021
Messages
54
Sorry...I forgot to change my code to Eng.

Unfortunately, the rule of this serial number is set several years ago. So I am bound to use the advice in #2.

Here is the code I used.
Code:
Private Sub SerialDate_AfterUpdate()
1001 Z = DMax("SerialNumber", "Table1")
1002 If IsNull(Z) = False Then
1003 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & Format(CStr(Val(Right(Z, 3) + 1)), "000")
1004 Else
1005 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & "001"
1006 End If
End Sub

I hope result look like this
1100801001
1100801002
1100802003
1100803004
.
.
.
1100901001
1100901002
(110 is the same as 2021in my country)

But, I don't how to find the max value of the serial number monthly.
 

plog

Banishment Pending
Local time
Yesterday, 18:10
Joined
May 11, 2011
Messages
11,645
Code:
DMax("SerialNumber", "Table1")
...Format(CStr(Val(Right(Z, 3) + 1)), "000")

Do you have a SerialDate field in Table1? If so, you will need to include it in the criteria argument of DMax so your numbers reset each time period. If not, then you need to construct a criteria filter such that you can filter SerialNumber to just those with a similar prefix of the one you are constructing. Much much easier with 2 seperate fields. Much much much much easier to use autonumbers.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:10
Joined
May 7, 2009
Messages
19,230
this will reset every month:
Code:
Private Sub SerialDate_AfterUpdate()
1001 Z = DMax("SerialNumber", "Table1", "Val(Left([SerialDate],3))+1911=Year(Date()) And Mid([SerialDate],4,2) = '" & Format(Date(),"mm") & "'")
1002 If IsNull(Z) = False Then
1003 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & Format(CStr(Val(Right(Z, 3) + 1)), "000")
1004 Else
1005 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & "001"
1006 End If
End Sub
 

yunhsuan

Member
Local time
Today, 07:10
Joined
Sep 10, 2021
Messages
54
Code:
DMax("SerialNumber", "Table1")
...Format(CStr(Val(Right(Z, 3) + 1)), "000")

Do you have a SerialDate field in Table1? If so, you will need to include it in the criteria argument of DMax so your numbers reset each time period. If not, then you need to construct a criteria filter such that you can filter SerialNumber to just those with a similar prefix of the one you are constructing. Much much easier with 2 seperate fields. Much much much much easier to use autonumbers.
There are SerialDate field and SerialNumber field in Table1.
I have corrected my access file below.
 

Attachments

  • Database3.accdb
    480 KB · Views: 330

yunhsuan

Member
Local time
Today, 07:10
Joined
Sep 10, 2021
Messages
54
this will reset every month:
Code:
Private Sub SerialDate_AfterUpdate()
1001 Z = DMax("SerialNumber", "Table1", "Val(Left([SerialDate],3))+1911=Year(Date()) And Mid([SerialDate],4,2) = '" & Format(Date(),"mm") & "'")
1002 If IsNull(Z) = False Then
1003 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & Format(CStr(Val(Right(Z, 3) + 1)), "000")
1004 Else
1005 Me![SerialNumber] = Format(Year(Me![SerialDate]) - 1911, "000") & Format(Me![SerialDate], "mmdd") & "001"
1006 End If
End Sub
I just tried this code.
But the last 3 number keep 001 in the same day.
ex: 1100903001, 1100903001
I hope the last 3 number is serial number.
ex:
1100801001
1100801002
1100802003
1100803004
.
.
.
1100901001
1100901002
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:10
Joined
May 7, 2009
Messages
19,230
your table should have SerialDate (date/time)
and SerialNumber (string).
see this demo.
 

Attachments

  • spclSerial.accdb
    548 KB · Views: 187

yunhsuan

Member
Local time
Today, 07:10
Joined
Sep 10, 2021
Messages
54
your table should have SerialDate (date/time)
and SerialNumber (string).
see this demo.
Both of SerialDate and SerialNumber are in my database.
But, the last three number of SerialNumber should start from 001 every month.
How can I edit it?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:10
Joined
May 7, 2009
Messages
19,230
test this again.
 

Attachments

  • spclSerial.accdb
    548 KB · Views: 378

Users who are viewing this thread

Top Bottom