Connect to an Oracle Database from Visual Basic 6 (Part 2)



Similar documents
Use the ADO Control in your Visual Basic 6 projects

Connect to an Oracle Database from within Visual Basic 6 (Part 1)

Access 2000 and Visual Basic 6

Create an Access Database from within Visual Basic 6.

Siemens Applied Automation Page 1 11/26/03 9:57 PM. Maxum ODBC 3.11

Connecting to an Excel Workbook with ADO

Add an Audit Trail to your Access Database

Crystal Reports. Overview. Contents. Connecting the Report Designer Component to a Data Source

High-Performance Oracle: Proven Methods for Achieving Optimum Performance and Availability

Visual Basic Database Connectivity

Visual Basic Database Programming

COURSE DESCRIPTION. Queries in Microsoft Access. This course is designed for users with a to create queries in Microsoft Access.

Introduction to Visual Basic

Getting Started Configuring Your Computer Network Settings

Crystal Reports for Visual Studio.NET

Managing Files. On a PC, after you find your file, right click it and selet Rename from the pop-up menu.

AuditWizard v8. SQL Maintenance

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

High Performance Websites: ADO versus MSXML

Welcome to Ipswitch Instant Messaging

How to Process Opt-Out Reqeusts in GroupMail (Subscriber Add-On)

Getting Started with STATISTICA Enterprise Programming

Sharepoint. Overview. Key features of SharePoint:

Before you may use any database in Limnor, you need to create a database connection for it. Select Project menu, select Databases:

Creating a New Excel Survey

Steps for Upgrading from Version 4.0 or 5.0 to Version 6.0

Access 2003 Macro Security Levels, Sandbox Mode, and Digitally Signed Files

Content Author's Reference and Cookbook

Addendum 3. Do not install Service Pack 3 if you use Oracle 8! Oracle 8 is no longer supported and will not operate with SP3.

Lesson 07: MS ACCESS - Handout. Introduction to database (30 mins)

Accessing Database Information Using Visual Basic:

Microsoft Access is an outstanding environment for both database users and professional. Introduction to Microsoft Access and Programming SESSION

Google Drive lets you store and share all your stuff, including documents, videos, images and other files that are important to

Hands-On Lab. Client Workflow. Lab version: Last updated: 2/23/2011

Newsletter Sign Up Form to Database Tutorial

HOW TO PURCHASE AND INSTALL YOUR VERISIGN DIGITAL SIGNATURE

Account Create for Outlook Express

Linking Access to SQL Server

Visual Basic 6 Error Handling

Internet Explorer 7 for Windows XP: Obtaining MIT Certificates

How to move to your account with MAC Mail

5 Airport. Chapter 5: Airport 49. Right-click on Data Connections, then select Add Connection.

ACCESSING IBM iseries (AS/400) DB2 IN SSIS

Integrating Web & DBMS

Content Author's Reference and Cookbook

Migration Manager v6. User Guide. Version

Using Delphi Data with Excel and Access

for esigntrust Personal Secure Enrollment and Generation Guide Operation Guide Microsoft Windows System & Internet Explorer Users

How to Back Up and Restore an ACT! Database Answer ID 19211

Install SQL Server 2000 for optimal usability

Sharing the Argos database. Sharing the same database between several Argos users

When you publish data to a SharePoint site, you first

Setting Oracle Passwords for the EDW

ODBC (Open Database Communication) between the ElevateDB Database and Excel

Hands-On Lab. Building a Data-Driven Master/Detail Business Form using Visual Studio Lab version: Last updated: 12/10/2010.

What is a Mail Merge?

Outlook 2007: Managing your mailbox

Introduction to Microsoft Access 2003

How To Create A Checkbox In Visual Basic (For A Powerpoint) With A Check Box At Run Time

MOVING THE SENIOR DEVELOPMENT CLASS FROM WEB DEVELOPMENT TO LIFE CYCLE DEVELOPMENT A CASE FOR VISUAL STUDIO 2005

Remote Desktop Solution, (RDS), replacing CITRIX Home Access

Use the Package and Deployment Wizard to distribute your Visual Basic 6 programs

Importing TSM Data into Microsoft Excel using Microsoft Query

Microsoft Office 2010

