如何使用 MySQL 后端在 MS Access 中创建(多字段)搜索表单?

How to create a (multi-field) search form in MS Access with MySQL Backend?

我正在为 MySQL 后端创建一个访问表单前端,我想知道是否有办法为 table 创建搜索多个字段的搜索表单。在寻找有关此主题的答案时,我似乎找到的共识是 "Learn and use VBA",但我想知道在使用 MySQL 后端时这是否仍然适用。由于我相对精通 MySQL,发起查询以搜索多个字段是微不足道的,但我不知道如何将其合并到 Access 中。有没有人有这方面的经验可以提供帮助?

是的,工作量最少,最好的选择就是简单地抛出一个表单,让用户输入要搜索的值,然后将 "where" 子句传递给表单或报告你要过滤。

Access 在上述方面做得相当好,只会拉下符合条件的记录。

所以说要搜索客户,你扔一个这样的表格:

因此,在上面,用户输入了 Smi。

所以,我们的代码可以这样:

me.RecordSource = "select * from tblCustomers where LastName like '" & me.txtSearch & "*'"

现在,在那种情况下我们确实创建了 SQL。但你也可以去:

docmd.OpenReport "rptCustomers",,,"LastName like '" & me.txtSearch & "*'"

因此,将 where 子句传递给表单将消除编写或连接 sql 字符串的需要。

显示结果后,您就可以如上图所示向下钻取。当您单击上面的一行(眼镜图标)时,我们将表单启动到一条记录:

docmd.OpenReport "ViewTour","id = " & me!id

因此,在大多数情况下,通过使用 "where" 子句,您不必创建或编写 SQL 查询。

如果您的表单只是简单地说出姓名和一个可选的城市?

dim strWhere   as string

if isnull(me.LastName) = False then
   strWhere = "LastName = '" & me.LastName & "'"
End if

If isnull(me.City) = False then
    if strWhere <> "" then strWhere = strWhere & " AND "
    strWhere = strWhere & "City = '" & me.City & "'"
end if

' as many more "optional" text boxes on the search form can follow
' if you leave the box blank, then the critera is not added.