Unit – 3

Objects in

An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object. Objects are things that you can program with, things that make programming easier. They contain a set of methods and properties that allow you to make the object do certain things without actually having to look at the objects code. For example, you have probably used the following statement many times:

Text1.Text = " "

Remember that? Well, what you are doing is setting the property Text, of the text box object Text1 to equal nothing. Objects are really easy, In fact, all the controls you see on your VB toolbox are all objects. For a better look at objects, either press F2 or click View, Object Browser. This nifty little window allows you to see all the properties and methods of the objects currently loaded. Although Visual Basic is not a completely object orientated language it does allow us to use objects in our code.

You can declare an object using either the Dim, Private or Public keyword. As with other variables, the Private and Public keywords can only be used in the General Declarations procedure. You will probably know that declaring something as Public allows anyone to access the variable, and declaring it as Private only lets the current module of code access the variable.

Code: Private m_strName As String

Well, here is a simple object variable declaration:

Code: Private m_clsClass1 As Class1

VB allows you to use the New keyword when you declare an object variable:

Code: Private m_clsClass1 As New Class1

When your program starts, the object is automatically created so you can use its methods etc immediately. If your object fires up a connection a and gets some records when you start, or if your object simply contains a large amount of code, then you may be causing the users machine to run slower then needed. If you don't make a call on the object for a few minutes, then you are wasting memory by having the object in memory.

So, what can you do? Well, simply avoid using the New keyword when declaring the object. Every time you want to use it, create a new instance of it and then destroy it When dealing with objects you use the Set keyword to perform some operations. When we want to destroy an object, we set it to nothing, quite literally:

Code: Set m_clsClass1 = Nothing This way, we only have it loaded in memory when we need to use it. If you have a lot of users using a database object for example, then you may want to open and close connections for each user. This is called object 'pooling' where you pool all your resources together and dish them out when requested. makes this easier by giving you Microsoft Transaction Server.

Objects let you declare variables and procedures once and then reuse them whenever needed. For example, if you want to add a spelling checker to an application you could define all the variables and support functions to provide spell-checking functionality. If you create your spelling checker as a class, you can then reuse it in other applications by adding a reference to the compiled assembly. Better yet, you may be able to save yourself some work by using a spelling checker class that someone else has already developed.

Each object in Visual Basic is defined by a class. A class describes the variables, properties, procedures, and events of an object. Objects are instances of classes; you can create as many objects you need once you have defined a class.

To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for example size and shape. The class is used to create objects. The objects are the cookies.

Two examples in Visual Basic might help illustrate the relationship between classes and objects. • The controls on the Toolbox in Visual Basic represent classes. When you drag a control from the Toolbox onto a form, you are creating an object — an instance of a class. • The form you work with at design time is a class. At run time, Visual Basic creates an instance of the form's class — that is, an object.

Objects newly created from a class are often identical to each other. Once they exist as individual objects, however, their variables and properties can be changed independently of the other instances.

To create an object from a class

1. Determine from which class you want to create an object. 2. Write a Dim Statement to create a variable to which you can assign a class instance. The variable should be of the type of the desired class.

Dim nextCustomer As customer

3. Add the New Operator keyword to initialize the variable to a new instance of the class.

Dim nextCustomer As New customer

4. You can now access the members of the class through the object variable.

nextCustomer.accountNumber = lastAccountNumber + 1

How to build Object models:

You can create your object model from an existing database and use the model in its default state. You can also customize many aspects of the model and its behavior.

If you are using Visual Studio, you can use the Object Relational Designer to create your object model.

If you do not have an existing database and want to create one from an object model, you can create your object model by using your code editor and Create Database

Documentation for the O/R Designer provides examples of how to generate a Visual Basic object model by using the O/R Designer.

UNIT -4

Data Access Objects:

Data Access Over the years, Visual Basic has become an application development tool used for developing Client/Server applications. This is an area of Visual Basic that seems to be getting better and better with every new version. For instance, Visual Basic 6.0 has introduced a new way of accessing data through ADO & OLEDB. And Microsoft says you stick to ADO because that is the future. Old methods like DAO and RDO are already obsolete. If you are wondering about all the stuff I mentioned, I would say, "do not worry". All that is part of data access and we will thoroughly discuss about DAO, RDO and ADO in the next couple of chapters.

