2014-12-18 21 views
0

我想創建一個新的表,在vba上有幾個字段用於Access 2000.我能夠創建表和字段數據類型的字段是文本但我需要它是數字。當我將數據類型從文本更改爲數字時,它會給我提供錯誤數據類型轉換錯誤。任何幫助,將不勝感激。謝謝在vba Access 2000中創建新表和字段時獲取錯誤數據類型轉換錯誤

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 

回答

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場後,你應該不需要索引或設置爲無重複。所有PK都以這種方式工作。

+0

感謝幫助貝絲。你能幫我解決一個問題嗎?如何設置主鍵並將索引設置爲是(無重複) – CodeEngine

+0

非常感謝您的幫助!你太棒了 ! – CodeEngine