Click here to Skip to main content
6,295,667 members and growing! (15,292 online)
Email Password   helpLost your password?
Database » Database » SQL     Intermediate License: The Code Project Open License (CPOL)

A Query Builder Class For Visual Basic 2005 Hand Coders

By Elizalde G. Baguinon

Most programmers prefer hand-coding database queries. This is a class that automates query building, while the code is as readable as the program logic.
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), SQL, .NET (.NET 2.0), Visual Studio (VS2005)
Posted:13 Nov 2008
Views:8,079
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 3.18 Rating: 4.09 out of 5

1
1 vote, 16.7%
2

3
2 votes, 33.3%
4
3 votes, 50.0%
5

Introduction

This is a Visual Basic class that semi-automates SQL query building. It is useful for database queries with numbers of columns that you find very hard to code every comma, quote, or field name.

Background

I am mostly familiar with the classic ASP and PHP programming methods. I do not usually use designers to create business logic and code implementations. While ADO.NET's classes are easy to understand and implement, I wanted some more coding to my heart's content. This class originated from classic ASP (VBScript), then moved to a VB6 class, to a PHP class, and finally to a Visual Studio 2005 (Visual Basic) class.

Using the code

Download the class file (QueryBuilder.zip) above and add it to your Visual Basic project. A typical use of this class is the SELECT query to a database. We will use a table called company.

Dim m_Qry As QueryBuilder
        
'/* Create the builder object */
m_Qry = New QueryBuilder
m_Qry.TableName = "company"
'/* Set the command type, 
'the default setting is QueryBuilder.CommandQuery.cqSELECT */
m_Qry.CommandType = QueryBuilder.CommandQuery.cqSELECT
'/* Add a filter. Each new filter will be 
'  considered as AND. To execute an OR or LIKE, create a string that looks
'  like this: m_Qry.AddFilter("Company_Id LIKE 'My%'")
'*/
m_Qry.AddFilter("Company_Id=1")
'/* Add Columns */
m_Qry.AddField("Company_Number")
m_Qry.AddField("Short_Name")
m_Qry.AddField("Full_Name")
m_Qry.AddField("Address1")
m_Qry.AddField("Address2")
m_Qry.AddField("City_Name")
m_Qry.AddField("Phone_Number")
m_Qry.AddField("Fax_Number")

'/* if you want to sort, just add: */
m_Qry.AddOrder("Full_Name")
'/* Generate the SQL */
Dim m_SQL As String = m_Qry.GetQuery()

'/* Try to display the output */
Debug.Print m_SQL

Note: To create joined queries, the TableName property should contain all the JOIN syntax (e.g., Company INNER JOIN Department ON Company.Company_Id=Department.Company_Id). The columns should include the table name or alias names, like company.Company_Number or A.Company_Number.

This class is very useful in queries that have many columns to add and set values. Look at the example below for an UPDATE:

Dim m_Qry As QueryBuilder

'/* Create the builder object */
m_Qry = New QueryBuilder
m_Qry.TableName = "company"
'/* Set the command type, the default setting is QueryBuilder.CommandQuery.cqSELECT */
m_Qry.CommandType = QueryBuilder.CommandQuery.cqUPDATE
'/* Add a filter. Each new filter will be 
'  considered as AND. To execute an OR or LIKE, create a string that looks
'  like this: m_Qry.AddFilter("Company_Id LIKE 'My%'")
'*/
m_Qry.AddFilter("Company_Id=1")
'/* Add Columns and Values */
m_Qry.AddFieldValuePair("Company_Number", ."MyCompanyNumber001", True)
m_Qry.AddFieldValuePair("Short_Name", "EGBC", True)
m_Qry.AddFieldValuePair("Full_Name", "EagleBush Software", True)
m_Qry.AddFieldValuePair("Address1", "Some Address in the Philippines", True)
m_Qry.AddFieldValuePair("Address2", "My Other Address in the Philippines", True)
m_Qry.AddFieldValuePair("City_Name", "Mandaluyong City", True)
m_Qry.AddFieldValuePair("Phone_Number", "+639174169922", True)
m_Qry.AddFieldValuePair("Fax_Number", "1800-FAXME", True)

'/* Generate the SQL */
Dim m_SQL As String = m_Qry.GetQuery()

'/* Try to display the output */
Debug.Print m_SQL

As for the INSERT command, we will just remove the AddFilter() function, and modify the command type.

Dim m_Qry As QueryBuilder

'/* Create the builder object */
m_Qry = New QueryBuilder
m_Qry.TableName = "company"
'/* Set the command type, the default setting is QueryBuilder.CommandQuery.cqSELECT */
m_Qry.CommandType = QueryBuilder.CommandQuery.cqINSERT
'/* Add Columns and Values */
m_Qry.AddFieldValuePair("Company_Number", ."MyCompanyNumber001", True)
m_Qry.AddFieldValuePair("Short_Name", "EGBC", True)
m_Qry.AddFieldValuePair("Full_Name", "EagleBush Software", True)
m_Qry.AddFieldValuePair("Address1", "Some Address in the Philippines", True)
m_Qry.AddFieldValuePair("Address2", "My Other Address in the Philippines", True)
m_Qry.AddFieldValuePair("City_Name", "Mandaluyong City", True)
m_Qry.AddFieldValuePair("Phone_Number", "+639174169922", True)
m_Qry.AddFieldValuePair("Fax_Number", "1800-FAXME", True)

'/* Generate the SQL */
Dim m_SQL As String = m_Qry.GetQuery()

'/* Try to display the output */
Debug.Print m_SQL

The DELETE command only requires filters. Please be careful not to omit the AddFilter() function, or all your data will be deleted. The class should warn you for DELETE queries with no filters.

Points of interest

I still use this class in most of my projects when data manipulation is required. This practice enables me to seamlessly transfer from one programming language to another. Also, this practice of creating readable code makes it easy to port from one scripting language to another, where program designers mostly fail.

History

The code is not complete as it should be for general usage. I add methods and properties as time goes by, or as is required by the application I'm doing. If you have implemented a more robust and elegant code than my own, please give me a copy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Elizalde G. Baguinon


Member
I started programming when I was the Philippine Women's University in Taft Ave, Manila. I was an Audio Visual Technician then. My interest in programming started when I noticed there are so many forms required and filled up with the clients and so many papers to organize.

My break came when I was hired for a contractual Systems Developer for Lyceum of the Philippines. There, I had designed databases, web applications and client-server applications such as the Mynerva Library System.

I am currently working for a solutions company where MIFARE SmartCards for PC application is the main product.
Occupation: Software Developer (Senior)
Company: SmartPoint Solutions Corp.
Location: Philippines Philippines

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
RantVote for Me!!! PinmemberElizalde G. Baguinon19:19 14 Dec '08  
GeneralQuestion - Does this handle relations (joins)? PinmemberTechpro6:12 19 Nov '08  
GeneralRe: Question - Does this handle relations (joins)? PinmemberElizalde G. Baguinon15:11 19 Nov '08  
GeneralQueryBuilder Fix PinmemberElizalde G. Baguinon14:57 16 Nov '08  
GeneralRe: QueryBuilder Fix Pinmemberddelft5:38 18 Nov '08  
QuestionLooks good but a compile error - 'Private m_DF As Functions' PinmemberDean_DOT6:49 14 Nov '08  
AnswerRe: Looks good but a compile error - 'Private m_DF As Functions' Pinmemberddelft6:56 14 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by Elizalde G. Baguinon
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project