如何修改Notes代码将REQ替换为创建年份生成年序自增编号
Modified Querysave Code for Year-Based Sequential IDs
Here's the updated code that generates IDs in the YYYY-00# format, with sequential numbers resetting each year:
Sub Querysave(Source As Notesuidocument, Continue As Variant) Dim docCount As Long Dim viewCollection As NotesViewEntryCollection Dim viewEntry As NotesViewEntry Dim currentYear As Integer Dim db As NotesDatabase Dim view As NotesView ' Get the current database and target view (replace "YourViewName" with your actual view name) Set db = Source.Parent.Database Set view = db.GetView("YourViewName") If source.IsNewDoc Then ' Get the document's creation year (uses actual document creation date, not current time) currentYear = Year(Source.Document.Created) ' Get only entries from the current year (requires view to have a first column with Year(Created) as numeric) Set viewCollection = view.GetAllEntriesByKey(currentYear) docCount = viewCollection.Count ' If no documents exist for the year, start at 1 If docCount = 0 Then docCount = 1 Else docCount = docCount + 1 End If ' Format ID as YYYY-00# (adjust "000" to "0000" for 4-digit sequential numbers) Call source.FieldSetText("ReqNo", CStr(currentYear) & "-" & Format(docCount, "000")) End If End Sub
Key Changes & Explanations:
- Replaced fixed "REQ" prefix with creation year: We pull the year directly from the document's
Createdfield usingYear(Source.Document.Created)—this is more accurate than usingNow()because it uses the actual time the document was created, not just when it's saved. - Year-specific sequential counting: Instead of counting all documents in the view, we filter to only count entries from the current year with
view.GetAllEntriesByKey(currentYear). This ensures numbers reset to001at the start of each new year, matching your requirement. - Adjusted number padding: Swapped
Format(docCount,"00000#")forFormat(docCount,"000")to get the 3-digit padded sequential number (e.g., 5 becomes 005, 27 becomes 027). - Explicit database/view reference: Added clear code to fetch the database and target view—don't forget to replace
"YourViewName"with the actual name of your view.
Critical Setup Note:
For the GetAllEntriesByKey method to work, your target view needs a first column with the formula Year(Created) (set as a numeric value, not text). This column should be sorted (ascending or descending doesn't matter) so the view can efficiently filter entries by year. If you can't modify the view, a full-text search for the current year is an alternative, but the key-based filter is far more efficient.
内容的提问来源于stack exchange,提问作者wardokdee
相关产品推荐
相关产品推荐

