473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Diary of a class module

In response to recent questions on when to use class modules in Access VBA,
here's a real-world scenario of mine.

I had some code that needed to build a Where clause for a Select statement
based on 0 or more criteria (0 criteria means no Where clause). My initial
stab at the code looked something like this...

<Code>
Private Sub Foo()
Dim strSql As String
Dim strWhere As String

If Not IsNull(Me!txtTypeId) then
strWhere = "[TypeID]=" & Me!txtTypeId
End If

If Not IsNull(Me!txtNameContains) then
If Len(strWhere) > 0 Then strWhere = strWhere & " AND "
strWhere = "[Name] Like *" & Me!txtTypeId & "*"
End If

If Not IsNull(Me!txtDescripContains) then
If Len(strWhere) > 0 Then strWhere = strWhere & " AND "
strWhere = "[Description] Like *" & Me!txtDescripContains & "*"
End If

If Len(strWhere) > 0 Then strWhere = " WHERE " & strWhere

strSql = "SELECT * FROM tblFoo" & _
strWhere & " " & _
"ORDER BY [Name];"
...
End Sub
</Code>

I see that in each case, I have to check to see if there's already something
in strWhere, so I know if I have to append an " AND ", so I factor that out
into a function.

<DemoCode>
Private Sub Foo()
Dim strSql As String
Dim strWhere As String

If Not IsNull(Me!txtTypeId) then
AppendDelimited _
strWhere, _
"[TypeID]=" & Me!txtTypeId, _
" AND "
End If

If Not IsNull(Me!txtNameContains) then
AppendDelimited _
strWhere, _
"[Name] Like ""*" & Me!txtNameContains & "*""", _
" AND "
End If

If Not IsNull(Me!txtDescripContains) then
AppendDelimited _
strWhere, _
"[Description] Like ""*" & Me!txtDescripContains & "*""", _
" AND "
End If

If Len(strWhere) > 0 Then strWhere = " WHERE " & strWhere

strSql = "SELECT * FROM tblFoo" & _
strWhere & " " & _
"ORDER BY [Name];"
...
End Sub

Private Sub AppendDelimited( _
ByRef strComboundExpr As String, _
strAppendExpr As String, _
strDelimiter As String _
)
If Len(strComboundExpr) > 0 Then
strComboundExpr = strComboundExpr & strDelimiter
End If
strComboundExpr = strComboundExpr & strAppendExpr
End Sub
</DemoCode>

This is a big improvement, but there are still 2 obvious bits of duplication
we'd like to be able to remove, passing the delimiter, and passing the
variable to append to. Furthermore, if more than one procedure uses this
pattern, there is duplication of the bit about checking to see whether the
clause has any content to see whether to prepend the Where keyword. Later, in
some other code, I also had the need to build clauses with suffices such as "
IN(1,2,3,...) "

Here's the Class module solution I came up with:

<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(strArgument As String)
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & strArgument & Suffix
End Sub
</clsClauseText>

And here's the example above, now using this new class module.

<DemoCode>
Private Sub Foo()
Dim strSql As String
Dim objWhereClause As New clsClauseText
' Always think before using "New", but it's fine here.

With objWhereClause
.Prefix = " WHERE "
.Delimiter = " AND "

If Not IsNull(Me!txtTypeId) then
.AddArgument "[TypeID]=" & Me!txtTypeId
End If
If Not IsNull(Me!txtNameContains) then
.AddArgument "[Name] Like ""*" & Me!txtNameContains & "*"""
End If
If Not IsNull(Me!txtDescripContains) then
.AddArgument _
"[Description] Like ""*" & Me!txtDescripContains & "*"""
End If
End With

strSql = "SELECT * FROM tblFoo" & _
objWhereClause.Value & " " & _
"ORDER BY [Name];"
...
End Sub
</DemoCode>

Next, if we take advantage of the fact that using + instead of & to
concatenate strings will result in Null if any of the arguments are Null, we
can take this yet another step.

<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(varArgument As Variant)
If IsNull(varArgument) Then Exit Sub
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & strArgument & Suffix
End Sub
</clsClauseText>

And here's the new usage example.

<DemoCode>
Private Sub Foo()
Dim strSql As String
Dim objWhereClause As New clsClauseText
' Always think before using "New", but it's fine here.

With objWhereClause
.Prefix = " WHERE "
.Delimiter = " AND "

.AddArgument "[TypeID]=" + Me!txtTypeId
.AddArgument "[Name] Like ""*" + Me!txtNameContains & "*"""
.AddArgument _
"[Description] Like ""*" + Me!txtDescripContains & "*"""
End With

strSql = "SELECT * FROM tblFoo" & _
objWhereClause.Value & " " & _
"ORDER BY [Name];"
...
End Sub
</DemoCode>

Now, here's a question. Which of these would you rather write? A better
question, then is which one of these would you rather read!?
Nov 13 '05 #1
4 1486
Correction to last class example:

On Thu, 27 May 2004 09:59:35 GMT, Steve Jorgensen <no****@nospam.nospam>
wrote:

....
<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(varArgument As Variant)
If IsNull(varArgument) Then Exit Sub
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & varArgument & Suffix
End Sub
</clsClauseText>

