Solved

Populating DataGrid from table or query in MS Access

Posted on 2006-06-11
5
317 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now