Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-20
6
Medium Priority
?
569 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Industry Leaders: 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 describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

618 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