Connect to Microsoft Access database and Fill DataGridView

As in the previous post , much sensilla and with very little code, we will see how to connect in visual basic. net with a database file in Microsoft Access and then retrieve data from one of the boards DataGridView filled with them.

The first thing to do is import two namespaces as show below:

 Imports System.Data
 Imports System.Data.OleDb

For this example we will use a form with a button (Button1) and a DataGridView (DataGridView1). In the click event of button1 a copy the following code and the name of the database. Mdb and the table name in the statement SELECT ) so that everything works fine.

 Dim cnn As New OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C: \ myBaseAccess.mdb; User Id = admin, Password =;")

 Dim da As New OleDbDataAdapter ("SELECT * FROM Products", cnn)
 Dim ds As New DataSet

 da.Fill (ds)

 DataGridView1.DataSource = ds.Tables (0)

Well, actually not much to explain ... you create a connection (CNN), then a DataAdapter (da) having as parameter the statement to retrieve data ( SELECT ) and the connection to use (cnn). If you want to select only certain records need to add a condition WHERE the select statement.

In the following lines we will leave some brief definitions in case any of you were needed and to learn more about the different options for creating a connection to Microsoft Access I leave also the following link: http://www.connectionstrings.com/access

DataAdapter

Represents a set of SQL commands and a database connection that are used to fill the DataSet and update the data source.
DataAdapter serves as a bridge between the DataSet and a data source for retrieving and storing data. To act as a bridge, DataAdapter Fill method assigns, modifying the data of DataSet to match the data source, and assigns the Update method, which modifies the data in the data source to coincide with those of DataSet.

DataSet

Represents a memory cache of data. It is a fundamental component of the ADO.NET architecture. DataSet consists of a collection of DataTable objects.

DataTable

Represents a data table in memory.
DataTable is a central object of the ADO.NET library. Among the objects that use DataTable and DataView DataSet included.

When accessing DataTable objects, you must keep in mind that these are case-sensitive conditionally. For example, if one DataTable is named "mydatatable" and other "Mydatatable", the string that is used to search one of the tables are considered case-sensitive. However, if there is "mydatatable" but no "Mydatatable" is considered the search string is not case sensitive. A DataSet can contain two DataTable objects that have the same TableName property value and different values ​​for the Namespace property.

