Not like; <>; != in query adodb excel

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
97
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi,

I am trying to use query like this in few different ways and it doesn't load anything.

Code:
qry = "SELECT * From db_actionlist WHERE [Status] != 'Canceled'"
- Error

Code:
qry = "SELECT * From db_actionlist WHERE [Status] <> 'Canceled'"
- Nothing

Code:
qry = "SELECT * From db_actionlist WHERE NOT ([Status] =  'Canceled'")
- Nothing

What could be issue or how I could set up statement not equal to some string?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Can you post a representative copy of your data (select/copy and paste into a post)? Or if you happen to be using lookup fields in your table, then the value Canceled is not "Canceled" - it is something else, such as a number like 2. The second example should work. The syntax of the 1st is incorrect, the other probably can't use NOT in that way, but I'm a bit fuzzy on that one (getting old I guess).
 
Upvote 0
Here are examples:
1686743869744.png


1686743881663.png


Here is example of the code
Code:
Private Sub Groupbydept() 'Group by user dept

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Action List")

'If sh.Range("A10").Value <> "" Then
'sh.Rows("10:" & Rows.Count).ClearContents
'Else
'sh.Rows("10:" & Rows.Count + 1).ClearContents
'End If

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim qry As String, i As Integer
Dim n As Long
  
qry = "SELECT * From db_actionlist WHERE [Status] <> 'Canceled'"
  
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "XXXXXXXXX" & ";" & "Jet OLEDB:Database Password= XXXXXX"

rst.Open qry, cnn, adOpenKeyset, adLockOptimistic

sh.Range("A10").CopyFromRecordset rst
  
rst.Close
cnn.Close

End Sub

Data on sheet is empty, but it should be 2 rows filled. When I remove WHERE part, it works fine and all 4 rows are there.
1686744085184.png
 

Attachments

  • 1686744023242.png
    1686744023242.png
    31.8 KB · Views: 5
Upvote 0
Solved...
Problem was Not Like doesn't work with 'null' fields.
I have to write something in empty rows instead null.

Please lock the topic.
 
Upvote 0
Solution
Not Like Null? Null is basically "unknown" so most comparisons cannot be compared to Null. There are 2 operations for this: IsNull (vba) which is a function, and Is Null (Access sql) as in
IsNull(myTextbox) or myField Is Null

If you read this carefully I expect you'll get a lot out of it.
BTW, posting pics of data is not the same as posting the data. I for one won't try to manually transpose a lot of data from a screen picture to a sheet in an effort to solve an issue, but that's just me.
 
Upvote 0
Problem was Not Like doesn't work with 'null' fields.
I have to write something in empty rows instead null.
If the data structure cannot be changed, then there will be always null values. You can use the following instead of editing records.

VBA Code:
qry = "SELECT * From db_actionlist WHERE [Status] <> 'Canceled' OR [Status] IS null"
 
Upvote 0

Forum statistics

Threads
1,224,885
Messages
6,181,574
Members
453,055
Latest member
cope7895

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top