T
Tony Scullion
Hello Group,
I am using a modified search form originally designed by
Dev Ashish & Terry Kreft. I have come across a problem
with the function 'Application.BuildCriteria', which I am
unable to solve and I was looking for expert advice.
The search form itself works well but fails under
conditions when searches are performed using reserved SQL
statements such as 'and' and '&'. My staff use the search
form daily and is vital in returning records of companies
such as - Seashore & Harbour Fisheries Ltd. Quite a lot
of my staff are not very knowledgeable with SQL etc and
much prefer to use the front end search form, which does
most of the work.
I have already done a lot of research in finding a
solution and I know it lies in adding delimiters around
entered criteria but I am unable to code and work it all
out. Would love some help with this as I can see myself
working for hours/days on this one and will probably get
nowhere.
Here is the code that the search form uses to build the
SQL statement, maybe of use.
Regards
Tony
Sub sBuildSQL()
' Take what's currently selected on the form
' and create a dynamic SQL statement for the
' lstResults listbox.
'
On Error GoTo ErrHandler 'Pick up and handle errors
Dim strSQL As String
Dim strWhere As String
Dim strJoinType As String
Dim strTable As String
Dim i As Integer
Dim j As Integer
Dim db As Database
Dim rs As Recordset
Dim tdf As TableDef
Dim qdf As QueryDef
Dim rsQdf As Recordset
Dim fld As Field
Const conMAXCONTROLS = 5
Set db = DBEngine(0)(0)
strSQL = "Select * "
'Right now we have five combo/textbox sets
'so set up the master loop to go through these controls
For i = 0 To conMAXCONTROLS - 1
strJoinType = vbNullString
'there might be some unused sets, so don't bother
'going through the disabled controls
If Me("cbxFld" & i).Enabled Then
'The Or/And set starts with the second
combo/textbox set
'so if there's only one criteria specified, don't
need to
'concatenate additional stuff.
If i > 0 Then
If Me("opgClauseType" & i) = 1 Then
strJoinType = " OR "
Else
strJoinType = " AND "
End If
End If
'Work out field type
'Get the a reference to the field in the
table/Query as
'we'll need it for BuildCriteria later on
If cstrSearchQuery = "Table" Then
Set tdf = db.TableDefs(cstrSearchQuery)
Set fld = tdf.Fields(Me("cbxFld" & i))
Else
Set rsQdf = db.OpenRecordset( _
"Select * from [" & cstrSearchQuery & "]
Where 1=2", dbOpenSnapshot)
Set fld = rsQdf.Fields(Me("cbxFld" & i))
End If
'Only build a criteria if something's typed in
the textbox
'otherwise assume all records
If Not IsNull(Me("txtVal" & i)) Then
strWhere = strWhere & strJoinType &
Application.BuildCriteria( _
"[" & Me("cbxFld" & i) & "]", _
fld.Type, Me("cboOperator" & i) & Me("txtVal" & i) & "")
Else
strWhere = strWhere & strJoinType & "[" & Me
("cbxFld" & i) & "] like '*'"
End If
End If
Next
Me.txtSQLWhere = strWhere
'The final all important SQL statement
strSQL = strSQL & " from [" & cstrSearchQuery & "]
Where " & strWhere
Me.txtSQL = strSQL
ExitHere:
Set rsQdf = Nothing
Set rs = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Sub
ErrHandler:
Select Case Err.Number
'we're trying to open a parameter query
Case 3061:
MsgBox "The query you've selected " _
& " is a Parameter Query." & vbCrLf &
Err.Description, vbExclamation + vbOKOnly, _
"Missing parameters"
Case Else:
'Either invalid SQL or some other error
End Select
Resume ExitHere
End Sub
I am using a modified search form originally designed by
Dev Ashish & Terry Kreft. I have come across a problem
with the function 'Application.BuildCriteria', which I am
unable to solve and I was looking for expert advice.
The search form itself works well but fails under
conditions when searches are performed using reserved SQL
statements such as 'and' and '&'. My staff use the search
form daily and is vital in returning records of companies
such as - Seashore & Harbour Fisheries Ltd. Quite a lot
of my staff are not very knowledgeable with SQL etc and
much prefer to use the front end search form, which does
most of the work.
I have already done a lot of research in finding a
solution and I know it lies in adding delimiters around
entered criteria but I am unable to code and work it all
out. Would love some help with this as I can see myself
working for hours/days on this one and will probably get
nowhere.
Here is the code that the search form uses to build the
SQL statement, maybe of use.
Regards
Tony
Sub sBuildSQL()
' Take what's currently selected on the form
' and create a dynamic SQL statement for the
' lstResults listbox.
'
On Error GoTo ErrHandler 'Pick up and handle errors
Dim strSQL As String
Dim strWhere As String
Dim strJoinType As String
Dim strTable As String
Dim i As Integer
Dim j As Integer
Dim db As Database
Dim rs As Recordset
Dim tdf As TableDef
Dim qdf As QueryDef
Dim rsQdf As Recordset
Dim fld As Field
Const conMAXCONTROLS = 5
Set db = DBEngine(0)(0)
strSQL = "Select * "
'Right now we have five combo/textbox sets
'so set up the master loop to go through these controls
For i = 0 To conMAXCONTROLS - 1
strJoinType = vbNullString
'there might be some unused sets, so don't bother
'going through the disabled controls
If Me("cbxFld" & i).Enabled Then
'The Or/And set starts with the second
combo/textbox set
'so if there's only one criteria specified, don't
need to
'concatenate additional stuff.
If i > 0 Then
If Me("opgClauseType" & i) = 1 Then
strJoinType = " OR "
Else
strJoinType = " AND "
End If
End If
'Work out field type
'Get the a reference to the field in the
table/Query as
'we'll need it for BuildCriteria later on
If cstrSearchQuery = "Table" Then
Set tdf = db.TableDefs(cstrSearchQuery)
Set fld = tdf.Fields(Me("cbxFld" & i))
Else
Set rsQdf = db.OpenRecordset( _
"Select * from [" & cstrSearchQuery & "]
Where 1=2", dbOpenSnapshot)
Set fld = rsQdf.Fields(Me("cbxFld" & i))
End If
'Only build a criteria if something's typed in
the textbox
'otherwise assume all records
If Not IsNull(Me("txtVal" & i)) Then
strWhere = strWhere & strJoinType &
Application.BuildCriteria( _
"[" & Me("cbxFld" & i) & "]", _
fld.Type, Me("cboOperator" & i) & Me("txtVal" & i) & "")
Else
strWhere = strWhere & strJoinType & "[" & Me
("cbxFld" & i) & "] like '*'"
End If
End If
Next
Me.txtSQLWhere = strWhere
'The final all important SQL statement
strSQL = strSQL & " from [" & cstrSearchQuery & "]
Where " & strWhere
Me.txtSQL = strSQL
ExitHere:
Set rsQdf = Nothing
Set rs = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Sub
ErrHandler:
Select Case Err.Number
'we're trying to open a parameter query
Case 3061:
MsgBox "The query you've selected " _
& " is a Parameter Query." & vbCrLf &
Err.Description, vbExclamation + vbOKOnly, _
"Missing parameters"
Case Else:
'Either invalid SQL or some other error
End Select
Resume ExitHere
End Sub