By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

How Do I Create A Search Box?

P: 23
Hi folks, looking for help on what I reckon should be easy, but can't seem to find an easy solution.

I've tried searching loads of different solutions, but none seem to be for what I'm trying to do, a simple search box.

Using Access 97, I've created a form - frmTests - which shows, in Form view as continous forms, all records from a table relating to items being tested.

In previous databases, I would have just placed a command button with simply...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGoFindText_Click()
  2.  
  3.     Screen.PreviousControl.SetFocus
  4.     DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70
  5.  
  6. End Sub
  7.  
But with this database, I'm wanting to something very similar, except the search string input box already exists on the form, call it, for example, txtSearchString to save the user from scrolling down thousands of records. I type in a string to the text box, then click a "Go" button next to it, which will then carry out a sort of "Find Next" style function and highlight the text I'm looking for.

In my head, it seems a very simple task, which shouldn't have much code to it. I know I could just use the DoMenuItem feature instead, but it's not what the user wants.

Currently, my code looks like this, and appears to do nothing, no failure, no highlight..

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdGoFindText_Click()
  2.     If IsNull(Me.txtTextToFind) = True Then
  3.         MsgBox ("No search term entered!")
  4.         Me.txtTextToFind.SetFocus
  5.     Else
  6.         strWhere = Me.txtTextToFind
  7.         DoCmd.FindRecord strWhere, acEntire, True, acSearchAll, True, acAll, True
  8.     End If
  9.  
  10. End Sub
  11.  
Jul 26 '10 #1
Share this Question
Share on Google+
2 Replies


100+
P: 374
d4zza just to clarify you are wanting them to be able to find the first instance then click again to find the next instance etc? or just filter the records based on the string (which you seem to be able to do anyway)
Jul 26 '10 #2

P: 23
@munkee
Hi, sorry for late reply, was trying various solutions.

Just wanted a simple search once function, no filter. Unbound text box, txtTextToFind, on the form is used to enter a string of text to find, then user clicks a "Go" command button, and the form will find and highlight the first instance, no find next required.

I did crack it this morning though. Based on the result with previous script, where nothing appeared to happen, I added in a field reference to focus on before searching. Code is now...

Expand|Select|Wrap|Line Numbers
  1. strWhere = Me.txtTextToFind
  2. Me.ReferenceNo.SetFocus
  3. DoCmd.FindRecord strWhere
  4.  
I'm only having to search the one field, ReferenceNo, and they all have unique entries.
Jul 27 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.