PRB: Cannot Filter ADO Recordsets for NullsID: Q187871
|
Using the Filter property of an ActiveX Data Objects (ADO) Recordset to filter out records containing NULLs always fails. It may fail with the following error or may accept the Filter property, but fail to apply the filter:
Run-time error '3001': The application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another.
Currently, it is not possible to use the ADO Recordset's Filter property to filter records for NULL values.
Change the SQL statement used to populate the recordset so that it handles the nulls. For instance, the following would only retrieve the records that have a null value for state value:
rs.Open "select * from authors where state is null", cn
'Don't forget to add a reference to the
'Microsoft ActiveX Data Objects Library
Dim cn As New Connection
Dim rs As New Recordset
Dim vFields() As Variant
'This example is connecting to SQL Server's Sample Pubs database.
'You may want to change the server name to reference your local
'SQL Server
cn.Open "driver={SQL Server};" & _
"server=local;uid=sa;pwd=;database=pubs"
rs.CursorLocation = adUseClient
'Create some records with Null values
cn.Execute ("update authors set state = NULL where state = 'UT'")
rs.Open "select * from authors", cn, adOpenStatic, adLockBatchOptimistic
'Build an array of bookmarks of records where desired field contains
'null.
i = 0
Do While rs.EOF <> True
If IsNull(rs(6).Value) Then
ReDim Preserve vFields(i)
vFields(i) = rs.Bookmark
i = i + 1
End If
rs.MoveNext
Loop
Debug.Print rs.RecordCount
rs.Filter = vFields
Debug.Print rs.RecordCount
'Reverse the changes made by the .Execute call above.
cn.Execute ("update authors set state = 'UT' where state = NULL")
This behavior is by design.
Data Access SDK Help; search on: "Filter Method"
ADO Help; search on: "Filter Method"
Additional query words: kbnokeyword kbado200 kbDatabase kbSweepNext kbVBp600
Keywords : kbDatabase kbVBp600 kbSweepNext
Version : WINDOWS:2.0
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 27, 1999