MS Access中的计数器字段,如何生成?

时间:2011-10-25 19:23:21

标签: sql ms-access

如何生成像0001A,0002A这样的计数器字段...因为在标准中它是0,1,2,3,4 ....如何更改?

3 个答案:

答案 0 :(得分:2)

最简单的解决方案是使用标准自动编号字段(长整数)。让Access为您维护这些值。然后,只要您需要“0001A”格式的值,请使用Format()函数添加前导零,并连接“A”。

这很简单。如果您的自动编号字段是名为ID,则可以使用此查询进行转换:

SELECT Format(ID, "0000") & "A" AS formatted_ID
FROM YourTable;

类似地,您可以将相同的表达式应用于表单或报表上文本框的控件源属性。

答案 1 :(得分:2)

添加@ HansUp的优秀答案,您可以隐藏IDENTITY列,同时使用SQL VIEW公开格式化列:然后您可以撤消表上的权限以便用户工作使用VIEW并且不要“看到”表格,例如演示:

复制+粘贴到任何VBA模块,不需要引用或访问UI /对象模型,在临时文件夹中创建新的mdb,例如使用Excel:

Sub YourView2()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")

  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"

    With .ActiveConnection

      Dim Sql As String

      Sql = _
      "CREATE TABLE YourTable ( " & _
      "ID INTEGER IDENTITY(1, 1) NOT NULL UNIQUE, " & _
      "data_col VARCHAR(20) NOT NULL);"
      .Execute Sql

      Sql = _
      "CREATE VIEW YourView AS " & _
      "SELECT FORMAT$(ID, '0000') & 'A' AS formatted_ID, " & vbCr & _
      "       data_col " & vbCr & _
      "  FROM YourTable;"
      .Execute Sql

      Sql = _
      "INSERT INTO YourView (data_col) VALUES ('one');"
      .Execute Sql      
      Sql = _
      "INSERT INTO YourView (data_col) VALUES ('day');"
      .Execute Sql      
      Sql = _
      "INSERT INTO YourView (data_col) VALUES ('when');"
      .Execute Sql      

      Sql = "SELECT * FROM YourView;"

      Dim rs
      Set rs = .Execute(Sql)
      MsgBox rs.GetString

    End With
    Set .ActiveConnection = Nothing
  End With
End Sub

  

如果您包含DDL GRANT / REVOKE样本来管理权限,我认为这会更好

以下是更新后的代码:

Sub YourView2()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  Kill Environ$("temp") & "\DropMeToo.mdw"
  On Error GoTo 0

  ' Create workgroup and db
  Dim cat As ADOX.Catalog
  Set cat = CreateObject("ADOX.Catalog")
  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Jet OLEDB:Engine Type=4;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMeToo.mdw;" & _
        "Jet OLEDB:Create System Database=-1"
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Jet OLEDB:Engine Type=4;" & _
      "Data Source=" & _
      Environ$("temp") & "\DropMe.mdb;" & _
      "Jet OLEDB:System Database=" & _
      Environ$("temp") & "\DropMeToo.mdw;"

    ' Add table with data and user with privileges
    With .ActiveConnection

      Dim Sql As String

      Sql = _
      "CREATE TABLE YourTable ( " & _
      "ID INTEGER IDENTITY(1, 1) NOT NULL UNIQUE, " & _
      "data_col VARCHAR(20) NOT NULL);"
      .Execute Sql

      Sql = _
      "CREATE VIEW YourView AS " & _
      "SELECT FORMAT$(ID, '0000') & 'A' AS formatted_ID, " & vbCr & _
      "       data_col " & vbCr & _
      "  FROM YourTable WITH OWNERACCESS OPTION;"
      .Execute Sql

      .Execute "CREATE USER onedaywhen pwd Chri5tma5;"
      .Execute "GRANT ALL PRIVILEGES ON YourView TO onedaywhen;"
      .Execute "REVOKE ALL PRIVILEGES ON YourTable FROM onedaywhen;"

    End With
  End With

  ' Test user can connect
  Dim con As ADODB.Connection
  Set con = New ADODB.Connection
  With con
    .ConnectionString = _
      "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Jet OLEDB:Engine Type=4;" & _
      "Data Source=" & _
      Environ$("temp") & "\DropMe.mdb;" & _
      "Jet OLEDB:System Database=" & _
      Environ$("temp") & "\DropMeToo.mdw;" & _
      "User ID=onedaywhen;Password=pwd;"
    .Open

    On Error Resume Next

    ' Attempt to insert to table (no privileges)
    Sql = _
    "INSERT INTO YourTable (data_col) VALUES ('one');"
    .Execute Sql
    If Err.Number <> 0 Then
      MsgBox _
          Err.Number & ": " & _
          Err.Description & _
          " (" & Err.Source & ")"
    End If
    On Error GoTo 0

    Dim rs

    On Error Resume Next

    ' Attempt to read table (no privileges)
    Sql = _
    "SELECT * FROM YourTable;"
    Set rs = .Execute(Sql)
    If Err.Number <> 0 Then
      MsgBox _
          Err.Number & ": " & _
          Err.Description & _
          " (" & Err.Source & ")"
    End If
    On Error GoTo 0

    ' From here, work only with VIEW
    Sql = _
    "INSERT INTO YourView (data_col) VALUES ('one');"
    .Execute Sql

    Sql = _
    "INSERT INTO YourView (data_col) VALUES ('day');"
    .Execute Sql

    Sql = _
    "INSERT INTO YourView (data_col) VALUES ('when');"
    .Execute Sql

    Sql = "SELECT * FROM YourView;"

    Set rs = .Execute(Sql)
    MsgBox rs.GetString

    Set con = Nothing
  End With
End Sub

答案 2 :(得分:1)

一种解决方案,仅适用于表单!

  1. 创建一个GetId()函数来计算您的计数器(通常使用DMax)
  2. 使用表单中的Before insert事件使用GetId()
  3. 设置字段的值 缺点:在多用户环境中,如果另一个User2在User1之后开始addind一条记录,但在User1保存之前保存它,则会出现重复的问题。您需要使用FormError事件重新生成ID并恢复保存过程。