[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

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

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
red24698
Asked:
red24698
  • 3
  • 2
1 Solution
 
ajb2222Commented:
change this line:

Dim dbreader As New OleDb.OleDbDataReader

to this

Dim dbreader As OleDb.OleDbDataReader

0
 
rockiroadsCommented:
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
 
red24698Author Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rockiroadsCommented:
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
 
ajb2222Commented:
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
 
ajb2222Commented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now