• Archives

  • Blog Stats

    • 15,963 hits
  • Jumpstart your Career and Life …

    visual basic
  • Visual Basic search

    visual basic help | visual basic download | visual basic tutorial | visual basic programming | visual basic for beginners | visual basic examples | visual basic free | Visual Basic Programming VB | learn visual basic | learn visual basic | microsoft visual basic | vb net | download visual basic | microsoft excel visual basic | Visual Basic Tutorials debugging | learn visual basic | microsoft visual basic | vb net | download visual basic | microsoft excel visual basic
  • Blog Catalog

  • Globe of Blogs

  • Blog Search

    Blog Search
  • Submit Now

  • Link with us

    Link With Us - Web Directory

Free Microsoft Visual Basic Download

Many are not aware that they can download and use free of cost, Microsoft Visual Basic 2008 express edition free of cost. Here is the link:

Download Visual Basic 2008

Note that there are simple lessons on the page listed above.

Updating Records using CommandObject in Visual Basic

You can use the SQL Update statement to issue a command that changes a record or group of records. The below code demonstrates how to do this.

Option Explicit
Dim CN As Connection
Dim comobj As Command

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With

With comobj
.ActiveConnection = CN
.CommandText = “UPDATE AccountsTable SET AccountName =’Main Creditor1′ WHERE             AccountCode=’C001′”
.Execute
End With

Inserting a new record in Visual Basic (SQL Insert)

We can use the SQL Insert statement to issue a command that will add a new record in a data source. The following code demonstrates how to use SQL Statements to Insert a record using CommandObject execute method in Visual Basic.

Option Explicit
Dim CN As Connection
Dim comobj As Command

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With
With comobj
.ActiveConnection = CN
.CommandText = “INSERT INTO AccountsTable(AccountCode,AccountName,AccountCat) values(‘C001’,’Creditor1′,’1′)”
.Execute
End With
End Sub

Multiple Command Objects Using a Single Connection in Visual Basic

You can also have multiple Command objects accessing data from  a data source, using the same Connection object.

General Declaration Section

Dim CN As Connection
Dim comobj As Command
Dim comTobj As Command

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command
Set comTobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With

With comobj
.ActiveConnection = CN
.CommandText = “Select * from AccountsTable”
.Execute
End With

With comTobj
.ActiveConnection = CN

.CommandText = “Select * from TranTable”
.Execute
End With
End Sub

Note : The database server treats two different connections as two separate entities, though they may be referring to the same data source. As a result, it assigns separate resources for both the Connection objects. This can adversely affect the performance of your application.

Retrieving data from the SQL Server database Using the Command Object in Visual Basic

A Command object is used to define an SQL command that you can execute on a data source.A Command object requires a Connection object. Data returned as a result of the execution of a command is accepted and stored in a Recordset object. The following diagram shows the relation between a Command object and a Connection object.

With reference to Command Object properties, the below mentioned three properties are essential.

  • ActiveConnection – This property sets or returns the active connection used by a object.
  • CommandText – Stores SQL statement or the name of the stored procedure to execute.
  • CommandType – Indicates the type of command specified in the property.

The following code example demonstrates how to use the CommandObject in Visual Basic. If the CommandText is an SQL statement, then CommandType need not be mentioned. In the General Declaration section, declare the Command object, comobj as shown below.

Option Explicit
Dim CN As Connection
Dim comobj As Command

In the Form_Load event, initialize the ActiveConnection and CommandText properties of the Command object, comobj  as shown below. We need to call the Execute method of the Command object.

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data             Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With

With comobj
.ActiveConnection = CN
.CommandText = “Select * from AccountsTable”
.Execute
End With

End Sub

Creating a Connection object

Creating a Connection object in Visual Basic

The Connection object establishes a connection to a data source. It allows your application to pass client information, such as username and password to the database for validation.

Steps:

  • Set a reference to the ADO Object Library.
  • Declare a Connection object
  • Specify an OLE DB data provider
  • Pass connection information

Set a reference to the ADO Object Library

Before you can use ADO in your Visual Basic application, you must first set a reference to the Microsoft ActiveX Data Objects 2.0 Liobrary. To create a reference to the ADO Object Library:

  • On the Project menu, click References.
  • Select Microsoft ActiveX Data Objects 2.0 Library, and then click OK.

Declare a Connection object

Once you have made a reference to the ADO object library, you can declare Connection object in your application. The following code example code declares and instantiates a new Connection object.

Dim CN As Connection
Set CN = New Connection

Specify an OLE DB data provider

Once you have instantiated a Connection object, you must specify an OLE DB data source provider. You do this by setting the Provider property. The following code specifies the Microsoft SQL Server OLEDB data provider.

CN.Provider = “SQLOLEDB”

Pass Connection information

The final step before establishing a connection to a data source is to specify the connection information. You do this by setting the Connection object’s ConnectionString property. Connection string arguments are provider specific, are passed directly to the provider, and are not processed by ADO.

The following connection string arguments are used with the SQL Server OLE DB provider.

Connection argument

Integrated Security
Data Source
Initial Catalog

Description

SSPI

Name of the remote server