26 Responses to "Connect to Microsoft Access database and Fill DataGridView"

  1. [...] To write the example of connecting to SQL Server and Microsoft Access Connection I realized that even though these are the two types most used databases when [...]

  2. Uruguay carla says:

    because the truth was very helpful. because I needed a code as simple as possible because I had trouble loading a table from access to a datagrid and all the codes I tried this was the only thing that brought me across the table and with so little code
    With truth and blessings
    Carla

  3. Edwin says:

    I served a lot thanks! watching it even if I wanted to know how filtering by country, to find a particular record.

  4. Oswaldo says:

    Greetings .. very good your article and your page ... I was going to ask how to clean the DataGridView1 every time you click a button to query and view data, but I think the fix, if not right, but at least I worked . The only thing I did was add "ds.Clear ()"
    So:
    ds.Clear ()
    da.Fill (ds)
    DataGridView1.DataSource = ds.Tables (0)

    Now as I make a TextBox where typing a name billboard showing only the results found in the DataGrid ..

    • Elisha says:

      A. - To clean: ok what you do, you could also do DataGridView1.DataSource = Nothing
      2. - To display only the data that match the textBox the procedure is the same, the only thing that changes is the SELECT statement that performs the search. For example: "select * from Employees WHERE Name LIKE '%" & TextBox1.Text & "%'"

      Luck

  5. Oscar says:

    I have worked with Visual Studio 6 and I'm trying to work with Visual Studio 2008. I did what the manual says to display data from a table in a DataGridView, without success. DataGridView is displayed, but not the data. In the load event of the form are automatically placed lines of code to populate the data, but apparently that does not work. Anyone know if this is a known problem?

  6. Maxi says:

    The fact that this works perfectly me but what I do not work for me is the insert update and delete ... even with conexionstring probe and sqlcomand .. and not to do ..
    Any ideas?
    Thank you very much.

    • Maxi says:

      Guys I answer myself.
      insert:
      As New OleDbDataAdapter Dim daagregar ("insert into orders (IDPLATO, Quantity, IDMesa, CustomerID, Date) values ​​('" & TxtCodigo.Text & "', '" & TxtCantidad.Text & "', '" & TxtMesa.Text & "', '0', '8 ')", cnx)
      daagregar.Fill (dsPedidos)

  7. Jorge says:

    hol such I would like to know as I do to insert data into a datagridview but if the value exists and send a message that already exists because GREETINGS

  8. Gabriel says:

    Grateful for the code ...

    Is there any way of recovering the data in the gridview and put them in a PDF file??.

    Greetings from Chile

  9. tttony says:

    Hello, I connect with an Access 2007 DB from C #. NET here all right, but I have a table that has three fields with dates and I want to give an output format and I have two options:

    1) After completing the DataGridView to format each field containing date

    2) Do it with a SELECT statement:

    SELECT TOP 20 *, Format ([buy_date], "Long Date") & "" & Format ([buy_date], 'Short Time') AS [buy_date] FROM ORDER BY id DESC testing

    The problem with this view is that it places the [buy_date] from position 4 to 2 and I have to write more code to order and that's what I do not want

    Regards

  10. elisha said:

    I get the following error
    Could not find installable ISAM.
    da.Fill (ds)
    I did everything as is why?

  11. Pablo_la31 says:

    Hi! I see I connect with a BD 2003 in vb.net access from there bring all the data from 3 tables in a datatable with all the columns of the 3 tables, now I want q is display on a dataGridView but not all columns but to some .. as I can do it??

  12. Jordi says:

    Thanks, A very simple explanation and very practical, Thanks

  13. Daniel says:

    Excellent post, very helpful and very detailed, just did not know it was so easy.

    Thanks

  14. Misael says:

    hello! great shape and very secilla
    A question once you fill the DataSet DataGridView1 How could I get some value from a specific cell, ie if the value kisiera DataGridView1 on to the column 1 in row 1?

  15. Claudio says:

    Hello;
    I use Visual Basic 2008 Microsofty and shortcuts as database .. to write the same code that is posted in this page I get an error on the line that says: da.Fill (ds)

    the error is not controlled System.Data.OleDb.OleDbException
    ErrorCode = -2147467259
    Message = "Not a valid filename."

    Could help me?
    Thank you ...

  16. tato25 says:

    hi all need help with discharge of a record to the data link connection and going well but then for the high I get this is the code
    connection:
    Formclientes_Load Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim connection As OleDbConnection
    connection = New OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0; data source = C: \ proyectovb.net \ tesis.mdb")
    As DataSet Dim dsCustomers
    As String Dim consultaclientes
    consultaclientes = "select clientes.dni, clientes.apellido from customers"
    As New OleDbDataAdapter Dim daclientes (consultaclientes, connection)
    dsCustomers = New DataSet ()
    daclientes.Fill (dsCustomers, "clients")
    Dgvclientes.DataSource = dsclientes.Tables ("clients")

    Add button:
    As DataTable Dim TABLACLIENTES = New DataTable ("Customers")
    As DataRow Dim REGISTROCLIENTES = TABLACLIENTES.NewRow
    REGISTROCLIENTES ("dni") = Me.TextBox1.Text
    REGISTROCLIENTES ("NAME") = Me.TextBox2.Text
    TABLACLIENTES.Rows.Add (REGISTROCLIENTES)
    I use to discharge as do I make a new high but first check if the record already exists
    I expect some response from some solidarity through

  17. Borro says:

    Big Beast!!!
    I had already burned up the tabs

    I'm using vb6 and vb recent change to 2010

    THANKS!!

  18. Jeysson says:

    SEEMS TO ME THIS IS NOT CODE FOR VB 2010
    PORFAVOT can teach anybody know
    LEARN QUISIEA URGENTLY!

  19. Rodhe Gav says:

    Excellent contribution. Congratulations poured me a lot. I iniciandome in the world of VB.NET. Although I have enough programming, but you always need examples so when you change the programming language. Regards

  20. John says:

    I saw nothing open about this, but I want to add form data to a database in access and I can not, if someone could help would appreciate

    regards

  21. Amaliam says:

    I was very helpful, thanks!

  22. Chilon says:

    Komo litter kapo, look I have a question, I'm programming with VB. Net 2008 for quite not use it, and good was picking up. I commented.

    I did a module where my definition of coneccionn sentensia, and the data adapter and data set, I made the connection in the jump from a mistake but I n my Coneccion.open, looks like I did and tell me if you can help me ..

    MODULE

    Imports System.Data
    Imports System.Data.OleDb
    Module Module1
    As New OleDbConnection Public ConecZion ("Provider = Microsoft.Jet.OLEDB.4.0; DataSource = C: \ Documents and Settings \ USER \ My Documents \ Pr Testear.mdb")
    As New OleDbDataAdapter Public DataAdap ("select * from [Club XXXX] order by NrodeSocio" ConecZion)
    Dataset As New DataSet Public
    End Module

    [B] AND HERE FROM THE [/ B]

    FrmFormwork_Load Private Sub (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ConecZion.Open ()

    DataSet.Clear ()
    DataAdap.Fill (Dataset, "miTestearDt")
    Dataset.Tables ("miTestearDt"). DefaultView.Sort = "NrodeSocio ASC"

    ConecZion.Close ()
    End Sub

    THIS ERROR AND STRIP ME

    Could not find installable ISAM.

    porq can be? : S..

    I hope to embrace response

  23. Marco says:

    Thanks for the help the problem is fixed commenters creating an instance of the DataSet that was declared at the beginning for example my variable is called a record then it would be: Dim record = New DataSet () and voila!

Comment