Nov 13 '05 #2
Dear Steve

That indeed is a pretty sharp solution. Thank you for that.

It seems to me that the Class Modules are not required, but very powerful in
simplifying things.
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:nd********************************@4ax.com...
Correction to last class example:

On Thu, 27 May 2004 09:59:35 GMT, Steve Jorgensen <no****@nospam.nospam>
wrote:

...
<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(varArgument As Variant)
If IsNull(varArgument) Then Exit Sub
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & varArgument & Suffix
End Sub
</clsClauseText>

---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004
Nov 13 '05 #3
Any language can perform any task, but some code of the types of code
organization to remove duplication, keep code and its data together, etc., are
not possible without class modules. Many other desirable organizational tasks
are not possible without the full compliment of OOP features that VB/VBA does
not have, but it's good to use all the tools we do have where they do provide
a clear benefit.

On Thu, 27 May 2004 22:18:18 +1200, "WindAndWaves" <ac****@ngaru.com> wrote:
Dear Steve

That indeed is a pretty sharp solution. Thank you for that.

It seems to me that the Class Modules are not required, but very powerful in
simplifying things.
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:nd********************************@4ax.com.. .
Correction to last class example:

On Thu, 27 May 2004 09:59:35 GMT, Steve Jorgensen <no****@nospam.nospam>
wrote:

...
<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(varArgument As Variant)
If IsNull(varArgument) Then Exit Sub
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & varArgument & Suffix
End Sub
</clsClauseText>

---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004


Nov 13 '05 #4
Yeah, that is funny actually.

A lot of people criticise me for using MS Access. However, as you say, it
does not really matter what you use, it is all about what you do with it.

I use my own way to keep things simple and I am sure that everyone has their
own tricks and theories.

Thanks again.

Nicolaas

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:h4********************************@4ax.com...
Any language can perform any task, but some code of the types of code
organization to remove duplication, keep code and its data together, etc., are not possible without class modules. Many other desirable organizational tasks are not possible without the full compliment of OOP features that VB/VBA does not have, but it's good to use all the tools we do have where they do provide a clear benefit.

On Thu, 27 May 2004 22:18:18 +1200, "WindAndWaves" <ac****@ngaru.com> wrote:
Dear Steve

That indeed is a pretty sharp solution. Thank you for that.

It seems to me that the Class Modules are not required, but very powerful insimplifying things.
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:nd********************************@4ax.com.. .
Correction to last class example:

On Thu, 27 May 2004 09:59:35 GMT, Steve Jorgensen <no****@nospam.nospam> wrote:

...
<clsClauseText>
Option Compare Database
Option Explicit

' This class assists in the construction of clause strings
' such as the clauses in SQL Statements.

Public Prefix As String
Public Delimiter As String
Public Suffix As String
Public Value As String

' -- AddArgument --
' Adds a string to the list of arguments included in the clause being
' built.
'
' strArgument : The new argument string to add
'
Public Sub AddArgument(varArgument As Variant)
If IsNull(varArgument) Then Exit Sub
If Len(Value) > 0 Then
Value = _
Left$(Value, Len(Value) - Len(Suffix)) & _
Delimiter
Else
Value = Prefix
End If
Value = Value & varArgument & Suffix
End Sub
</clsClauseText>

---
Please immediately let us know (by phone or return email) if (a) this emailcontains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 22/05/2004

---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
Hi all, I am writing a database using SQL as the back end and Access as the front end. My project is to create a rota for when people have to work. The problem I have is setting up the tables...
2
by: groups | last post by:
If you haven't read the book, nor watched the movie, "Harry Potter and the Chamber of Secrets". Harry Potter finds a diary with nothing written in it. When he starts writing in it, the diary...
0
by: jonathan.beckett | last post by:
Hi all, Just announcing the latest version of my blog / diary / journal script. You can get it (and read all about it) at; http://www.pluggedout.com/index.php?pk=dev_blog It's edging closer...
1
by: OHM \( One Handed Man \) | last post by:
Hi Guys, Im working on something which requires a method of managing entries for resources in ASP.NET and I need to find a good solution for it. In essence, I want my user to be able to book...
0
by: OHM \( One Handed Man \) | last post by:
Hi Guys, Im working on something which requires a method of managing entries for resources in ASP.NET and I need to find a good solution for it. In essence, I want my user to be able to book...
0
by: jonathan.beckett | last post by:
Version 1.9.9f of the PluggedOut Blog script is now available for download. PluggeOut Blog is a free, open source blog/diary/journal script, written by a professional software developer to give...
1
by: SandipAdhikary | last post by:
hello sir, I have faced a prob.how can i make a diary control using .net with c#. exactly microsoft diary control. explanation=: there is one calender control ,u have to click a cell of...
8
by: shrimpeh | last post by:
Hi im currently messing around with creating a diary/booking system for a leisure centre. And im stuggling with how I can display all the data. The booking is taking the activity the customer is...
1
by: TC | last post by:
Hi, I have an app that is going to require recurring diary entries, it's a relatively simple app but I have no idea how to go about the recurring entry side of things. The functionality needs to...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.