Remember Me
forgot your password?

AutoIt:Connect to Access Databases:mdb & accdb files

Ever wonder how to connect to from the AutoIt to a MS Access database? AutoIt, for those who don't know, is a program to automate keystrokes and mouse movements. This is in reference to a blog post regarding a problem I had for connecting to Access from AutoIt. There are two different file types in MS Access 2007. Access 2003, 2000 and older version will most use a .mdb file type; whereas, Access 2007 can use .accdb file extension/type. I'll show some basic code that will work with both versions. For this example, I will only retrieve one record and one field.

First let's first create some variables that will hold the database file name (whether .mdb or .accdb), the table name and the query to execute:

$dbname = "C:UsersvkDocumentsdbMarketingarticleSubmissionsTutorialRef.mdb"
$tblname = "articles"
$query = "SELECT * FROM " & $tblname & " WHERE articleID = 4"

The & is simply a concatenation of the strings.

Let's set the variable for the one field that we want to retrieve from the database.

Local $title

Pretty straightforward so far, isn't it?

Then create the connection to the ADODB:

$adoCon = ObjCreate("ADODB.Connection")

Then set the Provider. There is a different Provider for each file extension. A .mdb file will have its own Provider and a .accdb file will have another.

Here is the Provider for a .mdb file:

$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $dbname)

Here is the Provider for .accdb file:

$adoCon.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $dbname)

Now let's create the object Recordset, set some required options and then execute the query:

$adoRs = ObjCreate ("ADODB.Recordset")
$adoRs.CursorType = 1
$adoRs.LockType = 3
$adoRs.Open ($query, $adoCon)

A few notes on the CursorType and LockType properties. I needed to set these in order to retrieve the specified field's value.

Setting the CursorType to a value of '1' means 'adOpenKeyset'. CursorType is defined on the MSDN as: "Sets or returns a CursorTypeEnum value. The default value is 'adOpenForwardOnly'. Use the CursorType property to specify the type of cursor that should be used when opening the Recordset object." The adOpenKeyset is defined on the MSDN Microsoft site as: "Uses a keyset cursor. Like a dynamic cursor, except that you can't see records that other users add, although records that other users delete are inaccessible from your Recordset. Data changes by other users are still visible".

Oddly enough, when I tried to set the CursorType to a value of '2' which means 'adOpenDynamic' AutoIt could not retrieve the field's value. It was just blank but no error occurred. 'adOpenDynamic' is defined as: "Uses a dynamic cursor. Additions, changes, and deletions by other users are visible, and all types of movement through the Recordset are allowed, except for bookmarks, if the provider doesn't support them".

LockType is defined as "Indicates the type of locks placed on records during editing. Sets or returns a LockTypeEnum value. The default value is adLockReadOnly." With it's value set at '3' which indicates 'adLockOptimistic'. 'adLockOptimistic' is "Indicates optimistic locking, record by record. The provider uses optimistic locking, locking records only when you call the Update method" which is good so we leave it at that value.

Then we simply place the result into the $title variable we declared earlier. You can use either of the two following lines. The first is the retrieving the value by the field's name and the latter is retrieving it by the field's numerical sequence. Essentially, it's the column's number. Column numbers starts at '0' (i.e. zero) and not '1' (i.e. one). So if you wish to grab the third column's value then you would use the value '2'.:

$title = $adoRs.Fields("title").value

$title = $adoRs.Fields(2).value

Close the connection unless you have a specific reason for leaving your connection open:

$adoCon.Close

Then test the results with the MsgBox():

MsgBox(0,"testing",$title)

So here is the entire code:

Local $title
$adoCon = ObjCreate("ADODB.Connection")
;$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $dbname) ;Use this line if using MS Access 2003 and lower
$adoCon.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $dbname) ;Use this line if using MS Access 2007 and using the .accdb file extension
$adoRs = ObjCreate ("ADODB.Recordset")
$adoRs.CursorType = 1
$adoRs.LockType = 3
$adoRs.Open ($query, $adoCon)
$title = $adoRs.Fields("title").value    ;Retrieve value by field name
;$title = $adoRs.Fields(2).value         ;Retrieve value by column number
$adoCon.Close
MsgBox(0,"testing",$title)

In this article, we showed how to connect to an Access 2007 or 2003 database using AutoIt. This article discusses how to retrieve one record or row and only one field. In a future article, we'll describe how to retrieve multiple records in an array.

Victor Kimura

AutoIt Tutorial, tips, guides. Learn AutoIt tips. Victor Kimura
Vista Tutorial Please view the original article to properly view the code and step-by-step snapshots:AutoIt:Connect to Access Database

Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Programming Articles
  • More from Victor Kimura

Integrating facebook in cakephp

