Coding a WHERE clause using form options: what's best practice?

M

Max Moor

Hi All,

This is probably a real newbie thing, but what is the best practice
coding for creating the WHERE clause for an SQL statement, in VBA, using
options on a form. In particular, I[m curious about when none, any
combination, or all of the options may be selected. For the sake of
argument, let's say that if none are selected, all records should be
returned, so no WHERE is needed.

Here's what I currently do. I form the initial SELECT and FROM clauses
into a string. At the end of the string, I tack on "WHERE 1=1". Then I go
through the options, concatinating phrases like " AND yada yada yada" for
each option selected. Once done with the options, I add the ORDER, if used,
and close the string.

The only real down side I can see is that I always have a WHERE clause
being evaluated, even if there are no options. Especially if this is the
most common case, having an unnecessary WHERE hurts efficiency. This isn't
likely a big deal unless it's a huge installation or something, and that not
an issue for me.

So, what do the folks "in the know" do? What's the best way to handle
this sort of coding?

Thanks All,
Max
 
J

Jeanette Cunningham

Hi Max,
here is my preferred approach, copied from Allen Browne's Search form sample
db
http://www.allenbrowne.com/ser-62.html


Private Sub cmdFilter_Click()
'Purpose: Build up the criteria string form the non-blank search
boxes, and apply to the form's Filter.
'Notes: 1. We tack " AND " on the end of each condition so you can
easily add more search boxes; _
we remove the trailing " AND " at the end.
' 2. The date range works like this: _
Both dates = only dates between (both
inclusive. _
Start date only = all dates from this one onwards; _
End date only = all dates up to (and including
this one).
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string
to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates
in a JET query string.

'***********************************************************************
'Look at each search box, and build up the criteria string from the
non-blank ones.
'***********************************************************************
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.txtFilterCity) Then
strWhere = strWhere & "([City] = """ & Me.txtFilterCity & """) AND "
End If

'Another text field example. Use Like to find anywhere in the field.
If Not IsNull(Me.txtFilterMainName) Then
strWhere = strWhere & "([MainName] Like ""*" & Me.txtFilterMainName
& "*"") AND "
End If

'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterLevel) Then
strWhere = strWhere & "([LevelID] = " & Me.cboFilterLevel & ") AND "
End If

'Yes/No field and combo example. If combo is blank or contains "ALL", we
do nothing.
If Me.cboFilterIsCorporate = -1 Then
strWhere = strWhere & "([IsCorporate] = True) AND "
ElseIf Me.cboFilterIsCorporate = 0 Then
strWhere = strWhere & "([IsCorporate] = False) AND "
End If

'Date field example. Use the format string to add the # delimiters and
get the right international format.
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([EnteredOn] >= " & Format(Me.txtStartDate,
conJetDate) & ") AND "
End If

'Another date field example. Use "less than the next day" since this
field has times as well as dates.
If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
strWhere = strWhere & "([EnteredOn] < " & Format(Me.txtEndDate + 1,
conJetDate) & ") AND "
End If

'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to
remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the "
AND " at the end.
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to
Immediate Window (Ctrl+G).
'Debug.Print strWhere

'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub



Max Moor said:
Hi All,

This is probably a real newbie thing, but what is the best practice
coding for creating the WHERE clause for an SQL statement, in VBA, using
options on a form. In particular, I[m curious about when none, any
combination, or all of the options may be selected. For the sake of
argument, let's say that if none are selected, all records should be
returned, so no WHERE is needed.

Here's what I currently do. I form the initial SELECT and FROM clauses
into a string. At the end of the string, I tack on "WHERE 1=1". Then I
go
through the options, concatinating phrases like " AND yada yada yada" for
each option selected. Once done with the options, I add the ORDER, if
used,
and close the string.

The only real down side I can see is that I always have a WHERE clause
being evaluated, even if there are no options. Especially if this is the
most common case, having an unnecessary WHERE hurts efficiency. This
isn't
likely a big deal unless it's a huge installation or something, and that
not
an issue for me.

So, what do the folks "in the know" do? What's the best way to handle
this sort of coding?

Thanks All,
Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top