Microsoft Access
  Home arrow Microsoft Access arrow Page 6 - Connecting to Microsoft Access with ADO
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MICROSOFT ACCESS

Connecting to Microsoft Access with ADO
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2006-04-19

    Table of Contents:
  • Connecting to Microsoft Access with ADO
  • Overview of ActiveX Data Objects
  • Create a UDL file to connect to an MDB file
  • Read the connection string from the file
  • Create a reference to ADO
  • Write code to open and close the ADODB connection to the database

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Connecting to Microsoft Access with ADO - Write code to open and close the ADODB connection to the database


    (Page 6 of 6 )

    This is a very short script and you get the assistance of intellisense technology. Without this help, lazy as I am, I would not have taken to Microsoft technology; no need to thumb hefty volumes in the shadow of your manager hawk. Just type in the code window ADODB, and you will get a drop-down as shown, giving you all the object choices you can make: it could be a connection, it could be a command, and so forth. Insert the following code as shown in the next paragraph in the click event of the button.

    Private Sub Command0_Click()
    Dim conn As New ADODB.Connection
    Dim prop As Property
    Dim strg As String
    strg = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\
    Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;
    Persist Security Info=False" conn.Open strg MsgBox ("connection open") conn.Close MsgBox ("Connection closed") End Sub

    You created a new connection object called conn. The variable string is the one that you copied from the UDL's file while it is displayed as a text, the mantra. The connection can be opened, if you provide the connection string. The First message says the connection is Open. Trust me, the connection is really open. It is always a good practice to close the connection, and therefore you see the conn.close statement. The second message box shows that the connection is closed. If the connection is closed you have no access to the data. This is a basic step you need to take when you want to access data.

    Review connection properties using code

    You saw earlier in the Object Browser the driver specific connection properties for the referenced ADO library. You might also have seen some of them in the All tab of the data link window. We will look at the same using code since we have been able to open a connection.

    Add some code to modify the earlier code as follows. In ADO it is all about collections. The property collection is one such collection (refer to the ADO Model). The variable prop is declared to be a property, conn.Properties gives you access to all the properties, and the For... Next logic block gives you all the properties pertinent to this connection. We will also find out certain other properties of the connection object, such as the Ado Version in use, the connection state, and the default connection timeout.

    Just remember to add a textbox to the form before running the page and make sure the Text box number matches with what you are adding to the form. The message boxes when they appear should be clicked OK; they are left intentionally in code for you to follow the code as it gets executed.  The variable strAdd just gathers up all the strings and finally places them in the text box.

    Private Sub Command0_Click()
    Dim conn As New ADODB.Connection
    Dim strAdd As String
    strAdd = ""
    MsgBox ("ADO version used is: " & conn.Version)
    strAdd = strAdd + "ADO version used is: " & conn.Version
    Dim prop As Property
    Dim strg As String
    strg = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\
    Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;
    Persist Security Info=False" conn.Open strg MsgBox ("connection open") ' MsgBox (conn.State) strAdd = strAdd & vbCrLf & "Connection status (open) is :
    " & conn.State MsgBox ("The default connection timeout is: " &
    conn.ConnectionTimeout) strAdd = strAdd & vbCrLf & "The default connection timeout is:
    " & conn.ConnectionTimeout ' For Each prop In conn.Properties Debug.Print prop.Name, "=", prop.Value Next prop ' conn.Close MsgBox ("Connection closed") MsgBox (conn.State) strAdd = strAdd & vbCrLf & "Connection status(closed) is:
    " & conn.State With Text7 .SetFocus .Text = strAdd .BackColor = RGB(0, 0, 0) .ForeColor = vbYellow End With
    End Sub
    

    When you run this code you will see the following window after you click OK to the various messages that show up.

    In the immediate window you will see the message printed that lists all the properties for this source. Go to Microsoft Visual Basic Editor screen and open up the immediate window. The following is what you will see (only a part presented).

    Summary

    ADO is easier to handle than DAO. Anyway DAO is out and ADO is in. It has great scripting support and you have intellisense to make life a lot easier. The UDL file is a powerful means to connect to OLEDB sources, of which there are very many. In this tutorial only a couple of properties of the Connection object are described. There are other properties, and these will be presented after looking at some of the related other objects.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Yes, that is true coming straight from the vaults of MS. These remain for backward...
       · i'm new person want to use the aplication asp. Can u give me the sample how to...
       · I have created a system DSN called 'ForAsp' for your illustrationI have an IIS...
       · Send me your email, I will send the code.Jay
     

    MICROSOFT ACCESS ARTICLES

    - Linking SQL Express 2005 Tables to MS Access...
    - Working with Access Projects in Access 2007
    - Exploring Access 2007
    - Working with Stored Procedures in an MS Acce...
    - Creating and Using Action Queries
    - Creating Data Access Pages with Charts using...
    - Advanced Ideas using VBA
    - VBA Details
    - Updating Records in MS Access
    - Using ADO`s Record Object with URLs
    - Exporting XML from MS Access 2003
    - Importing XML into MS Access 2003
    - On Using Pass-through Queries in MS Access
    - Distributed Queries in MS Access
    - Configuring a Linked Microsoft Access Server...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway