I see. Although strWhere was initialized it was never set. I know that the
query works in the query designer. I've run it several times that way. The
WHERE clause in the query is this: WHERE
(((T2.Distance)<=CDbl([DISTANCE_MILES])))
Side Note: T2 is a query that has a paramater of [ZipCode]
However from my form I am trying to set two paramaters (the other one is
set
in T2) as the WHERE statement. Maybe I am going about this wrong. Can I
just
put: "WHERE paramZip, paramMiles" as my WHERE without using strWhere. But
then in the lower portion of my code what would I use to test the results
with IF statements?
Private Sub Search_Click()
Dim strWhere As String
Dim rs As ADODB.Recordset
'Set rs = SelectRadius.SelectRadius(txtZip.Value,
CDbl(txtDistance.Value))
Dim qry As New ADODB.Command
Dim paramZip As ADODB.Parameter
Dim paramMiles As ADODB.Parameter
Set paramZip = qry.CreateParameter("ZIP_CODE", adVarChar, adParamInput,
255, txtZip.Value)
Set paramMiles = qry.CreateParameter("DISTANCE_MILES", adDouble,
adParamInput, 8, CDbl(txtDistance.Value))
qry.Parameters.Append paramZip
qry.Parameters.Append paramMiles
qry.CommandText = "ChurchDistance"
qry.ActiveConnection = CurrentProject.Connection
Set rs = qry.Execute()
rs.MoveNext
'Begin my added code
' Check to see that we built a filter
If IsNull(strWhere) Then
MsgBox "You must enter search criteria.", vbInformation,
gstrAppTitle
Exit Sub
End If
' Open a recordset to see if any rows returned with this filter
Set rs = DBEngine(0)(0).OpenRecordset("SELECT T2.Distance, T1.* FROM
(Church AS T1 INNER JOIN qryChurchZip1 ON T1.ChurchID =
qryChurchZip1.ChurchID) INNER JOIN DistanceQuery AS T2 ON
qryChurchZip1.Zip5
= T2.ZIPCode WHERE " & strWhere)
' See if found none
If rs.RecordCount = 0 Then
MsgBox "No Churches meet your criteria.", vbInformation,
gstrAppTitle
' Clean up recordset
rs.Close
Set rs = Nothing
Exit Sub
End If
' Hide me to fix later focus problems
Me.Visible = False
' Move to last to find out how many
rs.MoveLast
' If 5 or less or frmContacts already open,
If (rs.RecordCount < 6) Or
CurrentProject.AllForms("ChurchInformation").IsLoaded Then
'Or IsFormLoaded("Contacts")
' Open Contacts filtered
' Note: if form already open, this just applies the filter
DoCmd.OpenForm "ChurchInformation", WhereCondition:=strWhere
' Make sure focus is on church information form
Forms!ChurchInformation.SetFocus
Else
' Ask if they want to see a summary list first
If vbYes = MsgBox("Your search found " & rs.RecordCount & "
churches. " & _
"Do you want to see a summary list first?", _
vbQuestion + vbYesNo, gstrAppTitle) Then
' Show the summary
DoCmd.OpenForm "ChurchSummary", WhereCondition:=strWhere
' Make sure focus is on contact summary
Forms!ChurchSummary.SetFocus
Else
' Show the full contacts info filtered
DoCmd.OpenForm "ChurchInformation", WhereCondition:=strWhere
' Make sure focus is on contacts
Forms!Contacts.SetFocus
End If
End If
' Done
'DoCmd.Close acForm, Me.Name
' Clean up recordset
rs.Close
Set rs = Nothing
End Sub
Minton M said:
I keep getting errors on the WHERE part of my query. I'm sure it's
something
simple.
I don't know if you will need the whole code to see what the problem
is. It
is lengthy, so I will start with just where the problem is.
Set rs = DBEngine(0)(0).OpenRecordset("SELECT T2.Distance, T1.*
FROM
(Church AS T1 INNER JOIN qryChurchZip1 ON T1.ChurchID =
qryChurchZip1.ChurchID) INNER JOIN DistanceQuery AS T2 ON
qryChurchZip1.Zip5
= T2.ZIPCode WHERE " & strWhere)
' See if found none
If rs.RecordCount = 0 Then
MsgBox "No Churches meet your criteria.", vbInformation,
gstrAppTitle
' Clean up recordset
rs.Close
Set rs = Nothing
Exit Sub
End If
Thanks for the help.
Can you provide the WHERE clause (the code that produces strWhere)? I
would recommend breaking up the code - namely, state the query in a
string first so you can see what the entirety of the query will be and
then running that in a query designer window. This will show any
errors, often with more meaningful help that just throwing the query
at the DB engine.
By the way, I wouldn't use rs.RecordCount in the way you are trying to
use it - use rs.EOF instead. RecordCount is only populated when you
move to the end of the recordset whereas EOF always returns a reliable
true/false indicator.
-- James