Add record to Access Database

T

thomasp

What do I need to add to the following code to be able to add a record to an
Access Database using VB.NET 2005 Express?

Dim da As New OleDb.OleDbDataAdapter("Select * from LCMR", cn)

Dim dt As New DataTable()
da.Fill(dt)

Dim dr As DataRow

dr = dt.NewRow

With dr
.Item("ATime") = strTime
.Item("ADate") = strDate
End With

dt.Rows.Add(dr)
 
O

Oenone

What do I need to add to the following code to be able to add a
record to an Access Database using VB.NET 2005 Express?

I think all you are missing is the following line after all of the code you
posted:

\\\
da.Update(dt)
///

This tells the DataAdapter to perform all the updates you have made to the
DataTable in the database itself.

Ideally you should also wrap all your code (except for the Dim statements)
inside a Try block, and in the Finally clause you should Dispose of the da
and dt objects.
 
T

thomasp

Thanks for the comment. I get the following error when I add that line of
code.

Update requires a valid InsertCommand when passed DataRow collection with
new rows.

Any suggestions?
 
O

Oenone

Update requires a valid InsertCommand when passed DataRow collection
with new rows.

Hmm, I've not seen that one before (but then I haven't written much code
against Access databases either).

Do you have a primary key on your LCMR table?
 

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

Top