【问题标题】:Access problem with consecutive CR-00000001连续 CR-00000001 的访问问题
【发布时间】:2022-08-17 08:56:13
【问题描述】:

每张账单我都需要这个连续的“CR-00000001”。

因此,我创建了一个单元格,其中 \"NUMBER_CONSECUTIVE\" 类型为 text,\"CONSECUTIVE\" 类型为计算值。在元素内部

CONSECUTIVE = \"CR\" + \"-\" + [NUMBER_CONSECUTIVE]

并且在表单中,元素 number_consecutive 有一个事件,我在其中放置了代码:

=Cint(DLast(\"NUMBER_CONSECUTIVE\"; \"FAC_BILL\")) + 1

但不工作,我不确定是正确的方法。

你能帮帮我吗,谢谢。

  • 单元格、内部和元素在 Access 中是未知的;你在使用哪个事件?
  • \'code\' 看起来像文本框 ControlSource 中的表达式。这不会将数据保存到表字段。不要相信 Last(或 First)来拉取期望的值。 Max可能是您应该使用的。 \"Inside the element\" 在 Access 世界中没有任何意义。

标签: ms-access


【解决方案1】:

我会以更强大的方式来做。更多的工作,但更可靠。我的解决方案是:

使用以下字段创建表 COUNTERS

  • COUNTER_ID。 PK。自动化
  • HEADER。文本。
  • SERIAL。长。

您使用HEADER="CR"SERIAL=0 创建一条新记录。

然后,每次你想要一个新的账单代码时,你调用以下函数:

Public Function CREATE_SERIAL_CODE(lngCounterId As Long) As String
   
   On Error Goto CREATE_SERIAL_CODE_Err
   
    'Declaration
    Dim dbCurrDB As DAO.Database
    Dim rstCounters As DAO.Recordset

    Dim lngCounterId As Long
    Dim strHeader As String
    Dim lngSerial As Long
    Dim strSerialCode As String
   
    Dim strMessage As String
    Dim strCriterion As String
    
    'Initialization
    Set dbCurrDB = CurrentDB
    Set rstCounters = dbCurrDB.OpenRecordset("COUNTERS",2)
   
    lngCounterId=Nz(lngCounterId)
   
    'Initial Validations
    If lngCounterId=0 Then
        strMessage="Some warning"
        MsgBox strMessage, bla bla bla
        Set dbCurrDB=Nothing
        Exit Function
    End If
   
    'Obtaining the geader and increasing the serial number
    With rstCounters
    
        strCriterion="COUNTER_ID=" & lngCounterId
        
        .FindFirst strCriterion
        
        If .NoMatch=True Then
            strMessage="Some warning"
            MsgBox strMessage, bla bla bla
            Set dbCurrDB=Nothing
            Exit Function
        End If
        
        'Obtain header and current serial number
        strHeader=!HEADER
        lngSerial=nz(!SERIAL)
        
        'Increase serial number
        lngSerial=lngSerial + 1
        
        'Write it on the table
        .Edit
            !SERIAL=lngSerial
        .Update
        
    End With
    
    'Constructing the serial code
    
    'Generate the necessary 0 (based on your example of a 8 digit serial)
    Select Case Len(Cstr(lngSerial))
        Case 1
            strSerialCode = "0000000" & lngSerial
        Case 2  
            strSerialCode = "000000" & lngSerial
        Case 3
            strSerialCode = "00000" & lngSerial
        Case 4
            strSerialCode = "0000" & lngSerial
        Case 5
            strSerialCode = "000" & lngSerial
        Case 6
            strSerialCode = "00" & lngSerial
        Case 7
            strSerialCode = "0" & lngSerial
        Case 8
            strSerialCode = "" & lngSerial
    End select
    
    'Attach the header
    strSerialCode = strHeader & "-" & strSerialCode
    
    CREATE_SERIAL_CODE = strSerialCode
    
    Set dbCurrDB = Nothign
    
    Exit Function
    
CREATE_SERIAL_CODE_Err:
    Set dbCurrDB = Nothign
    MsgBox Err.Description
    
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多