【问题标题】:VBA for Access 2003 - DDL for creating access file: setting the Autonumber data typeVBA for Access 2003 - 用于创建访问文件的 DDL:设置自动编号数据类型
【发布时间】:2011-02-20 15:26:56
【问题描述】:

所以我有下面的VB,它在默认工作区中创建一个访问文件,创建一个表,在该表中创建一些字段......只需要知道将第一个数据类型/字段设置为自动编号的语法...... .GUID、Counter 等无法像在 Access SQL 中那样工作

' error handling usually goes here

dim ws as workspace
dim dbExample as database
dim tblMain as TableDef
dim fldMain as Field
dim idxMain as Index

set ws = workspace(0)

set dbExample = ws.CreateDatabase('string file path')

set tblMain = dbExample.CreateTableDef("tblMain")

set fldMain = tblMain.CreateField("ID", 'right here I do not know what to substitute for dbInteger to get the autonumber type to work )

tblMain.Fields.Append fldMain
etc to create other fields and indexes

所以在这一行: 设置 fldMain = tblMain.CreateField("ID", dbInteger) 我需要用 VB 重新转换为自动编号属性的东西替换 dbInteger。 我试过 GUID、Counter、Autonumber、AutoIncrement....不幸的是这些都不起作用

有人知道我在这里缺少的语法吗?

谢谢, 贾斯汀

【问题讨论】:

    标签: database ms-access vba ms-access-2003 ddl


    【解决方案1】:

    请参阅 The access Web 上的 Creating an AutoNumber field from code

    以下是该链接页面的关键行:

    Set db = Application.CurrentDb
    Set tdf = db.TableDefs(strTableName)
    ' First create a field with datatype = Long Integer '
    Set fld = tdf.CreateField(strFieldName, dbLong)
    With fld
        ' Appending dbAutoIncrField to Attributes '
        ' tells Jet that its an Autonumber field '
        .Attributes = .Attributes Or dbAutoIncrField
    End With
    With tdf.Fields
        .Append fld
        .Refresh
    End With
    

    顺便说一句,你所做的不是 DDL。

    【讨论】:

    • 感谢汉斯!我之所以称它为 DDL,是因为我正在从中学习 O'Reilly 的这本书叫它??
    • @Justin Dunno 关于这一点。维基百科对数据定义语言的描述是我认为 DDL 所代表的。在我看来,您的 O'Reilly 示例使用 VBA 创建表作为 DDL 的替代品。
    • 是的,我认为您在再次阅读之后是对的。他们展示了 Access SQL DDL 的示例,然后我认为这是 VBA equiv(可以这么说意味着结束)。我的坏...只是学习这些东西。谢谢你的例子……我在我的子程序中调用了这个函数,它很好用。
    • 您使用的不是 DDL,而是 DAO,它比通用 DDL SQL 更了解 Jet/ACE 的数据类型和结构。为 O'Reilly 调用该代码 DDL 感到羞耻。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多