Cloud Services. Lync. IM/ Web Conferencing Admin Quick Start Guide

User's Guide. Using RFDBManager. For 433 MHz / 2.4 GHz RF. Version

eadvantage Certificate Enrollment Procedures

Excel Companion. (Profit Embedded PHD) User's Guide

Over the past several years Google Adwords has become THE most effective way to guarantee a steady

REDUCING YOUR MICROSOFT OUTLOOK MAILBOX SIZE

MSSQL quick start guide

Learning Services IT Guide. Access 2013

SQL Direct User Guide

BulkSMS Text Messenger Product Manual

Remote Access: Internet Explorer

Working with Office Applications and ProjectWise

Microsoft Office 365 Exchange Online Cloud

MS WORD 2007 (PC) Macros and Track Changes Please note the latest Macintosh version of MS Word does not have Macros.

Getting Started The Windows SharePoint Services Window

Chapter 4 Accessing Data

Chapter 1: Getting Started

Sage Intelligence Financial Reporting for Sage ERP X3 Version 6.5 Installation Guide

Microsoft Business Intelligence 2012 Single Server Install Guide

StarterPlus Mailbox Software Setup Guide

ASP.NET Programming with C# and SQL Server

How To Integrate SAP Business Data Into SharePoint 2010 Using Business Connectivity Services And LINQ to SAP

Installing LearningBay Enterprise Part 2

Visual COBOL ASP.NET Shopping Cart Demonstration

BarTender s ActiveX Automation Interface. The World's Leading Software for Label, Barcode, RFID & Card Printing

Initial Setup of Mozilla Thunderbird with IMAP for Windows 7

- SAP BusinessObjects and Xcelsius articles, links and ressources

SMS for Outlook. Installation, Configuration and Usage Guide

IGEL Universal Management Suite migration

CRM Setup Factory Installer V 3.0 Developers Guide

Set Up Instructions

Book 3. Database Connectivity. U:\Book\Book_03.doc Database Connectivity

Transcription:

Connect to an Oracle Database from Visual Basic 6 (Part 2) Preface This is Part 2 in a 2 part series on using Visual Basic 6 to connect to an Oracle database. In Part 1, I showed you how to use an ADO Data Control to make the connection. In this article, I show you how to use ADO Objects to make the connection. Not only will the article be useful for the Oracle programmers in the audience, it will also show you how to use ADO objects in your program. ADO Objects What's an Object? Hopefully you've read by well-received book, Learn to Program with Visual Basic 6 Objects. As much as I would love to spend a lot of time discussing object variables in detail, I'm going to presume that you know what they are and how, in a nutshell, they refer to an object instantiated from a class or template. Having said that, I will tell you that the ADO Object model can be confusing, particularly because there are frequently many ways to achieve the same functionality. In this article, we will create an ADO Connection Object, then use an ADO Command Object to return the results of a query to an ADO Recordset object. Finally, as an added bonus, I'll show you something that your average programmer doesn't know can be done--we'll set the Recordsource of an ADO DataGrid to the recordset we produce in code. In other words, without the ADO Data Control, we'll populate a DataGrid. Similar to what we did when we added the ADO Data Control to our Visual Basic Toolbox, in order to use ADO Objects we must first set a reference to the ADO Object Library. The version of the ADO Object Library installed on your PC will vary depending upon a number of factors-the PC I'm using as I write this article contains ADO Version 2.x. To add a reference, select Project-References from the Visual Basic Menu Bar, then select Microsoft ActiveX Data Objects 2.6 Library. If the version number doesn't match yours exactly, that's fine---just be sure to select the ActiveX Data Objects Library, not the ActiveX Data Objects Recordset Library that follows it in the list. Be sure that you select Project-References, NOT Project-Components. Object libraries have no visible interface, and so they won't appear in the http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (1 of 8)3/28/2004 11:35:52 AM

Components section. When you click on the OK button, nothing obvious happens--but you now have access to ADO Objects in your program. Before we get too far, let's start the process of adding the ADO DataGrid Control to our form. In order to do that, we need to select Project-Components (yes, that's right, Components this time) from the Visual Basic Menu Bar, and select Microsoft DataGrid Control 6.0. http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (2 of 8)3/28/2004 11:35:52 AM

