Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在TableDefs上创建表时出错。显示无效参数的追加_Vba_Ms Access - Fatal编程技术网

Vba 在TableDefs上创建表时出错。显示无效参数的追加

Vba 在TableDefs上创建表时出错。显示无效参数的追加,vba,ms-access,Vba,Ms Access,我正在Access 2013中编写一个程序。如果这是他们第一次安装该程序,它将创建一个外部数据库(稍后链接),并创建所需的表。在附带的代码中,我正在运行一个数组,该数组包含所需的所有表的列表,并使用一个内部(本地)表,该表包含与特定表关联的所有字段。它遍历代码并创建所有字段,但一旦到达行db1.TableDefs.Append newTable,就会抛出无效参数错误。我已经尽可能多地搜索了,但我的设置似乎与所有其他设置相同,无法找出问题所在 Function CreateDataTables()

我正在Access 2013中编写一个程序。如果这是他们第一次安装该程序,它将创建一个外部数据库(稍后链接),并创建所需的表。在附带的代码中,我正在运行一个数组,该数组包含所需的所有表的列表,并使用一个内部(本地)表,该表包含与特定表关联的所有字段。它遍历代码并创建所有字段,但一旦到达行db1.TableDefs.Append newTable,就会抛出无效参数错误。我已经尽可能多地搜索了,但我的设置似乎与所有其他设置相同,无法找出问题所在

Function CreateDataTables()
Dim db As Database
Dim db1 As Database
Dim rst As Recordset
Dim tblCount As Integer
Dim rst1 As Recordset
Dim newTable As TableDef
Dim newField As Field
Dim newIdx As Index
Dim newType As Variant
Dim newLength As Variant
Dim newPK As String
Dim sysTable As String
Dim SQLstr As String
Dim x As Integer
Dim tblNames() As Variant
Stop

SQLstr = "SELECT DISTINCT tableName "
SQLstr = SQLstr + "FROM sysdata;"

Set db = DBEngine(0)(0)
Set rst = db.OpenRecordset(SQLstr, dbOpenSnapshot, dbSeeChanges)
tblCount = rst.RecordCount
'Creating array to house table names
ReDim tblNames(tblCount)
tblNames() = rst.GetRows(tblCount)

Set db = Nothing
Set rst = Nothing

Set db = DBEngine(0)(0)
Set rst = db.OpenRecordset("sysdata", dbOpenSnapshot, dbSeeChanges)
'Create database reference to newly created database in prior function
Set db1 = OpenDatabase(dbFileLoc)

For x = 1 To tblCount
    'Create new tabledef
    Set newTable = db1.CreateTableDef(tblNames(0, (x - 1)))
    'Loop through each record of particular table (named in rst) to create each field
    Do While rst.Fields("tableName") = tblNames(0, (x - 1))
        newType = rst.Fields("typeData")
        newLength = rst.Fields("lengthField")
        newPK = Nz(rst.Fields("PKey"), "No")
        Set newField = newTable.CreateField(rst.Fields("fieldName"))
        With newField
            .Type = newType
            If Not IsNull(newLength) Then .Size = newLength
            'If field is indicated as a PK, this will create the PK
            If newPK = "Yes" Then
                With newTable
                    newField.Attributes = dbAutoIncrField
                    Set newIdx = .CreateIndex("PrimaryKey")
                    newIdx.Fields.Append newIdx.CreateField(tblNames(0, (x - 1)))
                    newIdx.Primary = True
                    .Indexes.Append newIdx
                End With
            End If
        End With
        'Append field
        newTable.Fields.Append newField

        rst.MoveNext
        If rst.EOF Then Exit Do
    Loop
    MsgBox "Table " + newTable.NAME + " create", vbOKOnly  'Placed to verify loop was completed and showing table name
    '''The line below gives the Invalid Argument error
    '''Do not know what is causing this
    db1.TableDefs.Append newTable
    db1.TableDefs.Refresh
Next

我最终发现sysdata表中指定字段类型的数字是错误的。我甚至从MSDN网站上得到了这个消息。我运行了一个循环来确定数据类型的实际数量,然后将它们放在表中,结果成功了!我还发现了与主键有关的代码中的另一个bug。在我添加索引的地方,CreateField中有表名,而不是字段名。一旦我改变了这一点,PK也会按其应有的方式填充