Automatically generate numbers

China☆狼群 提交于 2020-01-07 03:57:21

问题


I am in need of a tracking number that automatically generates.

I added this in the 'Before Insert' action (see below), but I am having a problem after the number 9.

Me![Tracking#] = Nz(DMax("[Tracking#]", "[TblTrackingNum]"), 0) + 0.01

My tracking number always starts of with 89669. This code works until it reaches the number ten.

The tracking number should do this: 89669.1... 89669.2... 89669.3... 89669.4... 89669.5.....

But after 9 it changes the number to 89670. I need it to say 89669.10.

Any suggestions?


回答1:


Create a field in a table containing only one row of data:

NextTrackNum Long

initialise NextTrackNum to 1.

When generating your number do this:

function generateNumber() as double
    dim nextnum as long
    nextnum = dlookup("NextTrackNum", "yourTable", filter...)
    generateNumber = CDbl("89669." & trim(nextnum))
end function



回答2:


First of all, the tracking number needs to be a string, as already mentioned by Johnny Bones in the comments.
So the final field needs to be a string, but the actual increasing number needs to be a numeric type (in order to properly increase it).

Put an auto-increment field into the table (and make it the primary key).
That will become the increasing part of your tracking number.

Then, there are different ways how to generate your tracking number out of that auto-increment field:

1) Create a query in MS Access that selects from the table and generates the tracking number.

SELECT "89669." & [ID] AS TrackingNumber, YourTable.*
FROM YourTable

Never use the table directly, always SELECT from the query:

SELECT * FROM qryYourTable WHERE ...

2) Create an additional text field called TrackingNumber in the table.
Each time you insert a new row in the table, call a query from your code that fills the field with the correct value:

Public Function FillTrackingNumbers()

    CurrentDb.Execute "UPDATE YourTable SET TrackingNumber = '89669.' & [ID] WHERE TrackingNumber Is Null"

End Function


来源:https://stackoverflow.com/questions/19715913/automatically-generate-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!