Solved

Populating DataGrid from table or query in MS Access

Posted on 2006-06-11
5
348 Views
Last Modified: 2008-02-01
Hello

I’m using a VB.Net front to an MS Access database.

I’d like to connect to the Access database, get all the data from a table or query in MS Access and display it in a DataGrid.

I have actually managed it, but it involves 2 connections to the database - which I’m pretty sure is not right.

Imports ADODB
Imports System.Data.OleDb

Dim Conn As Connection
Dim rs As ADODB.Recordset

Conn = New Connection
rs = New ADODB.Recordset

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                                "Persist Security Info=False"

        Conn.Open()

Dim adapter As New OleDbDataAdapter("QueryName", connection)

rs.Open("QueryName ", Conn)

Dim customersTable As New DataTable("Customers")

adapter.Fill(customersTable, rs)

DataGrid1.DataSource = customersTable

Conn.Close()


Thanks.
Iain
0
Comment
Question by:IainTheVBALearner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 16882312
Iain

You're right, it's not necessary to do everything twice.  The OleDb dataadapter and the datatable REPLACE the ADOBD recordset.  You should use the former in VB.NET.  Your code should work like this

'''Imports ADODB
Imports System.Data.OleDb

'''Dim Conn As Connection
'''Dim rs As ADODB.Recordset

'''Conn = New Connection
'''rs = New ADODB.Recordset

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

'''        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                                "Persist Security Info=False"

'''        Conn.Open()

Dim adapter As New OleDbDataAdapter("QueryName", connection)

'''rs.Open("QueryName ", Conn)

Dim customersTable As New DataTable("Customers")

adapter.Fill(customersTable) ''', rs)

DataGrid1.DataSource = customersTable

'''Conn.Close()

Roger

0
 
LVL 3

Author Comment

by:IainTheVBALearner
ID: 16887715
Hello Roger.  Thanks for the response.

If I put a simple SQL statement in "QueryName" then it works.  But If I put the name a query or table from the database it's connected it, then it doesn't work.  It fails on the adapter.Fill line with this error - no other details:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

So this works:
Dim adapter As New OleDbDataAdapter("SELECT * FROM [tbl Apps]", connection)

But this doesn't:
Dim adapter As New OleDbDataAdapter("qryProjectsBasicByTitle", connection)

qryProjectsBasicByTitle is a valid SELECT query in the database, with no parameters.

Do you know how to make it fill the DataGrid from a query?

Thanks
Iain
0
 
LVL 34

Accepted Solution

by:
Sancler earned 125 total points
ID: 16888222
If you want to use a dataadapter then you will need to use a SELECT statement, although you can do so with "SELECT * FROM MyAccessQuery".

If you want to call an Access Query directly, you will need to use a DataReader.  On these lines

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

Dim customersTable As New DataTable("Customers")
'code needed to structure datatable

Dim cmd As New OleDbCommand("MyAccessQuery")
cmd.Connection = connection
cmd.CommandType = CommandType.StoredProcedure
connection.Open
Dim dr As OleDbDataReader = cmd.ExecuteReader()
While dr.Read
   'code needed to write values from single record returned into row of datatable
End While
Connection.Close()

Sorry, in my initial response, I hadn't picked up the point about using an MS Access query directly.

As you can see, it is far simpler to use a dataadapter - selecting from a Query, rather than from a Table, if necessary - because (a) it will create the datatable structure for you and (b) will bring over all the necessary data without you needing to code putting it in the datatable.  But the other route (which is closer to the ADOBD recordset approach, perhaps) is available if you want to use it.

Roger
0
 
LVL 3

Author Comment

by:IainTheVBALearner
ID: 16889030
mmm....okay.  Thanks.  I guess that answers the question, although I'm not sure how to populate the DataTable by moving thought the DataReader...

I'm really struggling with this...I've done loads of MS Access - recordsets & multicolumn listboxes and they work great.

I'm teaching myself VB.NET but I'm struggling to see what's better...it seems that a DataGrid is an quivalent to a multicolumn listbox, as there is no multicolumn in VB.NET, but populating a DataGrid is such a palava - and I've not even begun editing DataGraid data and updating the Access tables based on the new DataGrid data...

I'm pretty new to this though, and I'm sure I'll look back on this and realise it's only because there were a bunch of things I didn't know...

Thanks.
Iain
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16890019
Iain

It will get easier, but there'll be a lot of swearing at it first ;-)

Thanks for the points

Roger

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question