Promote Your Blog
Google

Saturday, January 28, 2012

How To Add Data To Tables in MS Access Database Using ADODB and Visual Basic

We have already created database in my post How To Create MS Access Database Using ADOX and Visual Basic and learnt how to add table to database in post How To Add Tables to MS Access Database Using ADOX and Visual Basic. Now its time to add data to the added table in created database. Here is the code to do this using ADODB:

Imports ADODB

Const adOpenStatic = 3
Const adLockOptimistic = 3

Dim objConn As ADODB.Connection
Dim objRS
Dim strDBName, strTable As String
strDBName = "MyLibrary.mdb"
strTable = "Books"
objConn = CreateObject("ADODB.Connection")
objRS = CreateObject("ADODB.Recordset")
objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDBName)
objRS.Open("SELECT * FROM " & strTable,
objConn, adOpenStatic, adLockOptimistic)

objRS.AddNew()
objRS("Book") = tbBook.Text
objRS("Author") = tbAuthor.Text

objRS.Update()

objRS.Close()
objConn.Close()
objRS = Nothing
objConn = Nothing
MsgBox("Data Added Successfully")

Book and Author names are read from the GUI. Note that connection and recordset are closed before setting to Nothing

Getting confused first ADOX and then ADODB...Let me add more to confusion by adding more terms like ADO, ADO.NET, DAO, RDO, OLEDB, ODBC...

To sort out this confusion, read post on Microsoft Data Access Components at blog on Computer Science Notes.

No comments:

Post a Comment