By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,651 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,651 IT Pros & Developers. It's quick & easy.

How to use NOT Operator in Queries

P: 10
Dear All I am building a search form in access. I want my users to customize their search. So i have put AND OR and NOT operators in a combo box. AND and OR operators are working fine. But by using NOT operator returns a syntax error. What to do?
Query is as under:
Expand|Select|Wrap|Line Numbers
  1. select * from dtlsuggestions where (([Title] & ': ' & [Subtitle]) = 'Physics')
  2.  AND (([AUTHOR] & ', ' & [SUBAUTHOR]) = 'David')
  3.  OR (Subject = 'Quantum')
  4.  AND (Publisher = 'IRVIN')
  5.  NOT (([DDC_NO] & ' ' & [Auth_MARK]) = '658.2')
  6.  
  7.  
  8.  
Sep 12 '07 #1
Share this Question
Share on Google+
8 Replies


FishVal
Expert 2.5K+
P: 2,653
Dear All I am building a search form in access. I want my users to customize their search. So i have put AND OR and NOT operators in a combo box. AND and OR operators are working fine. But by using NOT operator returns a syntax error. What to do?
Query is as under:
Expand|Select|Wrap|Line Numbers
  1. select * from dtlsuggestions where (([Title] & ': ' & [Subtitle]) = 'Physics')
  2.  AND (([AUTHOR] & ', ' & [SUBAUTHOR]) = 'David')
  3.  OR (Subject = 'Quantum')
  4.  AND (Publisher = 'IRVIN')
  5.  NOT (([DDC_NO] & ' ' & [Auth_MARK]) = '658.2')
  6.  
  7.  
  8.  
NOT is unary logic operator, not binary like OR, AND.
it uses one operand only and reverses it's boolean value, e.g.
NOT False = True
NOT True = False

What are you expecting from expression like
Expr1 NOT Expr2 ?
Sep 12 '07 #2

P: 10
Dear FishVal
Thanks for response

If there are two filelds in tblabc, 'title' and 'author'. I want all titles having 'physics' But NOT author='david'

It will return 'Physics by vikram', 'physics by khan' but it will not return 'physics by david'

Is it possible?

Ata
NOT is unary logic operator, not binary like OR, AND.
it uses one operand only and reverses it's boolean value, e.g.
NOT False = True
NOT True = False

What are you expecting from expression like
Expr1 NOT Expr2 ?
Sep 12 '07 #3

P: 65
Hi,

I think this might be a solution to your problem:

SELECT * FROM dtlsuggestions WHERE title = "Physics" AND NOT author = "david";

This is the solution I think. If the title is longer than just physics you better use "like" to select the title.

Greetz
Twanne

Keeping the keys clean.
Sep 12 '07 #4

FishVal
Expert 2.5K+
P: 2,653
Or simply

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM dtlsuggestions WHERE title = "Physics" AND author <> "david";
  2.  
"NOT" is generally useful for those operators which don't have their logic opposites or for functions returning boolean values.

e.g.

Not In (SELECT ...)
Not objName Is Nothing
Not IsNull(varName)
Sep 12 '07 #5

P: 68
I use variations on an 'Add to Where' function I found on the web many years back, which allows me to search strings. Here are some samples;

Function AddToWhere(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " and "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & " Like " & Chr(39) & FieldValue & Chr(42) & Chr(39))

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

Function AddaValue(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " AND "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & "=" & FieldValue)

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

Function AddaStatement(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " AND "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & FieldValue)

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

These variations determine whether you include 'AND' or 'LIKE' or '=' in the sql statement, and you could create a further function to include 'NOT' or 'NOT LIKE'.

Here's a sample of using these functions, an expert coder would no doubt cringe at some of this, but it works :-)

Private Sub TriageFilter()
On Error GoTo Proc_Err

Dim MySQL As String
Dim MyDB As Database
Dim qdfNew As QueryDef
Dim MyCriteria As String
Dim MyServiceCriteria As String
Dim MyFilterCriteria As String
Dim MyRecordSource As String
Dim MyServiceArgCount As Integer
Dim MyFilterArgCount As Integer
Dim Tmp As Variant

Set MyDB = CurrentDb

MyServiceArgCount = 0
MyFilterArgCount = 0

MySQL = "SELECT tblCases.CaseID, tblEvents.ActionID, tblEvents.ServiceID, tblEvents.StatusID " & _
"FROM tblCases LEFT JOIN tblEvents ON tblCases.CaseID = tblEvents.CaseID " & _
"WHERE (((tblEvents.ActionID)=35 Or (tblEvents.ActionID)=44 Or (tblEvents.ActionID)=24)) AND "

MyCriteria = ""


AddaStatement Me.cboFilter.Column(0), "[StatusID]", MyFilterCriteria, MyFilterArgCount
AddaValue Me.cboService.Column(0), "[ServiceID]", MyServiceCriteria, MyServiceArgCount

If IsNull(Me.cboService) Then
MyCriteria = MyFilterCriteria
ElseIf IsNull(Me.cboFilter) Then
MyCriteria = MyServiceCriteria
Else
MyCriteria = MyFilterCriteria & " AND " & MyServiceCriteria
End If