What is Data Access? Data access is a feature of Visual Basic that allows you to access and manipulate any database from Visual Basic. The database may be either MS-Access database or FoxPro database or it may also be any of the relational such as Oracle. You can develop applications in Visual Basic that can access data in a database. The database may be on the same machine as the application or it may be on database server that is far away from Visual Basic application. Whatever may be the case, you can access and manipulate the data using applications written in Visual Basic.

The following are the various ways of access database:

Data Access Objects (DAO) This is an object model that has a collection of objects using which you can access a database. This model gives complete control on the database. This model uses Jet Engine, which is the native database engine used by Visual Basic and MS-Access. This was the first model to be used in Visual Basic. Though it is possible to access any database using this, it is particularly suitable for MS-Access database and not suitable for ODBC data sources such as Oracle and MS-SQL Server. So, Microsoft later introduced RDO.

Remote Data Objects (RDO) These objects are only used to access ODBC data sources such as Oracle. These objects access databases that are on remote machine (database server). This object model has less number of objects compared with DAO and more suitable for accessing remote databases. We will discuss more about RDOs in chapter 18.

ActiveX Data Objects (ADO) Microsoft has introduced a new object model called ActiveX Data Objects (ADO), which is based on ActiveX technology, for the first time in Visual Basic 6.0. This object model has very few objects and it is based on OLE DB interface. OLE DB interface is a new interface (replacing ODBC and others), through which you can access data of all formats in the same manner. ADO uses OLE DB providers to access the data. That means each database is accessed through OLE DB provider. And ADO provides the programming framework to access OLE DB. ADO is also much easier to deal with.

That is all about the introduction to data access methods. If you could not understand every part of that, don’t worry. As we unfold things, you will start understanding all about data access. But one thing is for sure. Visual Basic has got so many ways of accessing data (various object models and controls). It is certainly going to confuse a beginner. Also remember, that there is no single method that is efficient in all circumstances. Given the task on hand, choose the best-suited method. My personal suggestion is – use ADO as much as you can. Because that is what Microsoft heralds as the future.

Accessing MS-Access database

First, let us understand how to access data using a very simple and old method – using data control. Data control is one of the standard controls used to provide access to data. Data control internally uses DAO to access data. It means when we use data control, we are using DAO to access data. As we will understand later, though we use data control initially, without using DAO objects and their methods you cannot go much further with simple data control.

OLE DB: OLE DB (Object Linking and Embedding, Database, sometimes written as OLEDB or OLE-DB), an API (Application Program Interface) designed by Microsoft, allows accessing data from a variety of sources in a uniform manner. The API provides a set of interfaces implemented using the (COM); it is otherwise unrelated to OLE. Microsoft originally intended OLE DB as a higher- level replacement for, and successor to, ODBC, extending its feature set to support a wider variety of non-relational databases, such as object databases and spreadsheets that do not necessarily implement SQL. It is based on Component Object Model (COM) and is a part of Microsoft Data Access Components (MDAC) stack. OLEDB is advanced than ODBC as it is capable of accessing data from non- relational databases that do not use SQL. Moreover, OLEDB divides data sources from the application. It is because different applications require different types of data and it is not important to know how to access them with all technical methods. There are two major sections in OLEDB called consumer and provider. The consumer obtains data while the provider gives data to the consumers.

OLE DB separates the data store from the application that needs access to it through a set of abstractions that include the data source, session, command, and row sets. This was done because different applications need access to different types and sources of data, and do not necessarily want to know how to access functionality with technology-specific methods. OLE DB is conceptually divided into consumers and providers. The consumers are the applications that need access to the data, and the providers are the software components that implement the interface and thereby provides the data to the consumer. OLE DB is part of the Microsoft Data Access Components (MDAC) stack.

ODBC is constrained to relational data stores; OLE DB supports all forms of data stores (relational, hierarchical, etc) In general, OLE DB provides a richer and more flexible interface for data access because it is not tightly bound to a command syntax (like SQL in the case of ODBC).

The main difference between ODBC, OLEDB and JDBC is that the ODBC is an API developed by Microsoft to access relational databases and OLEDB is an API developed by Microsoft to access both relational and non-relational databases while JDBC is an API developed by Oracle to access the relational and non- relational database. When developing software, it is necessary to connect the application to the database. For example, assume a medical centre management system. The programmer has to write the code to insert, update and delete patient records, doctor records, etc. These systems always exchange data with a database. ODBC, OLEDB and JDBC are three APIs that allow connecting the application to a database in order to access data. Data Bound Controls: Bound controls are ones that are tied to a specific data source within your database such as a field and a table or a query. Values can be either text, dates, number, check boxes, pictures or even graphs. You use bound controls to display values that come from fields in your database.

The data-bound List control is similar to the regular ListBox control and can be populated with an entire column of a Record Set. The data-bound List control has all the functionality of the regular List Box control, and in addition, allows the enter new values (records). Follow the below link to use data bound control-- https://www.educator.com/computer-science/visual-basic/snape/using-databound-controls.php If you want to have data-bound controls on second form, you must place a data control on that form. To display data on the data-bound control that you are using like labels or textboxes. You need set it?s data source property and data field name which is column name form the table.

We create the controls, such as labels and text boxes, to display the actual data. Each control is a bound to particular field in the table. In this example the label is called a data bound control and automatically displays the contents of bound field when the project runs.

Using DAO to build simple database interface in Visual basic:

There's more than one way to get at data from Visual Basic. DAO was the first, and it's still viable. We kick off our series on data access with VB by looking at how you can implement DAO in your applications.

When it comes to implementing a data access solution in your VB applications, you currently have three choices: Data Access Objects (DAO), Remote Data Objects (RDO), and ActiveX Data Objects (ADO). In this series of articles, we will examine each of these options, noting their similarities and differences. We'll also look at some cases where one is better suited for a specific task than another. Let's start with a look at DAO.

DAO basics-- DAO, which was created before RDO and ADO, is a set of objects that enables client applications to programmatically access data. But DAO doesn't just let you access data—it also lets you control and manage local and remote databases in various formats. Using DAO, you can create and modify the database structure; create tables, queries, relationships, and indexes; retrieve, add, update, and remove data; implement security; work with different file formats; and link tables to other tables.

DAO objects: To understand DAO better, let's look at the DAO objects in Figure A.

The DBEngine is the highest-level object in the DAO object model. It contains all other objects and collections. The Database object is the member of the Databases collection of the default Workspace object, which is a member of the Workspaces collection of the DBEngine object.

With DAO, objects you use to work with the data in the database are generally not saved but are created every time you need them. Objects that represent the structure of the database are saved with the database. When you create a new DAO object to be saved in the database, you have to append it to the appropriate collection using the collection's Append method. Keep in mind that all DAO objects are indexed beginning with zero.

DAO lets you work with three database formats:  · Microsoft Jet (all databases that are created with the Microsoft Jet database engine)  · Installable ISAM format  · ODBC data sources

Jet and ISAM data use the Microsoft Jet object model; however, with ODBC data, you can use either Microsoft Jet or ODBCDirect. There are some limitations in accessing ODBC data using Jet, although you can use it if you need to take advantage of a particular Jet feature. But ODBCDirect is more flexible, allowing you to run queries or stored procedures against the backend server and perform batch updates and asynchronous queries. It also makes your application run faster because it allows you to bypass Microsoft Jet's middle layer.

Follow the below link (videos) carefully more about Crating database and connect it to VB…

https://www.youtube.com/watch?v=h-JEaAN4cJM

https://www.youtube.com/watch?v=3N4sIWgmYPE&t=54s

https://www.youtube.com/watch?v=7pGEwQhwcrQ

Visual Basic Report Designer:

After learned how to build a database in Visual Basic 6 & we have not learned how to display the saved data in a report. Reports are important and useful in many respects because they provide useful and meaningful information concerning a set of data. Steps in building your report in Visual Basic 6, follow the below link:

https://www.vbtutor.net/lesson40.html