By: Digisha Modi | 30/12/2009
FaceBook Application is the powerful tool of marketing or to connect people for all type of business. Now it's easier to find social applications on Facebook, on your phone, and on external websites. The new "Featured by Facebook" section highlights Facebook's favorites. Since 2007 the kind of concept FaceBook has come up with become a unique challenger to established payers. The benefit of FaceBook application is any one can post their own application with out any cost.

Outsourcing of iPhone Application Development Offers Profits for Developers as Well as Users

By: Arun Kumar | 30/12/2009
Being a 3G phone, it offers users the advantage of thrilling web access which helps them being on top of all online activities even on the move. Also, there are several additional applications available for download for the device. Thanks to the efforts of the outsource iPhone application development firms which have helped iPhone users get more with their devices using the apps developed by their expert developers.

Magento Template Integration

By: Digisha Modi | 30/12/2009
If you have purchased Magento template or simply need to integrate an existing graphic design template into your e-commerce shop we will integrate it into Magento for you. A web template is a ready- made web design need to be integrated. Usually web templates make possible a standardized layout (page arranging, colors, positions, etc.) for different web contents while using the same basic layout.

Magneto Template Designing

By: Digisha Modi | 30/12/2009
If you want your magento store to be very attractive and user friendly then we at Biztech Consultancy do it for you. We help in customizing the appearance of your Magento e-commerce store with Magneto’s powerful theming engine. Basically Magento theme is combination of layout, template and/or skin file(s) that create the visual experience of your store.

Pass4side 000-135 practice test

By: Adela1987 | 30/12/2009
Pass4side 000-135 Details: Pass4side is your source for the IT Exam 000-135 exam. With our 000-135 Exam Resources, you can be rest assured that you will Pass your 000-135 Exam on Your First Try. Our Exams are written and formatted by Top senior IT Professionals working in today is prospering companies and data centers. All of our practice exams including the IBM 000-135 exam guarantee you success on your First Try. 000-135 is a challenging exam,with our 000-135 study guide,you can feel safe wit

Open Source Development

By: Digisha Modi | 30/12/2009
Open Source Software are free applications released under special licensing terms where the core coding is viewable and able to be edited to suit the needs of the user. Open Source applications cover a myriad of uses - from entertainment to enterprise ecommerce. Open source software like Joomla, magento and osCommerce are very popular and widely used. They are content management system and it maintains track of every piece of content including music, videos, text, widgets, images and documents.

Cakephp PhpBB Integration

By: Digisha Modi | 29/12/2009
PhpBB Forum is a popular bulletin board script written in the server side scripting language PHP. Installable on most web servers, it is free open source Framework available under the GNU public license. The software allows website and forum administrators to set up unlimited number of forums and categories. It is also possible to create users, moderators and user groups with varying degrees of permissions.

Cakephp Amazon API Integration

By: Digisha Modi | 29/12/2009
The Amazon Web Service lets your cakephp web applications make queries to the huge database of products at Amazon and extract as much information as you need. The information you get in your application is exactly the same that is available on the Amazon website at that instant. The Amazon Web Service returns data in real-time, and updates automatically.

Thunderbird Cannot Connect to Server-Download Email Problem

By: Victor Kimura | 07/07/2009 | Software
Thunderbird cannot connect to server problem. How to resolve the problem if you have trouble downloading email from the server and the hour glass figure keeps displaying. Could be related to a virus, or Norton attempting to scan the infected email or a process error of Thunderbird.

Javascript Validate Name Field

By: Victor Kimura | 07/07/2009 | Programming
Simple Javascript tutorial on validating a name field. Checks to see if there is a value in the name field with Javascript after the user submits a form.

PHP PDO MySQL:Simple Example Connecting to MySQL with PDO Class

By: Victor Kimura | 19/06/2009 | Programming
A simple example on how to connect to a MySQL database with PHP PDO class. Possible errors in coding using PDO and short discussion on PDO error handling from MySQL results.

AutoIt: How to copy a file's contents and paste using the clipboard

By: Victor Kimura | 19/06/2009 | Programming
AutoIt tutorial-How to copy text and paste it from the clipboard using variables. How to copy a file and its name to another directory.

AutoIt:Connect to Access Databases:mdb & accdb files

By: Victor Kimura | 15/06/2009 | Programming
AutoIt tutorial discussing how to connect to an Access 2007 and 2003 database. .mdb and .accdb file extensions connections are made. How to retrieve a single record and place a field's value into a variable.

IMAP Multiple Connections / Processes Problem:Site Down

By: Victor Kimura | 15/06/2009 | Networks
IMAP with multiple connections can cause problems and bring down your website. This could be due to multiple IMAP processes that linger. You need to kill these IMAP processes in the control panel.

Web Hosting Transfer Domain:Transferring Domain to Another Provider or Account

By: Victor Kimura | 11/06/2009 | Networks
How to transfer domains to another web hosting provider or to another web hosting account (with the same provider). Step-by-step instructions for transferring your domain safely.

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.06, 1, w3)