在vba Access 2000中创建新表和字段时。获取错误数据类型转换错误

时间:2014-12-18 21:11:28

标签: vba ms-access access-vba

我正在尝试在vba上为Access 2000创建一个带有几个字段的新表。当字段的数据类型为Text但我需要它为Number时,我能够创建表和字段。当我将数据类型从Text更改为Number时,它给出了错误数据类型转换错误。任何帮助,将不胜感激。谢谢

Public Sub CreateNewDB()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

'Initialize the Contractor table

Set db = CurrentDb()
Set tdf = db.CreateTableDef("new test table")

'Specify the fields
With tdf

'Create Sequence No Fied
 ' Set fld = .CreateField("Sequence No", dbText, 10) ' This works 
Set fld = .CreateField("Sequence No", dbNumber, Long INteger) ' this gives me an error 
.Fields.Append fld

End With

'save  the table
db.TableDefs.Append tdf
Set fld = Nothing
Set tdf = Nothing

 End Sub

1 个答案:

答案 0 :(得分:2)

尝试dbLong而不是dbNumber。 Reference

这是在新创建的表上设置PK的代码:

Set td = CurrentDb.CreateTableDef(sLocalTableName, dbAttachSavePWD, sRemoteTableName, sConnect)
CurrentDb.TableDefs.Append td
sExec = "CREATE UNIQUE INDEX PK ON [" & sLocalTableName & "]"
For iPK = 0 To iPkfldCount - 1
    sPKfields = sPKfields & "[" & CurrentDb.TableDefs(sLocalTableName).Fields(iPK).Name & "],"
Next
sPKfields = Left(sPKfields, Len(sPKfields) - 1)     ' strip trailing comma
CurrentDb.Execute sExec & " (" & sPKfields & ")"

设置PK字段后,您不需要索引或设置为No Duplicates。所有PK都以这种方式工作。