Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Programmatically connect msaccess table to datagridview

Posted on 2009-02-17
11
Medium Priority
?
524 Views
Last Modified: 2012-05-06
Hi,
How can I connect msaccess table to dataGridView object?
there are an easy sample code?
I want to connect it programmatically in Vb.net.
Thanks
0
Comment
Question by:bobdylan75
  • 6
  • 5
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23667811
You can use the following code
(you need to set the connection and sql-string correctly to your project)
Dim StrConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Mydb.mdb"
Dim oConn As New OleDbConnection(StrConn)
Dim da As OleDbDataAdapter
Dim oDS As New DataSet
'
Dim sSQL As String
'
sSQL = "Select * From MyTable"
da = New OleDbDataAdapter(sSQL, oConn)
da.Fill(oDS, "Table1")
DataGridView1.DataSource = oDS.Tables("Table1").DefaultView
DataGridView1.Refresh()

Open in new window

0
 

Author Comment

by:bobdylan75
ID: 23668108
Thanks, but using that code It gives me a not define error "E_FAIL" on the FILL code line.
I'm using VS2005.

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23668132
Did you make the adjustments to your database ?
1. Data Source=C:\Mydb.mdb
2. select * from ....
0
Technology Partners: 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!

 

Author Comment

by:bobdylan75
ID: 23668439
yes I do!
the path is correct and the SQL too..
if I use the "automatic objects" it works,
If I do it programmatically gives me an error..
I'm thinking to make a test in a standalone project..

I would use the "automatic objects" but in the file.config it creates
a string with an absolute path, I would like use a relative one..

But the programmatically solution is what I prefer..
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23668566
What is the exact error ?
0
 

Author Comment

by:bobdylan75
ID: 23668829
System.Data.OleDb.OleDbException was not handled
  ErrorCode=-2147467259
  Message="IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."
  Source="System.Data"
...
0
 

Author Comment

by:bobdylan75
ID: 23669079
One moment..
I have change the database..
I've choosen another one,
and everything runs.
But I have to use the other database..
I have done it with MsAccess2007 comptible with access 2002/2003..
I'm thinking that the problem should be here..
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23669195
If you want to use an ms access 2007 database, you need to alter the connectionstring

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

Source: http://connectionstrings.com/access-2007
0
 

Author Comment

by:bobdylan75
ID: 23669473
Ok it works fine,
and if I want to add a user/passowrd to the database?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23669595
With database password
This is the connection string to use when you have an Access 2007 database protected with a password using the "Set Database Password" function in Access.

Quote:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Jet OLEDB: Database Password=MyDbPassword;  
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23669613
By the way. When you add a question, you can assign multiple zones for one question. If you do that, you'll probably reach more experts
This question could be also under the next zones: Visual Studio, VB.NET
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

564 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