473,289 Members | 1,896 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,289 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 5586

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;...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.