Database name in the external data source

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAcc;Data Source=SYS1”
.Provider = “SQLOLEDB”
End With

Integrated Security is for Windows Authentication

Connecting to a Data Source

Once we have specified an OLE DB data provider and have passed the ConnectionString information, you use the Open method to establish a connection to the data source.

The following example code creates a connection to a Microsoft SQL Server database called FinAcc on the server SYS1 using SQL Server OLE DB provider. Type the following code in the frmAccountDetails form.

Option Explicit
Dim CN As Connection

Private Sub Form_Load()
Set CN = New Connection
With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAcc;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With
End Sub

Closing a connection

CN.Close
Set CN=Nothing

To release the connection to a databse, we need to execute the Close method of the Connection object, and set the Connection object to Nothing. If you choose to release the Connection object whenever the form gets unloaded, then the above code needs to be written in the Unload event of the from.

For more information on connection strings in visual basic.net and c# .net click here.

ADO Objects – using them in Visual Basic Applications

The following topics illustrate how to use ADO Objects to perform the basic database operations using Visual Basic and SQL Server.

  • Creating a Connection object.
  • Retrieving data from the data source.
  • Multiple Command Objects Using a Single Connection.
  • Inserting a new record, updating and deleting a record using the Execute command.
  • Executing a Stored Procedure from a Command object.
  • Executing a Stored Procedure having INPUT parameters using a Command object.
  • Executing a Stored Procedure having INPUT and OUTPUT parameter using a Command object.
  • Creation of Recordset.
  • Creation of RecordSert as a Server-side Recordset and Client-side Recordset.
  • Assigning a Lock for a client-side Recordset object.
  • Navigating a recordset and Binding controls to a recordset.
  • Using a recordset to add a new record delete and modify a record

Each of the above tasks can be used to retrieve data, modification of data, inserting data, navigation and deleting of data. By familiarizing yourself with the above tasks you can handle almost any requirement to handle data.

Connection using Visual Basic 6.0

How to make Form Connection Setting Visual Basic 6.0

Establish Connection – Execute Commands – Use Data Reader

In this method, Connection is established  to the data source,  a Command is used to access and execute commands against the data source, and Data Reader is used to display data to the -application. We will discuss how the above can be achieved. Consider the following code. This code is a part of an application which has one data entry screen and one database. The application when compiled displays data from a database.

This code accomplishes the following tasks.

1.Establish the connection with the FinAccounting database with Connection object.
2.Execute the command (Select statement) with the Command object.
3.The data will be read by the data reader object and display the contents of first record in the textboxes.

Imports System.Data.SqlClient

Public Class Form1
Inherits System.Windows.Forms.Form

Dim str_sql_user_select As String = “SELECT * FROM AccountsTable”
Dim str_connection As String = “Data Source=KRISHNA\VSDOTNET;Integrated                 Security=SSPI;Initial Catalog=FinAccounting”
Dim mycon As SqlConnection
Dim comUserSelect As SqlCommand
Dim myreader As SqlDataReader
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles

MyBase.Load
mycon = New SqlConnection(str_connection)
‘Instantiate the commands
comUserSelect = New SqlCommand(str_sql_user_select, mycon)
TextBox1.Text = “ “
TextBox2.Text = “ “
mycon.Open()
myreader = comUserSelect.ExecuteReader
If (myreader.Read = True) Then
TextBox1.Text = myreader(0)
TextBox2.Text = myreader(1)
Else
MsgBox(“You have reached eof”)
End If
End Sub
End Class

Now let us see which part of the code accomplishes the above tasks.

1.Establish the connection with the  FinAccounting database with Connection object.

The following tasks are accomplished by the code given under this section.

a.Declare the string variable ‘str_sql_user_select’ to hold the SQL statement.
b.Declare string variable ‘str_connection ’ to hold the Connection string.
c.Declare the Connection object ‘mycon’ .
d.Declare the command object ‘comUserSelect’.
e.Declare the Data reader object ‘myreader’  .
f.Instantiate the connection object.

Dim str_sql_user_select As String = “SELECT * FROM AccountsTable”
Dim str_connection As String = “Data Source=AB\VSDOTNET;Integrated Security=SSPI;Initial Catalog=finaccounting”
Dim mycon As SqlConnection
Dim comUserSelect As SqlCommand
Dim myreader As SqlDataReader

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

mycon = New SqlConnection(str_connection)

2.Execute the command  (Select  statement )  with the Command object

The following tasks are accomplished by the code given under this section.

a.Instantiate the Command object.
b.Initialize the textbox controls.
c.Open the Connection.

comUserSelect = New SqlCommand(str_sql_user_select, mycon)
TextBox1.Text = “ “
TextBox2.Text = “ “
mycon.Open()

3.The data will be read by the Data Reader object and display the contents of  first record in the textboxes.

The following tasks are accomplished by the code given under this section.

a.    Execute the command and return rows in DataReader.
b.    Display the contents of the first record in the textbox controls.  The statement myreader(0)
indicates the first field of the current record.

myreader = comUserSelect.ExecuteReader
If (myreader.Read = True) Then
TextBox1.Text = myreader(0)
TextBox2.Text = myreader(1)
Else
MsgBox(“You have reached eof”)
End If

