Solved

Connecting and reading from Access 2007 database with VB.Net 2008

Posted on 2010-09-20
6
561 Views
Last Modified: 2012-08-14
I am trying to build a windows application with Visual Basic 2008 using vb.net that will connect and read from and insert new records into an access 2007 database.  I have searched several sites and cannot find any clear reference on how to do this.  I will post the code I have so far below.  Please fix my code or give me a new example of a better way to do this.

I am only getting one error right now and that is on the third line in my code:
"Dim dbreader As New OleDb.OleDbDataReader"

The error I am getting is:
"Type 'System.Data.OleDb.OleDbDataReader' has no constructors."

Thanks in advance for your help.
Dim dbcon As New OleDb.OleDbConnection
Dim dbcomm As New OleDb.OleDbCommand
Dim dbreader As New OleDb.OleDbDataReader
Dim dbProvider As String
Dim dblookupString As String

dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.accdb;Persist Security Info=False;"

dbcon.ConnectionString = dbProvider
dbcon.Open()

dblookupString = "Select * from tblinventory where ItemBarcode = " & textAddItem.Text

dbcomm.Connection = dbcon
dbcomm.CommandText = dblookupString
dbreader = dbcomm.ExecuteReader()

While (dbreader.Read())
   MsgBox(dbreader("ItemName")) 'Show data in a Message Box
End While

dbcon.Close()

Open in new window

0
Comment
Question by:red24698
[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
6 Comments
 
LVL 6

Accepted Solution

by:
ajb2222 earned 500 total points
ID: 33721881
change this line:

Dim dbreader As New OleDb.OleDbDataReader

to this

Dim dbreader As OleDb.OleDbDataReader

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33721885
You do not need to use the New keyword on these types. You use new to instantiate an object and these ole types obviously have no constructor which means they cannot be instantiated.

So just do this
Dim dbcon As  OleDb.OleDbConnection
Dim dbcomm As  OleDb.OleDbCommand
Dim dbreader As  OleDb.OleDbDataReader
0
 

Author Comment

by:red24698
ID: 33721899
ajb2222's solution worked, when I took New off of all the other declarations it gave me about 5 new errors but taking it off just the reader statement worked.

ajb2222:  I am going to give you credit for the correct answer, but while I have your attention can you give me an example of an insert statement?
0
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!

 
LVL 65

Expert Comment

by:rockiroads
ID: 33721930
I know you are now working with ajb2222 but hopefully you understood the usage of the new keyword. I will leave your remaining questions to ajb2222.
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 33731526
The best way to modify data (insert or update) is to use a datatable or a data set which is a collection of data tables.

Here is a good explination of using a dataset to update data. http://support.microsoft.com/kb/301248
if you just need to insert a row, a quick way is to use ExecuteNonQuery in the OleDb.OleDbCommand class.
Example:
dbcomm.CommandText  = "Insert into tblinventory....."
dbcomm.ExecuteNonQuery()
 
 
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 33731564
The other thing you are going to want to learn is the basics of Object Oriented Programming.

http://msdn.microsoft.com/en-us/library/aa289512(VS.71).

This will teach you about classes and objects.  Basically,  when you use the new keyword you are creating an object from a class.  The reason you can't just say "New Datareader" is because the Datareader class does not have a constructor.  It is constructed from the OleDBCommand class using the execute reader member.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 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