我在MS Access中有一个非常基本的应用程序来检查一些数据。我在表单中有一个Texbox和一个按钮。我的简单目标:我只想在测试框中写一些东西,单击按钮,然后检索与该搜索匹配的第一条记录。因此,我可以检查所有字段。然后,我再次单击并获得下一条记录,等等。如果我在“单击时”按钮事件中编写了一些基本代码
DoCmd.FindRecord "XXX", , True, , True
它可以正常工作,并且找到带有XXX的第一条记录。
但是,当我替换固定的“ XXX”并使用Texbox中的文本(称为ProjectBox)时,它不起作用。
DoCmd.FindRecord ProjectBox.Value, , True, , True
DoCmd.FindRecord ProjectBox.Text, , True, , True
两条陈述都失败...您能建议些什么吗?先感谢您!
好了,您可以编写代码来搜索+检查数据记录,但是为什么不只在现有表单上放置一个文本框呢?您输入一些搜索(例如XXX),然后在该文本框旁边放置一个按钮。
所以,说你有这个:
所以我放了一个文本框(txtSearch)和一个按钮。
因此,现在单击按钮时的代码可以是:
Private Sub Command13_Click()
Me.Filter = "HotelName like '*" & Me.txtSearch & "*'"
Me.FilterOn = True
End Sub
因此,现在对表单进行了过滤,您可以通过所有匹配的记录来回浏览。我的意思是,您想过滤一些数据-但是您想在哪里,何时何地处理结果?说提取一些数据并仅在代码中对其进行过滤没有多大意义,因为那样您(或用户)如何处理这些结果数据?
当然,任何形式的用户都可以按Ctrl-F,然后会弹出此对话框:
因此,这并不是什么,因为它是内置的,所以您通常不需要编写任何代码。
Perhaps you wondering how to traverse, seach, process the data but NOT with the UI (user interface) but ONLY say in VBA code?
Well, then you could write this code that takes the input from the text box above and do this:
Dim strSQL As String
strSQL = "SELECT ID, HotelName FROM tblHotels WHERE HotelName LIKE '*" & Me.txtSearch & "*'"
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset(strSQL)
Do While rst.EOF = False
Debug.Print "ID and Hotel name match = " & rst!ID & "," & rst!HotelName
rst.MoveNext
Loop
rst.Close
So the above is pure code - does not use the form. So, if in above I typed in Banff, then the debug.print (ctrl-g - immedate window) would display this output:
Output:
ID and Hotel name match = 153,Banff Springs
ID and Hotel name match = 188,Banff Hotelling
ID and Hotel name match = 206,Banff Inn
ID and Hotel name match = 292,Best of Banff
So the issue is NOT writing code to search or filter data, but WHERE, WHEN, HOW you going to present that data to the user.
由于您要在文本框中键入一些搜索内容,因此构建表单很有意义,但是如上所述,表单具有内置的搜索功能,但是您当然可以在表单上设置过滤器,并且那么下一个或上一个移动的所有“动作”将通过表格中内置的简单的下一个/上一个功能来处理。因此,您只需要编写少量代码即可进行过滤-但是表单的内置导航功能因此使您不必编写所有下一个/上一个代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句