If MyCriteria = "" Then
MyCriteria = "True"
End If
MyRecordSource = MySQL & MyCriteria
' I sometimes put in a msgbox and exit sub here when developing the
' statement, so that I can see the sql and check it's what I wanted, e.g.
'msgbox MyRecordSource
'exit sub
If acbDoesObjExist("qryTriageBaseline", acQuery) Then
DoCmd.DeleteObject acQuery, "qryTriageBaseline"
End If

With MyDB
Set qdfNew = .CreateQueryDef("qryTriageBaseline", MyRecordSource)
End With

Me.RecordSource = "select * from qryTriage;"

If Form.RecordsetClone.RecordCount = 0 Then
Me.txtMessage = "THERE ARE NO CASES MATCHING YOUR SEARCH CRITERIA."
Me.txtRecordNo = "No Cases"
'Form.Requery
Else
Me.txtMessage = ""
End If
Form_Current
Form.Refresh

Set MyDB = Nothing
Proc_Exit:
Exit Sub

Proc_Err:
MsgBox "Oops, that must be error number " & Err.Number & ": " & Err.Description
Resume Proc_Exit

End Sub

tip: being a bit dense at times I sometimes can't visualise the resulting sql from the code, so I often put in a msgbox
Sep 12 '07 #6

P: 10
can you send me this application working?

Ata


I use variations on an 'Add to Where' function I found on the web many years back, which allows me to search strings. Here are some samples;

Function AddToWhere(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " and "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & " Like " & Chr(39) & FieldValue & Chr(42) & Chr(39))

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

Function AddaValue(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " AND "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & "=" & FieldValue)

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

Function AddaStatement(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)

' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " AND "
End If

' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & FieldValue)

' Increase argument count.
ArgCount = ArgCount + 1
End If

End Function

These variations determine whether you include 'AND' or 'LIKE' or '=' in the sql statement, and you could create a further function to include 'NOT' or 'NOT LIKE'.

Here's a sample of using these functions, an expert coder would no doubt cringe at some of this, but it works :-)

Private Sub TriageFilter()
On Error GoTo Proc_Err

Dim MySQL As String
Dim MyDB As Database
Dim qdfNew As QueryDef
Dim MyCriteria As String
Dim MyServiceCriteria As String
Dim MyFilterCriteria As String
Dim MyRecordSource As String
Dim MyServiceArgCount As Integer
Dim MyFilterArgCount As Integer
Dim Tmp As Variant

Set MyDB = CurrentDb

MyServiceArgCount = 0
MyFilterArgCount = 0

MySQL = "SELECT tblCases.CaseID, tblEvents.ActionID, tblEvents.ServiceID, tblEvents.StatusID " & _
"FROM tblCases LEFT JOIN tblEvents ON tblCases.CaseID = tblEvents.CaseID " & _
"WHERE (((tblEvents.ActionID)=35 Or (tblEvents.ActionID)=44 Or (tblEvents.ActionID)=24)) AND "

MyCriteria = ""


AddaStatement Me.cboFilter.Column(0), "[StatusID]", MyFilterCriteria, MyFilterArgCount
AddaValue Me.cboService.Column(0), "[ServiceID]", MyServiceCriteria, MyServiceArgCount

If IsNull(Me.cboService) Then
MyCriteria = MyFilterCriteria
ElseIf IsNull(Me.cboFilter) Then
MyCriteria = MyServiceCriteria
Else
MyCriteria = MyFilterCriteria & " AND " & MyServiceCriteria
End If

If MyCriteria = "" Then
MyCriteria = "True"
End If
MyRecordSource = MySQL & MyCriteria
' I sometimes put in a msgbox and exit sub here when developing the
' statement, so that I can see the sql and check it's what I wanted, e.g.
'msgbox MyRecordSource
'exit sub
If acbDoesObjExist("qryTriageBaseline", acQuery) Then
DoCmd.DeleteObject acQuery, "qryTriageBaseline"
End If

With MyDB
Set qdfNew = .CreateQueryDef("qryTriageBaseline", MyRecordSource)
End With

Me.RecordSource = "select * from qryTriage;"

If Form.RecordsetClone.RecordCount = 0 Then
Me.txtMessage = "THERE ARE NO CASES MATCHING YOUR SEARCH CRITERIA."
Me.txtRecordNo = "No Cases"
'Form.Requery
Else
Me.txtMessage = ""
End If
Form_Current
Form.Refresh

Set MyDB = Nothing
Proc_Exit:
Exit Sub

Proc_Err:
MsgBox "Oops, that must be error number " & Err.Number & ": " & Err.Description
Resume Proc_Exit

End Sub

tip: being a bit dense at times I sometimes can't visualise the resulting sql from the code, so I often put in a msgbox
Sep 12 '07 #7

P: 10
Dear Twanne and FishVal

Thanks. Now it is working

Ata


Or simply

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM dtlsuggestions WHERE title = "Physics" AND author <> "david";
  2.  
"NOT" is generally useful for those operators which don't have their logic opposites or for functions returning boolean values.

e.g.

Not In (SELECT ...)
Not objName Is Nothing
Not IsNull(varName)
Sep 12 '07 #8

P: 68
can you send me this application working?

Ata
I'm sorry, but not at the moment - I'm still developing the one this code came from, but happy to elaborate on the functions if it would help. The methods used in other posts look interesting, I may try these as an alternative to mine.
Sep 12 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.