VB and Access writing data into a new table

K

knut Peters

Hallo NG!
When a new Table is inserted into a database (using the command
createTblTest.... ) I have to start the program to be able to add new data:

rstTest.AddNew
rstTest.Field1 = "XYZ"
rstTest.update

Is there any way to initiate the new table to be able to add data without
restarting the program? What is my mistake?
Thanks in advance for your comments
Regards
Knut
 
V

Vincent Johns

Knut,

I didn't have any trouble creating the new Table and adding a record to
it. Here's my code (omitting the usual comments and the error-handling
stuff):

Public Function NewRec()

Dim newTable As DAO.TableDef
Dim rstTest As DAO.Recordset

Set newTable = CurrentDb.CreateTableDef("TblTest")
With newTable
.Fields.Append .CreateField("TblTest_ID", dbLong)
.Fields.Append .CreateField("Field1", dbText, 10)
End With 'newTable

CurrentDb.TableDefs.Append newTable

Set rstTest = CurrentDb.OpenRecordset("TblTest")
With rstTest
.AddNew
![Field1] = "XYZ"
.Update
.Close
End With 'rstTest

End Function 'NewRec

Of course, the Table should not already exist when you run this. After
it's created, I'd think you would want to use an Append Query, or some
such, to add more records to it.

-- Vincent Johns <[email protected]>
Please feel free to quote anything I say here.
 
Top