Solved

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

Posted on 2010-09-20
6
550 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Field Properties in the DB Vs Bound Control on the form 9 33
User Level Security 6 38
SetFocus doesn't wait for input 14 34
Modify report 8 8
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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