搜索框的消息框未返回任何结果,Access 2010

时间:2016-07-02 04:07:20

标签: vba ms-access ms-access-2010

我是VBA的新手,但我想为包含多个字段的拆分表单创建一个搜索框。我开始使用宏,然后最终将其转换为VBA,因此代码中可能存在错误。

无论如何,我使用了apply filter命令,我希望在没有结果时弹出一个消息框,但是这段代码不起作用:

mysql> SELECT * FROM payments;
+----+------------+---------+-------+
| id | date       | user_id | value |
+----+------------+---------+-------+
|  1 | 2016-06-22 |       1 |    10 |
|  2 | 2016-06-22 |       3 |    15 |
|  3 | 2016-06-22 |       4 |    20 |
|  4 | 2016-06-23 |       2 |   100 |
|  5 | 2016-06-23 |       1 |   150 |
|  6 | 2016-06-23 |       2 |   340 |
+----+------------+---------+-------+
6 rows in set (0.00 sec)

mysql> select max(value),user_id from payments group by user_id;
+------------+---------+
| max(value) | user_id |
+------------+---------+
|        150 |       1 |
|        340 |       2 |
|         15 |       3 |
|         20 |       4 |
+------------+---------+
4 rows in set (0.00 sec)

mysql> select max(T.value),TT.user_id from payments T inner join (select max(value) as val,user_id from payments group by user_id) TT on T.value!=TT.val and T.user_id=TT.user_id group by T.user_id;;
+--------------+---------+
| max(T.value) | user_id |
+--------------+---------+
|           10 |       1 |
|          100 |       2 |
+--------------+---------+
2 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:2)

您可以通过问题中的语法着色来判断字符串构造不起作用。使用单引号'会容易得多。还有一些变数。

Private Sub cmdSearch_Click()

    On Error GoTo cmdSearch_Click_Err
    Dim sSearch As String, sFilter As String

    sSearch = "'*" & [Forms]![Main Menu]![txtSearch] & "*'"
    sFilter = "[IDTag] Like " & sSearch & " Or [Title] Like " & sSearch
    Debug.Print sFilter  ' Use Ctrl+G to see Debug output

    DoCmd.ApplyFilter "", sFilter

如果没有结果时显示MsgBox,请参阅链接的副本。