connecting to microsoft access

yaniv

Well-known member
Joined
Apr 15, 2002
Messages
162
Location
israel
I know this question asked before, but i cant make the connection to the database, cant even find the right statment to make in the Dim stament.

can any body give sample code for it?
should i put the oledbconnection controler on the bord?

how do i make the connection move one record?

i just cant make it!!
 
yaniv, if you can wait about an hour or so when I get to my lunch break, I will be more than happy to whip you up a quick little tutorial so to speak on how to get the database connection you want. I will be back then, so if someone has already responded, I will not. And although I can have you up and running with a few easy steps, it might not be the most efficient. So just a little forewarning.
 
As promised, I am back to give you a short tutorial the best I know how.

First, you will need to add a reference to your project.

To do this, go to the Project -> Add Reference. Go to the "COM" tab and scroll down to find the Microsoft ActiveX Data Objects 2.7 Library. Highlight it and click Select on the Right. Click OK to add the reference and if asked, click Yes to add the wrapper (if not asked, dont worry about it). here is what it will look like (roughly)
[Broken External Image]:http://www.tonka6969.homestead.com/files/AddReference.JPG

Then in your code use this.

Code:
Dim cnADOConnection As New ADODB.Connection()

To open your connection, use the following, I am assuming you are using access, so I will include that.

Code:
m_cnADOConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = "C:\Filename.mdb")

I set my recordset variables as global, but it is up to you. where ever you want to put the code, you can.

Code:
 Dim rstRecordset as New ADODB.Recordset()

When opening your recordset, use the following format:
Code:
rstRecordset.Open("TableName", m_cnADOConnection, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

Now, of course, the permissions you set will vary according to what you want to do with it. These permissions give you the most freedom.

Finally, to move around in the records, and such.

Code:
rstRecordset.MoveFirst
rstRecordset.MovePrevious
rstRecordset.MoveNext
rstRecordset.MoveLast

Also, by typing rstRecordset and then a period, you can search through some of the other methods and properties of the recordset that you can play with.

Hopefully, that will give you some help. if you need any more help, just let me know and I will try to the best of my ability.

Like I said before, this might not be the best way to do it, but it works.

Good Luck.
 
Do you really want to use ADODB (COM) and not the new ADO.NET provider? ADO.NET is soooo much nicer...

The MSDN docs are filled with sample code. If you need me to post some, let me know.

-ner
 
i tryed to use ADO, but i have problem:


I copyed samle of thinker from this forum to run connection to access table and i"m getting the same error all the time:

"An unhandled exception of type System.Data.OleDb.OleDbException occurred in system.data.dll"

the code is:

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Dim name As String
Dim sql As String
Dim cn As New OleDb.OleDbConnection()
Dim dr As OleDb.OleDbDataReader

cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Documents and Settings\
 
Back
Top