The problem of no records is taken care of in a procedure which builds tblSrch.
I don't understand your comment about the rs.Close statement. I opened (if that's the right word) it in the statement: ' Set rs = Me.RecordsetClone"
The procedure to build tblSrch follows.
Private Sub cmdSearch_Click()
On Error GoTo Err_cmdSearch_Click
Dim rs As DAO.Recordset
Dim stSQL As String
Dim varData As Variant
Dim varInput As Variant
Dim strWhere As String
Dim intSpaces As Integer
Dim intWords As Integer
Dim raWords() As String
Dim I As Integer
On Error GoTo Err_cmdSearch_Click
' Delete tblSrch. If tblSrch does not exist, an error will be created
' and taken care of by the error handler
CurrentDb.TableDefs.Delete "tblSrch"
' Make sure tblSrch is available to the project
Application.RefreshDatabaseWindow
' Copy what's in the text box named txtInput to varInput
varInput = Nz(Me.txtInput, Space(0))
' Case of no input
If varInput = Space(0) Then
MsgBox "You must input something!", vbOKOnly
GoTo Exit_cmdSearch_Click
End If
' The TrimAll function will remove leading and trailing spaces from varInput
' then check the spaces between words and delete any extra spaces.
varInput = TrimAll(ByVal varInput)
' How many spaces are left?
intSpaces = CountIn(varInput)
' There will be one more word than spaces
'Redimension the array to the number of words. Note:
zero-based
ReDim raWords(0 To intSpaces)
' Put the words in an array (one word per array element).
' First handle the condition of only one word
If intSpaces = 0 Then
raWords(0) = varInput
Else
raWords = Split(varInput, " ")
End If
' Build the Where clause for the SQL
' First clear the variable
strWhere = Space(0)
' Make an element in the Where clause for each word.
' Note: chr(34) is a quote and chr(42) is an asterisk.
For I = 0 To intSpaces
strWhere = strWhere & "(tblNutrition.DESC) Like " & _
Chr(34) & Chr(42) & raWords(I) & _
Chr(42) & Chr(34) & " AND "
Next I
' Take the " And " off the end of strWhere
strWhere = Left(strWhere, Len(strWhere) - 5)
' Add the final parenthesis to strWhere
strWhere = strWhere & "))"
' Preface strWhere
strWhere = "WHERE ((" & strWhere
' Records will be selected if Description field contains all of
' the words in the food description segment input by the user.
stSQL = "SELECT tblNutrition.NutrID, tblNutrition.DESC, tblNutrition.SRVG " & _
"INTO tblSrch FROM tblNutrition " & _
strWhere & _
" ORDER BY tblNutrition.NutrID;"
' Create tblSrch from stSQL
CurrentDb.Execute stSQL
' Make sure tblSrch is available to the project
Application.RefreshDatabaseWindow
' Open a recordset of tblSrch
Set rs = CurrentDb.OpenRecordset("tblSrch")
' See if there are records in the recordset (tblSrch)
If rs.BOF And rs.EOF Then
MsgBox "No records found.", vbOKOnly
rs.Close
Set rs = Nothing
Me!txtInput = Space(0)
GoTo Exit_cmdSearch_Click
End If
' Records were found so make cboSrch visible
Me.Box62.Visible = True
Me.cboSearch_Label.Visible = True
Me.cboSearch.Visible = True
' Cleanup
rs.Close
Set rs = Nothing
Me.txtInput = Space(0)
Me.txtInput.SetFocus
Exit_cmdSearch_Click:
Exit Sub
Err_cmdSearch_Click:
If Err.Number = 3265 Or Err.Number = 94 Then
Resume Next
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdSearch_Click
End If
End Sub