0

我需要能够创建一个唯一的 ID#,它使用在文本框中选择的日期。当该月/年有多个记录时,我需要它来计算该月和该年的记录数,并创建一个加一的 ID。因此,如果 2013 年 10 月有 4 个案例,我需要它来创建 ID,如下所示:

  • CEF-1013-1
  • CEF-1013-2
  • CEF-1013-3
  • CEF-1013-4

如果有以下代码:

Private Sub Text0_AfterUpdate()
    'Creates a custom case number based on date and how many have been entered in that month/year
    Dim caseNum As String
    Dim caseCount As Integer
    caseNum = Format(Forms!frmEnterCase!Text0, "mmyy")
    caseCount = DCount("CaseID", "case", Format(Table!Case!CaseID, "mmyy") = caseNum)
    caseNum = "CEF-" & caseNum & "-" & (caseCount + 1)
    Forms!frmEnterCase!Text31 = caseNum

End Sub
4

2 回答 2

2

你应该能够做这样的事情:

Private Sub Text0_AfterUpdate()
    Forms!frmEnterCase!Text31 = GetNextCaseNum()
End Sub

Public Function GetNextCaseNum() As String
    Dim rs as DAO.Recordset, sSQL as String
    sSQL = "SELECT TOP 1 CaseID FROM case WHERE CaseID LIKE 'CEF-" & Format(Date, "mmyy") & "-*' ORDER BY CaseID DESC"
    Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
    If Not (rs.EOF and rs.BOF) Then
        GetNextCaseNum = "CEF-" & Format(Date, "mmyy") & Format(Cstr(Clng(Right(rs("CaseID"), 2))+1), "00")
    Else
        GetNextCaseNum = "CEF-" & Format(Date, "mmyy") & "-01"
    End If
    rs.close
    Set rs = Nothing
End Function

请注意,我没有测试代码。它可能需要一些调整/调试。基本概念仍然成立。

在这种情况下,最好制作用于获取/创建 ID 的函数。

另外,我可能会提到,我通常会为这样的表分配一个无意义的自动编号字段作为主键,然后仍然可能使用类似于您在此处所做的有意义的 CaseCode 字段。数字主键性能更好。虽然您确实看到了很多,但使用有意义的主键字段通常被认为是一种糟糕的设计实践。

于 2013-10-07T21:04:57.740 回答
1

For what it's worth, I would recommend that you keep the sequence number you derive in a separate numeric field, something like this:

CasePK  CaseDate    CaseMonthSeq
------  ----------  ------------
   101  2013-10-01             1

That way you can streamline the process to derive new sequence numbers, and you can easily build the "CaseID" string from its constituent parts. (That is, it's always easier to glue strings together than it is to split them apart.)

The following test code illustrates how you could derive the sequence number when you insert the record into the table. Because the derivation is done inside the INSERT statement it should be done within an implicit transaction and therefore be suitable for a multi-user environment

Sub CaseSeqTest()
Dim cdb As DAO.Database, SQL As String
Dim dtCaseDate As Date
dtCaseDate = DateSerial(2013, 10, 3)  ' sample date for testing
Set cdb = CurrentDb
SQL = _
        "INSERT INTO Cases (" & _
                "CaseDate, " & _
                "CaseMonthSeq " & _
            ") VALUES (" & _
                "#" & Format(dtCaseDate, "yyyy-mm-dd") & "#, " & _
                "Nz(DMax(""CaseMonthSeq"", ""Cases"", ""Format(CaseDate, """"yymm"""") = """"" & Format(dtCaseDate, "yymm") & """""""), 0) + 1 " & _
            ")"
Debug.Print SQL
cdb.Execute SQL, dbFailOnError
Set cdb = Nothing
End Sub

The Debug.Print statement simply prints the SQL command to be executed, in this case...

INSERT INTO Cases (CaseDate, CaseMonthSeq ) VALUES (#2013-10-03#, Nz(DMax("CaseMonthSeq", "Cases", "Format(CaseDate, ""yymm"") = ""1310"""), 0) + 1 )

...and the record is inserted into the table as follows:

CasePK  CaseDate    CaseMonthSeq
------  ----------  ------------
   101  2013-10-01             1
   102  2013-10-03             2

If you want to display the CaseID then you can always piece it together "on the fly", like this...

SELECT
    CasePK,
    CaseDate,
    "CEF-" & Format(CaseDate, "yymm") & "-" & CaseMonthSeq AS CaseID
FROM Cases

...returning

CasePK  CaseDate    CaseID    
------  ----------  ----------
   101  2013-10-01  CEF-1310-1
   102  2013-10-03  CEF-1310-2

...or, since you are using Access 2010 (or later) you could alter the [Cases] table to create a [CaseID] as a Calculated field using the expression

"CEF-" & Right(Year([CaseDate]),2) & IIf(Month([CaseDate])>9,"","0") & Month([CaseDate]) & "-" & [CaseMonthSeq]

so you could just retrieve [CaseID] directly and wouldn't keep having to re-create the logic to assemble the CaseID every time you wanted to use it in a query or report.

于 2013-10-08T12:35:26.003 回答