Visual Basic Programming Concepts

In order to understand the application development process, it is helpful to grasp some of the key concepts upon which Visual Basic is built. As Visual Basic is a Windows development language, familiarity with the Windows programming environment is necessary. Understanding the windows programming environment can be classified into the following two steps:

a.    How Windows Works: Windows, Events and Messages
b.    Understanding the Event-Driven Model

a.    How Windows Works: Windows, Events and Messages

Think of a windows as simply a rectangular region with its own boundaries. You are probably already aware of different types of windows: an Explorer window in Windows 95, a document window within your word processing program, or a dialog box that pops up to remind you of an appointment. While these are the most common examples, there are actually many other types of windows. A command button is a window. Icons, text boxes, option buttons and menu bars are all windows.

b.    Understanding the Event-Driven Model

In an event-driven environment the application executes different code sections in response to events. Events can be triggered by the user’s actions, by messages from the system or other applications, or even from the application itself.

The sequence of these events determines the sequence in which the code executes, thus the path through the application’s code differs each time the program runs. Because you can’t predict the sequence of events, your code must make certain assumptions about the “state of the world” when it executes. When you make assumptions (for example, that an entry field must contain a value before running a procedure to process that value), you should structure your application in such a way as to make sure that the assumption will always be valid (for example, disabling the command button that starts the procedure until the entry field contains a value). Typing the text in a text box cause the text box’s Change event to occur. This would cause the code (if any) contained in the Change event to execute. If you assumed that this event would only be triggered by user interaction, you might see unexpected results. It is for this reason that it is important to understand the event-driven model and keep it in mind when designing your application.

Visual Basic Single Point Resource of useful links:

A collection of Visual Basic Links:

Visual Basic Tutorial – Beginners

Visual Basic Tutorials for Newbies

First Visual Basic 6 Program Designing and Writing

Gary Beene’s Visual Basic Tutorials (popular)

VB Helper Tutorials

VB Tutorials Cool ones

Visual Basic journey tips tricks and tutorials

DevCentral – Visual Basic Tutorials

Visual Basic Info. covered on every aspect

Visual Basic Section

Visual Basic Business Objects – A Primer

VB- Beginners and Intermediate Tutorials

VB Tutorials by TegoSoft

Visual Basic Resource: 20 Tutorials

From Visual Basic Call VC++ .DLL

Connecting HTML Help to Visual Basic Programs

How to Create super compact Super Fast C++ Dll for VB

Passing picture from C++ DLL to a VB PicBox

How to write C DLL’s and call them from Visual Basic

Context Sensitive Help for Visual Basic

Optimize SQL Queries in Visual Basic

XML and Visual Basic

Professional Visual Basic 6 XML

XML for Visual Basic Developers (PPT)

Visual Basic and UML (Unified Modeling Language)

Visual Basic Debugging

Visual Basic Error Handling

Visual Basic Coding Standard

Jaz Lichy’s Visual Basic Standards

Effective Visual Basic coding standards enforcement.

VB Law Enforces Coding Standards

Visual Basic Programming Standards

Visual Basic VB Source Code Archives

A1 VB Source Code

DevX: Visual Basic Zone

Free VB Code

Programmers Heaven – Visual Basic Zone

vb source code sites

Visual Basic E-Books and References

New Stuff in Visual Basic 6.0 (Documents = 2.5 MB)

New Stuff in Visual Basic 6.0 (Examples = 630 KB)

Visual basic (VB) and SQL Server (Documents == 4.00 MB)

VB and SQL Server (Examples – 709 KB)

3 Chapters from Visual Basic for Kids

Optimize Size and Speed of Visual Basic Applications

Visual Basic Tips and Tricks

Tim’s Visual Basic 5 Tips and Tricks

Visual Basic: Tips & Tricks (Must)

vbVision – Visual Basic Tips and Tricks

Cool VB Tips Tricks

vbAccelerator Tips

Visual BASIC Land Tips

Top 15 Visual Basic Tips

Tapping into Data with the VB .NET DataBuilder Classes

Executing a Package from Visual Basic

Web Services in Visual Basic .Net

Encrypting a File Using .NET

Implementing Interfaces in Visual Basic.NET

Phone Number Dialer – Code Sample

Thin Client Programming – Learn all the basics

Visual Basic .NET as a Fully Object-Oriented Language

Create Your Own Visual Basic Add-Ins

Building Property Pages

Using the Winsock Control in Client/Server Applications

The Windows Registry

All about windows hooks

Drawing Fast Polygons / Graphics with VB5/6

Handling Screen Resolution

Random Numbers and Tic Tac Toe with Visual Basic .NET

To Avoid Common Database Programming Mistakes in VB
Visual Basic Barcode FAQ & Tutorial

Embedded Visual Basic FAQ

Visual Basic(Microsoft) Databases FAQs

Frequently Asked Questions About VB

Visual Basic FAQ’s

Visual Basic FAQ

VB FAQ

Visual Basic Dot Net

Visual Basic Dot Net Utilities

Books on Dot Net