30 March 2009

AdoDB VB 6.0

Database Programming: ADO Object Model
The Microsoft ActiveX Data Objects (ADO) architecture is considerably less complicated than that of the DAO or RDO object model. The relative simplicity of the ADO architecture doesn't mean that learning ADO is simple, however. Even though the ADO object model has fewer objects and collections than DAO and RDO, those elements it has are often more complex than their DAO or RDO counterparts because they expose more methods and properties. A few ADO objects also expose events, which weren't implemented in DAO.


ADO is so rich, powerful, and flexible

 One reason the ADO object model is simpler than the DAO and RDO object models is that it has fewer collections, which abound in DAO and RDO. For example, in ADO you can create any number of Connection and Recordset objects, but they are stand-alone objects and the object hierarchy won't maintain a reference to them for you. At first glance, you might believe that having to keep track of all the active objects and store them in your own collections if you want to use them later is going to make your job a bit more difficult. But when you take a closer look, you'll realize that dealing with stand-alone objects greatly simplifies the structure of your programs because in most cases you don't have to write cleanup code: When an object goes out of scope, ADO takes care of its correct termination and automatically closes open Recordsets and connections if necessary. This approach reduces memory leaking and delivers applications that require fewer resources


Make Connections/disconnections to database using procedure ADODB.


First we need objects:

---------------------------------------------------------------------------------------------

Private  mabz_con            As ADODB.Connection
Private mabz_cmd                 As ADODB.Command
Private mabz_rst                 As ADODB.Recordset
----------------------------------------------------------------------------------------------

then add this procedure:
i used microsoft access for this, so i made a connection string provider jet.OLEDB 4.0


Private Sub connect_to_DB()


    Set mabz_con = New ADODB.Connection
   mabz_con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                              & "Data Source=" _
                              & mabz_getpath _   'it is a function
                              & "mydata.mdb" 'name of my database
    mabz_con.Open  ' so it will open that data source provided above

    Set mabz_cmd = New ADODB.Command
    Set mabz_cmd.ActiveConnection =mabz_con
    mabz_cmd.CommandType = adCmdText '<-fixed type of command type

End Sub


------------------------------------------------------------------------------------

End of the procedure in connecting database


Next, every time we open a database, we should close it whenever it is not needed to avoid
any conflicts.
-------------------------------------------------------------------------------------

Private Sub Disconnect_From_DB()

    Set mabz_cmd = Nothing
    
    mabz_con.Close ' close connections
    Set mabz_con = Nothing

End Sub

---------------------------------------------------------------------------------------
This is the code for mabz_getpath: you can put it a module or not . But, it is very useful when
it is a module. For fast, effective, and efficient to code.

'mabz_getpath


----------------------------------------------------------------------------------------

Public Function mabz_getpath() As String


    Dim strAppPath As String
    
    strAppPath = App.Path
    
    If Right$(strAppPath, 1) <> "\" Then
        strAppPath = strAppPath & "\"
    End If
    
    mabz_getpath = strAppPath

End Function

----------------------------------------------------------------------------------------

Source: http://vb6.us/

[updated: July 24 2011]

Advertisement


Featured Offers:
Advertisement