【问题标题】:Creating an MDB table using VBA and ADO, how to automatically use UUID as the primary key?使用VBA和ADO创建MDB表,如何自动使用UUID作为主键?
【发布时间】:2020-07-13 19:22:42
【问题描述】:

我正在 mdb 文件中创建一个表。

目前我的函数添加了一个自动递增的主键。

我希望使用 UUID 创建新行,而不是顺序整数。

这个可以吗?

这是我当前工作的 MDBCreateTable 函数。

Function MDBCreateTable(myPath As String, TableName As String, ColumnDefinition As String) As Boolean

    ' ColumnDefinition structure     Column1;;Datatype::Column2;;Datatype
    Dim Columns() As String
    Columns = Split(ColumnDefinition, "::")

    Dim xCat As ADOX.catalog
    Dim xTable As ADOX.Table

    'Instantiate the ADOX-object.
    Set xCat = New ADOX.catalog
    Set xTable = New ADOX.Table

    On Error GoTo Failed

    Dim ConnectionString As String
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myPath & ";"
    xCat.ActiveConnection = ConnectionString

    'Name the table.
    xTable.Name = TableName
     'Create the field which also is the Primary Key (PK) field for the Table.
    xTable.Columns.Append "ID", adInteger
     'In order to access the properties we need to set the Parent Catalog.
    xTable.ParentCatalog = xCat
    xTable.Columns("ID").Properties("AutoIncrement").Value = True
     'Append the PK.
    xTable.Keys.Append "PrimaryKey", adKeyPrimary, "ID"

    For x = 0 To UBound(Columns)
        If inStB(Columns(x), ";;") Then
            Select Case Split(Columns(x), ";;")(1)
                Case "integer"
                    xTable.Columns.Append Split(Columns(x), ";;")(0), adInteger
                Case "decimal"
                    xTable.Columns.Append Split(Columns(x), ";;")(0), adNumeric
                Case "date"
                    xTable.Columns.Append Split(Columns(x), ";;")(0), adDate
                Case Else
                    xTable.Columns.Append Split(Columns(x), ";;")(0), adWChar
            End Select
        End If
    Next x

    xCat.Tables.Append xTable
    MDBCreateTable = True

Failed:
End Function

作为记录,此功能基于以下论坛主题

http://www.ozgrid.com/forum/printthread.php?t=40365

这是我发现的关于使用 UUID 作为主键的文本

https://tomharrisonjr.com/uuid-or-guid-as-primary-keys-be-careful-7b2aa3dcb439

另外,我是使用 MDB 文件的新手,我目前将所有数据存储在 excel 工作表中,这是我第一次尝试使用 MDB 文件。

所以我从创建数据库文件和下一次创建表开始。

(接下来我将创建从 MDB 文件中读取和写入的函数) (我最终也将迁移到 vb.net,我希望 ADO api 在 vb.net 中是相似的,并且我不是白学它。我很难在 DAO、ADO 和 ADODB 之间做出选择,最后我随机挑选的,我想) 谢谢!

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您必须定义字段/列的类型,如底部here 所示:

    ADODB.Connection cn = new ADODB.Connection(); 
    ADOX.Column clx = new ADOX.Column();
    ADOX.Catalog cat = new ADOX.Catalog(); 
    ADOX.Table tblnam;
    cn.CursorLocation = ADODB.CursorLocationEnum.adUseClient;cn.Open(sExternalDBConnectionString, "", "", 0); 
    cat.ActiveConnection = cn;
    clx.ParentCatalog = cat;
    clx.Type = ADOX.DataTypeEnum.adGUID; 
    clx.Name = "IDField";
    clx.Properties["AutoIncrement"].Value = false; 
    clx.Properties["Fixed Length"].Value = true;
    clx.Properties["Jet OLEDB:AutoGenerate"].Value = true;clx.Properties["Jet OLEDB:Allow Zero Length"].Value = true; 
    tblnam = cat.Tables[sExternalDBTableName];
    tblnam.Columns.Append(clx, ADOX.DataTypeEnum.adGUID, 16);
    cn.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2015-10-27
      • 1970-01-01
      相关资源
      最近更新 更多