【问题标题】:How to filter an ADO recordset with AND operator如何使用 AND 运算符过滤 ADO 记录集
【发布时间】:2022-01-01 23:28:00
【问题描述】:

我有这个 VBA 代码,可以在 Microsoft Access 数据库中保存和编辑许多信息。

在一个 sub 中,我需要使用 AND 运算符过滤我的记录集。

我现在有这个:

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

我想把它变成这样的东西:

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

这是整个子:

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

任何帮助将不胜感激

【问题讨论】:

    标签: vba ms-access recordset


    【解决方案1】:

    AND 成为过滤字符串的一部分:

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

    【讨论】:

    • 完美运行,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多