John and Duane,
Here's the bigger picture. The database holds Material Safety Data Sheet records. I'm trying to build a form that will search the records using criteria from some unbound text and combo boxes. The user puts whatever they want in the form then presses the search button which should open another form with the filtered results. They can click whatever record they want and it opens another form and displays the record and a copy of the MSDS that has been scanned and stored on a server. Here is the procedure under for the search button. I haven't got the SQL right yet.
Private Sub btnSearch_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("qrySearch")
Dim lMSDSID As Variant
Dim sProductName As String
Dim lCategory As Variant
Dim sManufacturer As String
Dim sChemtrac As String
Dim sPartNo As String
If IsNull(Me.txtMSDSID.Value) Then
lMSDSID = " "
lMSDSID = " = " & Me.txtMSDSID.Value & " "
If IsNull(Me.txtProductName.Value) Then
sProductName = " Like '*' "
sProductName = "= Like *'" & Me.txtProductName.Value & "'* "
If IsNull(Me.cboCategory.Value) Then
lCategory = " Like '*' "
lCategory = "= " & Me.cboCategory.Value & " "
If IsNull(Me.cboManufacturer.Value) Then
sManufacturer = " Like '*' "
sManufacturer = "= Like *'" & Me.cboManufacturer.Value & "'* "
If IsNull(Me.txtChemTrekNo.Value) Then
sChemtrac = " Like '*' "
sChemtrac = "= Like *'" & Me.txtChemTrekNo.Value & "'* "
If IsNull(Me.txtPartNo.Value) Then
sPartNo = " Like '*' "
sPartNo = "= Like *'" & Me.txtPartNo.Value & "'* "
strSQL = "SELECT qS.* " & _
"From qS " & _
"Where qS.MSDSID" & lMSDSID & _
"AND qS.ProductName" & sProductName & _
"AND qS.Category" & lCategory & _
"AND qS.MName" & sManufacturer & _
"AND qS.ChemTrekNo" & sChemtrac & _
"AND qS.PartNO" & sPartNo & ";"
qdf.SQL = strSQL
DoCmd.OpenForm "frmSearchResults", acFormDS
DoCmd.Close acForm, "frmSearch", acSaveNo
---In MS_Access_Professionals@yahoogroups.com, <duanehookom@...> wrote :
I agree with John. As zou start building your WHERE condition, you should ignore the columns/fields that aren't relevant.
However, you could use this type of syntax:
WHERE qS.MSDSID & '' Like '*'...
Sent: Wednesday, November 16, 2016 11:37 AM
Subject: [MS_AccessPros] Will Like '*' return records if the value is null?
I'm building a query on the fly through VBA and if appears that if the values for a field are null, the query won't return the record using criteria Like '*' for the field. Is that a correct? Here is the SQL of the query:
WHERE (((qS.MSDSID) Like '*') AND ((qS.ProductName) Like '*') AND ((qS.Category) Like '*') AND ((qS.MName) Like '*') AND ((qS.ChemTrekNo) Like '*') AND ((qS.PartNO) Like '*'));
In my table there are only 6 records and ChemTrekNo is null for all 6. Using Like '*' as criteria for ChemTrekNo no records are returned. What do I do if I want the records that contain nulls? Wrap it in Nz? What would the SQL look like?
Posted by: firstname.lastname@example.org
|Reply via web post||•||Reply to sender||•||Reply to group||•||Start a New Topic||•||Messages in this topic (5)|