This is how you can connect to an Access Database using ADODB..
First add a reference to Microsoft ActiveX Data Objects 2.0 Library
Now to lets Declare some variables :
Expand|Select|Wrap|Line Numbers
- Private Con As New ADODB.Connection
- Private Rec As New ADODB.Recordset
Now lets Create a Connection to your Database :
Expand|Select|Wrap|Line Numbers
- Con.Provider = "Microsoft.jet.oledb.4.0; "
- Con.Open Database Path
After this Connection will established with the Database.
Now we have to open some Tables and have to fetch Records to your app, to do so we need a Recordset in which all the records from selected Tables are fetched.
Now the following code will fetch all Records from Table1 in MDB File :
Expand|Select|Wrap|Line Numbers
- Rec.Open "Select * From Table1", Con, adOpenDynamic, adLockOptimistic
* Second parameter is Connection
* Third parameter is Open Mode means you can move forward or backward in the table, can add and delete. There are several Modes.
* Fourth parameter helps you to Lock your Database File that it should open once at a time.
* There are few more parameters but i m not discussing them here if anybody want to know can ask :)
Now lets create a sample program which will open a Database and then Load all Records from Table1 in a Recordset.
Expand|Select|Wrap|Line Numbers
- Private Con As New ADODB.Connection
- Private Rec As New ADODB.Recordset
- Public Sub Create_Connection(File_Path as String)
- Set Con = New ADODB.Connection
- Set Rec = New ADODB.Recordset
- Con.Provider = "Microsoft.jet.oledb.4.0; "
- Con.Open File_Path
- End Sub
- Public Sub Load_Records(sqlQuery as String)
- Rec.Open sqlQuery, Con, adOpenDynamic, adLockOptimistic
- End Sub
- Public Sub Close_Connection()
- Rec.Close
- Con.Close
- End Sub
Expand|Select|Wrap|Line Numbers
- Create_Connection (App.Path & "\db1.mdb")
- Load_Records ("Select * From Table1")
> How to Navigate ?
Expand|Select|Wrap|Line Numbers
- Rec.MoveNext
- Rec.MovePrevious
- Rec.MoveFirst
- Rec.MoveLast
# if anywhere in the article, i have made any mistake please correct that.
Regards
ARUZ