Index   Search   Add FAQ   Ask Question  
 

Please note: This page is not maintained anymore. Please visit the new and improved FAQ at http://www.orafaq.com/faq/vb

Oracle/ MS Visual Basic FAQ

$Date: 18-Jul-2005 $
$Revision: 0.03 $
$Author: Frank Naudé $

Topics

  • What is VB and what is it used for?
  • What is DAO, RDO, ADO and OLE DB? Which method should one use?
  • How does one connect to Oracle from VB?
  • Why is there only one record in my recordset?
  • Where can one find more info about VB?

  • Back to Oracle FAQ Index

    What is VB and what is it used for?

    Visual Basic (VB) is an extremely popular and easy to use programming language provided by Microsoft Corporation. VB is mainly used to develop Windows based applications with.

  • Back to top of file

  • What is DAO, RDO and OLE DB? Which one should one use?

    DAO, RDO, ADO and OLE DB are data access methods that all accomplish exactly the same task.

    DAO - Data Access Objects (1-tier)
    Allow VB applications to talk to a database (the JET Engine) via ODBC. DAO was Microsoft's first object oriented solution for the manipulation of databases using the Jet Database Engine. The JET engine duplicates the functionalities of ODBC, and thus does not add much value. As the JET engine is generic, many of Oracle's features would not be accessible. Microsoft is phasing out this method.

    RDO - Remote Data Objects (2-tier)
    Allow VB applications to talk to a relational database (various Relational DBMSs) via ODBC. RDO is an interface to remote RDBMS via OBDC. One needs the Enterprise Edition of Visual Basic to use RDO. Microsoft is encouraging developers to migrate their RDO programs to ADO and OLE-DB.

    ADO - ActiveX Data Objects (1 to n-tier)
    Allow VB/Other Web Tools (Browsers) to interface with different kinds of data sources. ADO is a more recent Microsoft Data Access technology designed to replace DAO and RDO. ADO is designed to be simpler to use and more powerful than DAO/RDO. Serves an interface to Microsoft's new OLE-DB technology (thinner than ODBC). Can be used to access all sorts of "non traditional data" (e.g., web pages/documents, etc.).

    OLE DB data provider
    OLE DB is Microsoft's successor to ODBC that utilizes a set of COM interfaces for accessing and manipulating of data. Oracle implemented OLE DB as part of their "Oracle Provider for OLE DB" client software. It provides interface for both data-consuming applications and database providers. OLE DB is considered a thin middle layer which provides better data access performance.

    Summary
    RDO and DAO still works in VB for backwards compatibility. However, it is best to convert to ADO or OLE-DB.

  • Back to top of file

  • How does one connect to Oracle from VB?

    Connectivity to Oracle is provided via ODBC or OO4O (Oracle Objects for OLE). For more information about ODBC, read the ODBC FAQ. For information about OO4O, read the OO4O FAQ. Look at this examples:

    ' DAO Example (Data Access Objects)

    Dim wstemp As Workspace
    Dim dbtemp As Database
    Dim rstemp As Recordset
    
    Set wstemp = DBEngine.Workspaces(0)
    Set dbtemp = wstemp.OpenDatabase("", False, False, "ODBC;DSN=Oracle;USR=scott;PWD=tiger")
    Set rstemp = dbtemp.OpenRecordset(myquery.Text, dbOpenDynaset, dbSQLPassThrough)
    howmany = 0
    Combo1.Clear
    Do Until rstemp.EOF
     	msgbox rstemp(0)
    	rstemp.MoveNext
    	howmany = howmany + 1
    Loop
    
    ' RDO Example (Remote Data Objects)
    Dim contemp As New rdoConnection
    Dim rstemp  As rdoResultset
    Dim envtemp As rdoEnvironment
    Set envtemp = rdoEngine.rdoEnvironments(0)
    envtemp.CursorDriver = rdUseServer
    	          ' or rdUseOdbc, rdUseNone, rdUseIfNeeded, rdUseClientBatch
    With contemp
      .Connect = "ODBC;DSN=Oracle;USR=scott;PWD=tiger"
      .EstablishConnection rdDriverNoPrompt, false, rdoForwardOnly
                                               ' or rdoStatic, rdoKeyset, rdoDynamic
    End With
    
    Set rstemp = contemp.OpenResultset("select ...") ' Your SQL here
    
    howmany = 0
    With rstemp
    Do Until .EOF Or howmany > 2000
       msgbox .rdoColumns(0) ' Popup a message box showing the 1st column
       .MoveNext
       howmany = howmany + 1
    Loop
    
    ADO Example
    Option Explicit
    
    Public m_adoCnn As New ADODB.Connection
    Public m_adoRst As New ADODB.Recordset
    
    Public Sub Command1_Click()
        m_adoCnn.ConnectionString = "Provider=MSDAORA;Password=tiger;User ID=scott;Data Source=database"
        m_adoCnn.Open
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        m_adoCnn.Close
    End Sub
    
    OO4O Example
    Option Explicit
    
    Dim m_oraSession  As Object
    Dim m_oraDatabase As Object
    
    Private Sub Command1_Click()
        Set m_oraSession = CreateObject("OracleInProcServer.XOraSession")
        Set m_oraDatabase = m_oraSession.DBOpenDatabase("Database", "user/password", 0&)
        
        MsgBox "Connected to " & m_oraDatabase.Connect & "@" & m_oraDatabase.DatabaseName
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Set m_oraDatabase = Nothing
        Set m_oraSession = Nothing
    End Sub
    
  • Back to top of file

  • Why is there only one record in my recordset?

    When you do a recordcount and it return only one record in the recordset, while you know there are more records, you need to move to the last record before doing the count. Look at this example.
    Dim rs As Recordset 
    rs.MoveLast 
    TxtNumRows.Text = rs.RecordCount
    

    NOTE: Don't forget to do a rs.MoveFirst to get back to the first record again.

  • Back to top of file

  • Where can one find more info about VB?

  • Back to top of file
  • HOME | ASK QUESTION | ADD FAQ | SEARCH | E-MAIL US