Link to home
Start Free TrialLog in
Avatar of bobdylan75
bobdylan75Flag for Afghanistan

asked on

Programmatically connect msaccess table to datagridview

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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

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

Avatar of bobdylan75

ASKER

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

Did you make the adjustments to your database ?
1. Data Source=C:\Mydb.mdb
2. select * from ....
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..
What is the exact error ?
System.Data.OleDb.OleDbException was not handled
  ErrorCode=-2147467259
  Message="IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."
  Source="System.Data"
...
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..
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok it works fine,
and if I want to add a user/passowrd to the database?
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;  
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