MS Access通过文本框而不是下拉列表搜索记录 [英] MS Access search for record by textbox instead of dropdown
问题描述
我对MS Access还是很陌生.我正在尝试创建一个简单的表单,该表单将基本上使用文本框而不是下拉框来搜索特定记录.本质上,用户将能够输入ID号并检索其他一些相关的信息.但是,我不希望用户能够将任何新记录添加到数据库中.我已经能够使表单看起来像我想要的样子,但是我不确定将代码放置在哪里(是否创建宏,将代码插入按钮的属性中?)任何帮助都很大感谢!
I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
推荐答案
我假定您已将表单绑定到表或查询,并且希望能够在文本框中手动输入ID,然后按Enter.并加载该记录的数据,如果没有这样的记录,则会显示一条错误消息.
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
为 dsteele 说,请确保将表单的数据属性允许添加设置为No
以禁止用户添加记录.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No
to disallow users from adding records.
然后,从文本框的AfterUpdate
事件添加以下代码(假设您的文本框名为txtGoTo
):
Then, from the AfterUpdate
event of the textbox, add the following code (assuming that your textbox is named txtGoTo
):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
请注意,您必须将行rs.FindFirst "[ID]=" & txtGoTo
更改为适合您的数据的行:
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo
to something that is adequate for your data:
-
"[ID]="
应该替换为您要搜索的字段(可以是"[POReference]="
或其他名称.
"[ID]="
should be replaced by the field you want to search (it could be"[POReference]="
or something else.
如果要通过数字ID进行搜索(例如,因为该字段是自动编号列),则该代码很好.
否则,如果要搜索的字段是字符串(例如PN12-G
),则必须将代码更改为:
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G
) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
不使用正确的引号(或在不必要的地方使用引号)将导致错误类型数据类型不匹配... .
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
以新用户身份,我建议您查看示例 NorthWind 项目数据库,该数据库随较早版本的Access一起提供,或可作为从Access 2007下载的模板使用. br> 作为新的Access开发人员,有很多技术可以学习,包括其他实现记录导航的方法.
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
这篇关于MS Access通过文本框而不是下拉列表搜索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!