How to connect ms access with vb.net using OLEDB

The last time, I showed you how to connect ms access and vb.net using ODBC. This time we will use OLEDB to connect the two.

The procedure is similar, so you may want to check this for a more complete tutorial.

I supposed you still have the files from the previous tutorial. So I’m going straight to the point.

What needs to be changed is the connection string and the name space.

The namespace this time is:

imports system.data.oledb

The connection string will be:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersWernDocumentsDatabase1.accdb;

Remember to change the data source to the actual path where your database is. Followed by the filename of your database. You can use either .mdb or .accdb for this.

Finally, change all the odbc that you see to oledb.

The whole of your code in your parent class will look something like this:

Imports System.Data.OleDb
 
Public Class Class1
    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersWernDocumentsDatabase1.accdb;")
 
    Dim cmd As New OleDbCommand
 
    Public neym As String
 
    Public Sub New()
        con.Open()
 
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM table1"
 
    End Sub
 
    Public Sub creates()
        cmd.CommandText = "INSERT INTO table1(Neyms) VALUES('" + neym + "')"
        cmd.ExecuteNonQuery()
 
    End Sub
End Class

That’s it, don’t forget to declare an object of the parent class that you created on your main form:

dim parent as new class1

Where class1 is the name of the class, and parent is the object name that you want. You can change it to whichever you want. But the class name always depends to what class name you have inputted a while ago.

2 thoughts on “How to connect ms access with vb.net using OLEDB

  1. Pingback: vb.net database connection basics part 1 « Data Integrated Entity

  2. what if the project has been saved as .exe and the path of the database has been changed how can you manipulate and change the datasource? new in vb.net please help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s