VB and Access writing data into a new table

  • Thread starter Thread starter knut Peters
  • Start date Start date
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top