Monday, December 27, 2010

How connect MS Access database with VB6.0 Using ADODB

Hello everyone!
Here is my technique on how to connect MS Access database with VB6.0 using ADODB.

Ok.
I assume that you already know the basics of vb6.

Note: This is not a full tutorial about connecting MS Access database with VB6.0 using ADODB. I will only show my personal technique on how i do this.


Ok.. Here it is,,

The first thing i do is this.

In you VB6 IDE, On the Menu bar Click on "Project>References" then put a check on the "Microsoft ActiveX Data Object 2.0 Library". Without this you cannot access the ADODB. So, for us to use the ADODB we must check this.


Ok after checking.

Firstly, we must declare a connection.
(In this case we are going to declare an objects which are accessible only in a form. So we are going to use "Dim")

Do this:
'Type this on the top of the Coding Area of your form
Dim cn as new ADODB.Connection 'This is for the connection
Dim  rs as new ADODB.Connection 'This is for the recordset that we are going to use

'-----
Let say we have a database named mydb.mdb. And inside our db, we have a table named sampledata.

'put this to the Private Sub Form_Load()

Private Sub Form_Load()
 on error goto ErrorHandler

     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & App.Path & "\mydb.mdb;"
     rs. Open "Select * From sampledata", cn, adopenstatic, adlockoptimistic
   
     if rs.Eof = true then
               msgbox "Table has no content."
    else
               msgbox "Table has contents."
   end if

     set rs = nothing
     set cn = nothing
   
exit sub
   
ErrorHandler:

msgbox Err.Description

End Sub


'Note: we have to put our database inside the directory where our project is located.






'The program will display a msgbox saying "Table has no content" or "Table has contents" if it is connected and the table exists. But if the connection is incorrect and the table does not exists, there will be a msgbox saying the error description.




Ok that's how i connect access database with vb6.0 using adodb.


Thank you.

2 comments:

  1. How about Making a Login Form using ADODC control with no modules to use? ...can you make tutorial of that?? my email is whovitamyx@ymail.com ,, th :ank you :)

    ReplyDelete
    Replies
    1. Hi WHOVITAMAX I will make as what you have requested. it will be posted here soon..

      Delete