使用条件搜索字段,等等

时间:2016-05-20 18:36:57

标签: vba ms-access

我需要一个vba中的命令,它将从绑定到我的[采购订单列表]表的表单中搜索我的[部件名称列表]表

两个表都有[部件名称]并且在[部件名称列表]表中它永远不会重复

----我需要搜索一个操作----

操作存储在[部件名称列表],[OP1],[OP2]等的6个不同字段中,等等

采购订单表格中有9个关键字由复选框表示,每个OP都应该包含其中一个确切的关​​键字,在少数情况下,零件可以有多个实例,例如我的部分零件需要锯切多个一旦这样,我将需要返回这两个

1 个答案:

答案 0 :(得分:0)

我不完全确定我理解你的问题,但这可能会让你失望:

Dim i As Integer
Dim strWHERE As String
Dim strSQL As String

strWHERE = ""

For i = 1 To 9
   If Me("OP" & i) = True Then
      strWHERE = strWHERE & "'" & Me("OP" & i).Tag & "',"
   End If
Next i

If strWHERE <> "" Then
    strWHERE = Left(strWHERE, Len(strWHERE) - 1)
    strSQL = "SELECT OP1 AS Operation FROM [part name list] WHERE OP1 IN (" & strWHERE & ") " & _
        "UNION SELECT OP2 AS Operation FROM [part name list] WHERE OP2 IN (" & strWHERE & ") " & _
        "UNION SELECT OP3 AS Operation FROM [part name list] WHERE OP3 IN (" & strWHERE & ") " & _
        "UNION SELECT OP4 AS Operation FROM [part name list] WHERE OP4 IN (" & strWHERE & ") " & _
        "UNION SELECT OP5 AS Operation FROM [part name list] WHERE OP5 IN (" & strWHERE & ") " & _
        "UNION SELECT OP6 AS Operation FROM [part name list] WHERE OP6 IN (" & strWHERE & ")"
    'open recordset
End If

这将使您获得与采购订单上所选关键字之一匹配的所有操作。如果没有样本输出,很难判断这是否是您需要的。

如果您需要匹配[部件名称],您也可以将其包括在WHERE标准中。