Add record to Access Database

  • Thread starter Thread starter thomasp
  • Start date Start date
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)
 
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.
 
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?
 
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?
 
Back
Top