N
Nelson The Missing Lead
Hi,
I trying to create a table using visual basic, when i try to run it. It
prompt me "No fields defined, can't append Table or Index. Anyone, could
highlight to me what is the problem. My code is as follows:
Public Sub PCreateTable()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim fldTradeDate As DAO.Field
Dim fldStockName As DAO.Field
Dim fldRemarks As DAO.Field
Dim fldCurrency As DAO.Field
Dim fldClose As DAO.Field
Dim fldChange As DAO.Field
Dim fldVolume As DAO.Field
Dim fldHigh As DAO.Field
Dim fldLow As DAO.Field
Dim fldValue As DAO.Field
Dim fldDaysAvg14 As DAO.Field
Set dbs = CurrentDb
On Error Resume Next
'If the table already exists, delete it
dbs.TableDefs.Delete "SGX Individual Historical"
On Error GoTo 0
'Create the table definition in memory
Set tdf = dbs.CreateTableDef("SGX Individual Historical")
Set idx = tdf.CreateIndex("ID")
idx.Primary = True
idx.Required = True
idx.Unique = True
Set fldTradeDate = tdf.CreateField("Trade Date", dbDate)
Set fldStockName = tdf.CreateField("Stock Name", dbText, 50)
Set fldRemarks = tdf.CreateField("Remarks", dbText, 5)
Set fldCurrency = tdf.CreateField("Currency", dbText, 5)
Set fldClose = tdf.CreateField("Close", dbDouble, 3)
Set fldChange = tdf.CreateField("Change", dbDouble, 3)
Set fldVolume = tdf.CreateField("Volume", dbLong)
Set fldHigh = tdf.CreateField("High", dbDouble, 3)
Set fldLow = tdf.CreateField("Low", dbDouble, 3)
Set fldValue = tdf.CreateField("Value", dbLong)
Set fldDaysAvg14 = tdf.CreateField("DaysAvg14", dbDouble, 3)
'Append the index to the Indexes Collection
tdf.Indexes.Append idx
'Append the fields to the TableDef's Fields Collection
tdf.Fields.Append fldTradeDate
tdf.Fields.Append fldStockName
tdf.Fields.Append fldRemarks
tdf.Fields.Append fldCurrency
tdf.Fields.Append fldClose
tdf.Fields.Append fldChange
tdf.Fields.Append fldVolume
tdf.Fields.Append fldHigh
tdf.Fields.Append fldLow
tdf.Fields.Append fldValue
tdf.Fields.Append fldDaysAvg14
'Append the TableDef to the Database's TableDefs collection
dbs.TableDefs.Append tdf
'Refresh the Indexes collection
tdf.Indexes.Refresh
'Refresh the TableDefs collection
dbs.TableDefs.Refresh
Set idx = Nothing
Set fldTradeDate = Nothing
Set fldStockName = Nothing
Set fldRemarks = Nothing
Set fldCurrency = Nothing
Set fldClose = Nothing
Set fldChange = Nothing
Set fldVolume = Nothing
Set fldHigh = Nothing
Set fldLow = Nothing
Set fldValue = Nothing
Set fldDaysAvg14 = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub
Thank you.
Nelson Chou
I trying to create a table using visual basic, when i try to run it. It
prompt me "No fields defined, can't append Table or Index. Anyone, could
highlight to me what is the problem. My code is as follows:
Public Sub PCreateTable()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim idx As DAO.Index
Dim fldTradeDate As DAO.Field
Dim fldStockName As DAO.Field
Dim fldRemarks As DAO.Field
Dim fldCurrency As DAO.Field
Dim fldClose As DAO.Field
Dim fldChange As DAO.Field
Dim fldVolume As DAO.Field
Dim fldHigh As DAO.Field
Dim fldLow As DAO.Field
Dim fldValue As DAO.Field
Dim fldDaysAvg14 As DAO.Field
Set dbs = CurrentDb
On Error Resume Next
'If the table already exists, delete it
dbs.TableDefs.Delete "SGX Individual Historical"
On Error GoTo 0
'Create the table definition in memory
Set tdf = dbs.CreateTableDef("SGX Individual Historical")
Set idx = tdf.CreateIndex("ID")
idx.Primary = True
idx.Required = True
idx.Unique = True
Set fldTradeDate = tdf.CreateField("Trade Date", dbDate)
Set fldStockName = tdf.CreateField("Stock Name", dbText, 50)
Set fldRemarks = tdf.CreateField("Remarks", dbText, 5)
Set fldCurrency = tdf.CreateField("Currency", dbText, 5)
Set fldClose = tdf.CreateField("Close", dbDouble, 3)
Set fldChange = tdf.CreateField("Change", dbDouble, 3)
Set fldVolume = tdf.CreateField("Volume", dbLong)
Set fldHigh = tdf.CreateField("High", dbDouble, 3)
Set fldLow = tdf.CreateField("Low", dbDouble, 3)
Set fldValue = tdf.CreateField("Value", dbLong)
Set fldDaysAvg14 = tdf.CreateField("DaysAvg14", dbDouble, 3)
'Append the index to the Indexes Collection
tdf.Indexes.Append idx
'Append the fields to the TableDef's Fields Collection
tdf.Fields.Append fldTradeDate
tdf.Fields.Append fldStockName
tdf.Fields.Append fldRemarks
tdf.Fields.Append fldCurrency
tdf.Fields.Append fldClose
tdf.Fields.Append fldChange
tdf.Fields.Append fldVolume
tdf.Fields.Append fldHigh
tdf.Fields.Append fldLow
tdf.Fields.Append fldValue
tdf.Fields.Append fldDaysAvg14
'Append the TableDef to the Database's TableDefs collection
dbs.TableDefs.Append tdf
'Refresh the Indexes collection
tdf.Indexes.Refresh
'Refresh the TableDefs collection
dbs.TableDefs.Refresh
Set idx = Nothing
Set fldTradeDate = Nothing
Set fldStockName = Nothing
Set fldRemarks = Nothing
Set fldCurrency = Nothing
Set fldClose = Nothing
Set fldChange = Nothing
Set fldVolume = Nothing
Set fldHigh = Nothing
Set fldLow = Nothing
Set fldValue = Nothing
Set fldDaysAvg14 = Nothing
Set tdf = Nothing
Set dbs = Nothing
End Sub
Thank you.
Nelson Chou