473,388 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,388 developers and data experts.

User-defined class for SQL Select expressions

FishVal
2,653 Expert 2GB
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox.

The usual way is the following.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo1_AfterUpdate()
  2.  
  3.     Me.Combo2.RowSource = "SELECT ... FROM ... WHERE Table2.keyID=" & Me.Combo1 & ";"
  4.     Combo2.Requery
  5.  
  6. End Sub
  7.  
Till now it looks good, however it has the following disadvantages.
1) It is not suitable to place in the code long string containing SQL Select statement with numerous fields and joins if the only purpose is to change WHERE clause.
2) Each time the RowSource property is changed in Design view the code has to be modified too.

A human being is not obliged to work so hard but a computer has to.

The solution I'm proposing is to write User-defined class.

Class module "SQLSelect".

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Compare Database
  3.  
  4. Dim varClauses(1 To 4) As Variant
  5. Dim strClausesNames(1 To 5) As String
  6. Dim strSQL As String
  7.  
  8. Public Property Get SelectString() As String
  9.     SelectString = Me.SelectExpression & ";"
  10. End Property
  11.  
  12. Public Property Let SelectString(ByVal strNewValue As String)
  13.  
  14.     Dim intCutPosition As Integer
  15.     Dim intCutLength As Integer
  16.     Dim strSubStr As String
  17.  
  18.     strSQL = strNewValue
  19.  
  20.     For i = 1 To 4
  21.         intCutPosition = InStr(1, strSQL, strClausesNames(i), vbTextCompare)
  22.         If intCutPosition <> 0 Then
  23.             intCutPosition = intCutPosition + Len(strClausesNames(i))
  24.             For j = i + 1 To 5
  25.                 intCutLength = InStr(1, strSQL, _
  26.                     strClausesNames(j), vbTextCompare)
  27.                 If intCutLength <> 0 Then
  28.                     intCutLength = intCutLength - intCutPosition
  29.                     strSubStr = Mid(strSQL, intCutPosition, intCutLength)
  30.                     varClauses(i) = Trim(strSubStr)
  31.                     Exit For
  32.                 End If
  33.             Next j
  34.             'Debug.Print strClausesNames(i) & "   <" & varClauses(i) & ">"
  35.         Else
  36.             varClauses(i) = Null
  37.         End If
  38.     Next i
  39.  
  40. End Property
  41.  
  42. Public Property Get What() As Variant
  43.     What = varClauses(1)
  44. End Property
  45.  
  46. Public Property Let What(ByVal vNewValue As Variant)
  47.     varClauses(1) = vNewValue
  48. End Property
  49.  
  50. Public Property Get From() As Variant
  51.     From = varClauses(2)
  52. End Property
  53.  
  54. Public Property Let From(ByVal vNewValue As Variant)
  55.     varClauses(2) = vNewValue
  56. End Property
  57.  
  58. Public Property Get Where() As Variant
  59.     Where = varClauses(3)
  60. End Property
  61.  
  62. Public Property Let Where(ByVal vNewValue As Variant)
  63.     varClauses(3) = vNewValue
  64. End Property
  65.  
  66. Public Property Get OrderBy() As Variant
  67.     OrderBy = varClauses(4)
  68. End Property
  69.  
  70. Public Property Let OrderBy(ByVal vNewValue As Variant)
  71.     varClauses(4) = vNewValue
  72. End Property
  73.  
  74. Private Sub Class_Initialize()
  75.     strClausesNames(1) = "SELECT"
  76.     strClausesNames(2) = "FROM"
  77.     strClausesNames(3) = "WHERE"
  78.     strClausesNames(4) = "ORDER BY"
  79.     strClausesNames(5) = ";"
  80. End Sub
  81.  
  82. Public Property Get SelectExpression() As String
  83.  
  84.     Dim varClause As Variant
  85.  
  86.     SelectExpression = ""
  87.  
  88.     For i = 1 To 4
  89.         If Not (IsNull(varClauses(i)) Or IsEmpty(varClauses(i))) Then
  90.             varClause = strClausesNames(i) & " " & varClauses(i)
  91.             SelectExpression = SelectExpression & varClause & " "
  92.         End If
  93.     Next i
  94.  
  95.     SelectExpression = RTrim(SelectExpression)
  96.  
  97. End Property
  98.  
  99.  

The class has the following properties:

.What - r/w, SELECT clause of SQL Select statement
.From - r/w, FROM clause of SQL Select statement
.Where - r/w, WHERE clause of SQL Select statement
.OrderBy - r/w, ORDER clause of SQL Select statement
.SelectString - r/w, SQL Select statement with trailing ";"
.SelectExpression - r, the same as .SelectString w/o trailing ";"

Now the above example looks like.

Form module.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Combo1_AfterUpdate()
  3.  
  4.     Dim sqlRowSource As New SQLSelect
  5.  
  6.     With Me.Combo2
  7.         sqlRowSource.SelectString = .RowSource
  8.         sqlRowSource.Where = "Table2.keyID=" & Me.Combo1
  9.         .RowSource = sqlRowSource.SelectString
  10.         .Requery
  11.     End With
  12.  
  13.     Set sqlRowSource = Nothing
  14.  
  15. End Sub
  16.  
  17.  
To my mind this simple solution makes programming more flexible and provides a more readable and intuitive code.

I hope this will be helpful.

Best regards to all.

Fish
Jun 9 '07 #1
0 5592

Sign in to post your reply or Sign up for a free account.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
5
by: Michelle Stone | last post by:
Hi everybody I am writing a simple asp.net application using form authentication. I store the list of all users and their passwords in an SQL Server database table. My client recently told me...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
3
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
3
by: shapper | last post by:
Hello, On my web site I have a property, Visitor, which is available for Anonymous users: public class Visitor { public CultureInfo Culture { get; set; } public List<GuidPolls { get; set;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.