When you click on the OK button, the DataGrid Control (the ADO version) is added to the Visual Basic Toolbox. http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (3 of 8)3/28/2004 11:35:52 AM

With the DataGrid control in your Toolbox, now it's time to add it to your form... http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (4 of 8)3/28/2004 11:35:52 AM

Working with ADO Objects to achieve your Oracle Connection The Connection Property It was at this point in Part 1 of this article that we adjusted properties of the ADO Data Control to achieve a Connection to an Oracle Database, and to build a Recordset which was then used to populate the ADO DataGrid. Here, we'll use an ADO Connection Object and a Recordset Object to achieve the same functionality. For demonstration purposes, let's place this code in the Load Event Procedure of the Form... Dim oconn As New ADODB.Connection http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (5 of 8)3/28/2004 11:35:52 AM

Dim rs As New ADODB.Recordset Dim strsql As String strsql = "SELECT * FROM EMPLOYEES" Set oconn = New ADODB.Connection oconn.open "Provider=msdaora;Data Source=John.world;User Id=jsmiley; Password=neveruseyourdogsnameasyourpassword;" rs.cursortype = adopenstatic rs.cursorlocation = aduseclient rs.locktype = adlockoptimistic rs.open strsql, oconn,,, adcmdtext Set DataGrid1.DataSource = rs What's going on? Let me explain. These first two lines of code declare 2 object variables. The first is an ADO Connection Object Dim oconn As New ADODB.Connection followed by an ADO Recordset Object Dim rs As New ADODB.Recordset Both of these declarations are necessary in order to build a Recordset--which is a virtual (in memory) representation of an actual Oracle table in our Database. The names for these two object variables are pretty standard, and you see them all the time in code and in books and articles. In fact, if you search for oconn or rs using a Google search, you'll gets lots of results for ADO. The final variable declaration is a String variable that we will use to 'hold' the SQL statement used to build our recordset. Dim strsql As String Once we've declared the strsql variable, it's time to assign a SQL statement to it. You may remember this statement from Part 1 of this article--it's used to retrieve every record from the Employees table in my Oracle database... strsql = "SELECT * FROM EMPLOYEES" Our next step is to open our ADO Connection. We do that by executing the http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (6 of 8)3/28/2004 11:35:52 AM

Open method of our Connection Object. The Open method is looking for four parameters: the Provider name, the Data Source (or HostName), the User ID and the Password of the database in question. oconn.open "Provider=msdaora;Data Source=John.world;User Id=jsmiley; Password=neveruseyourdogsnameasyourpassword;" Do the details of the Open Method look familiar? They should. In Part 1 I told you that the Connection String that the ADO Data Control wizard built for us could be used to open a Connection in code. In fact, some programmers use the ADO Data Control wizard to build their Connection Strings for them, and then copy and paste them into the code window. Before we build the Recordset object (not the same as the Connection object) we need to adjust three properties of the Recordset object, the CursorType, CursorLocation and LockType. If you want to know more about the details of the Recordset object, I highly recommend Wrox's ADO 2.6 Programmer's Guide. rs.cursortype = adopenstatic rs.cursorlocation = aduseclient rs.locktype = adlockoptimistic Now it's time to open the Recordset. If you are confused about the difference between a Connection and a Recordset, think of the Connection as the Database window you see when you first start Access. The Database contains a window with a list of tables that you can then select. When you select one, a Data Window opens up with just the information from that table. This is the equivalent of the Recordset. rs.open strsql, oconn,,, adcmdtext In theory, we now have a Recordset object built containing all of the data in the Employees table of my Oracle database. How do we see the Recordset? We can use the Set statement to assign the Recordset object to the DataSource property of our DataGrid. Set DataGrid1.DataSource = rs If we now run the program, the code in the Load Event procedure of the form executes. A Connection object is created, initiating the Connection to my Oracle Database. Then a Recordset object is created, retrieving Employee records. Finally, the DataSource property of the DataGrid is set to point to the Recordset object. Here's our form after the code executes--as you can see, the DataGrid is now populated. http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (7 of 8)3/28/2004 11:35:52 AM

Summary I hope you've enjoyed this article. http://www.johnsmiley.com/cis18.notfree/smiley004/smiley004.htm (8 of 8)3/28/2004 11:35:52 AM