Since a combo box with nothing selected is Null, not an empty string (""),
try replacing the 4 comparisons that are like this:
combo <> ""
with:
IsNull(combo) = False
or
Len(combo & vbNullString) > 0
Also, the spaces around the single quotes were only there to make them
obvious. Your code should look like:
Private Sub Search_Click()
stDocName = "Project_Inventory"
Dim db As DAO.database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.openrecordset("Select * From Project_Inventory")
If Len(cmbPARID & vbNullString) > 0 And _
Len(cmbBO_Project_Name & vbNullString) > 0 Then
rs.findfirst "[ID]='" & cmbPARID & "' AND " & _
[BO_Project_Name]= '" & cmbBO_Project_Name & "'"
ElseIf Len(cmbPARID & vbNullString) > 0 Then
rs.findfirst "[ID]='" & cmbPARID & "'"
ElseIf Len(cmbBO_Project_Name & vbNullString) > 0 Then
rs.findfirst [BO_Project_Name] = '" & cmbBO_Project_Name & "'"
Else
MsgBox "Please select either a Project Name or a Project ID."
Exit Sub
End If
If Not rs.nomatch Then
' This is where you add the code to create the resultset
Else
MsgBox "No record was found matching this criteria!"
End If
DoCmd.OpenForm stDocName, , , strWhere
End Sub
I don't understand, though, why you're using FindFirst. Only have the
recordset return the relevant data:
Private Sub Search_Click()
stDocName = "Project_Inventory"
Dim db As DAO.database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strWhere As String
strSQL = "Select * From Project_Inventory"
If Len(cmbPARID & vbNullString) > 0 Then
strWhere = strWhere & "[ID]='" & cmbPARID & "' AND "
End If
If Len(cmbBO_Project_Name & vbNullString) > 0 Then
strWhere = strWhere & "[BO_Project_Name] = '" & _
cmbBO_Project_Name & "'"
End If
If Len(strWhere) = 0 Then
MsgBox "Please select either a Project Name or a Project ID."
Else
strWhere = Left$(strWhere, Len(strWhere) - 5)
Set db = CurrentDb()
Set rs = db.openrecordset(strSQL & " Where " & strWhere)
If rs.BOF = False and rs.EOF = False Then
MsgBox "No record was found matching this criteria!"
Else
' This is where you add the code to create the resultset
End If
DoCmd.OpenForm stDocName
End Sub
Of course, that will always open the Project_Inventory form, whether or not
anything was found. Sounds to me as though maybe all you need to do is open
the form with the Where clause above, and put logic in it to determine
whether there's anything to display on the form:
If Len(strWhere) = 0 Then
MsgBox "Please select either a Project Name or a Project ID."
Else
strWhere = Left$(strWhere, Len(strWhere) - 5)
DoCmd.OpenForm stDocName, , , strWhere
End If
--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)
Okay, thanks. The code is running but the form is still blank. This
is the code I have currently.
Private Sub Search_Click()
stDocName = "Project_Inventory"
Dim db As DAO.database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.openrecordset("Select * From Project_Inventory")
If cmbPARID <> "" And cmbBO_Project_Name <> "" Then
rs.findfirst "[ID]=' " & cmbPARID & " ' AND [BO_Project_Name]= ' "
& cmbBO_Project_Name & " ' "
ElseIf cmbPARID <> "" Then
rs.findfirst "[ID]=' " & cmbPARID & " ' "
ElseIf cmbBO_Project_Name <> "" Then
rs.findfirst [BO_Project_Name] = " & cmbBO_Project_Name & " ' "
Else
MsgBox "Please select either a Project Name or a Project ID."
If Not rs.nomatch Then
' This is where you add the code to create the resultset
Else
MsgBox "No record was found matching this criteria!"
End If
DoCmd.OpenForm stDocName, , , strWhere
End Sub- Hide quoted text -
- Show quoted text -