Solved

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

Posted on 2010-09-20
6
542 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

746 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

12 Experts available now in Live!

Get 1:1 Help Now