Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter an ADO recordset with AND operator

I have this VBA code that save and edit many informations in a Microsoft Accsess database.

In one sub, I need to filter my recordset with an AND operator.

I have this now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

rst.Filter = "OT ='" & oot & "'"
rst.Filter = "Parcial ='" & PARCIAL & "'"
rst.Delete

And I would like to transform it into something like this:

rst.Filter = "OT ='" & oot & "'"  AND  "Parcial ='" & PARCIAL & "'"
rst.Delete

Here is the entire sub:

Sub delete_ot()

ThisWorkbook.Activate
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rst As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath
Dim x As Long, i As Long
Dim nextrow As Long
Dim wsc As Worksheet
Dim wb As Workbook
Dim oot As String
Dim PARCIAL As String

PARCIAL = Corte.PARCIAL

oot = CStr(Corte.TextBox16)

On Error GoTo errHandler:


Set wb = ThisWorkbook
Set wsc = wb.Worksheets("Auxiliar")

Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path
Dim pasta As String

pasta = folderPath & "\" & wsc.Range("J2").Value
dbPath = pasta & "\" & wsc.Range("J4").Value & ".accdb"

'Initialise the collection class variable
Set cnn = New ADODB.Connection

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath

Set rst = New ADODB.Recordset
'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rst.Open Source:="Registro_corte", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable


rst.Filter = "OT ='" & oot & "'"
rst.Filter = "Parcial ='" & PARCIAL & "'"
rst.Delete

'close the recordset
rst.Close
' Close the connection
cnn.Close
'clear memory
Set rst = Nothing
Set cnn = Nothing

On Error GoTo 0
Exit Sub
errHandler:
'Clear memory
Set rst = Nothing
Set cnn = Nothing
MsgBox "Error FAVOR AVISAR ENGENHARIA " & err.Number & " (" & err.Description & ") in procedure Delete_OT"

End Sub

Any help would be highly appreciated

>Solution :

AND becomes part of the filter string:

rst.Filter = "OT ='" & oot & "' AND Parcial ='" & PARCIAL & "'"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading