Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SEARCHES 1

Status
Not open for further replies.

villica

Programmer
Feb 25, 2000
332
CA
I have a form with last name first name and Id number. I like to be able to do a search on any of the 3 fields above. I am not sure where to start to do a search. I am wondering if anyone has a code sample that I can use as guidance to create a search for the 3 fields above. Thank you for any suggestions.<br> <p>Villica<br><a href=mailto:villica67@hotmail.com>villica67@hotmail.com</a><br><a href= > </a><br>
 
One of the easiest ways is to put a button on your form and use the wizard to create the functionality you need.&nbsp;&nbsp;To get the wizard to start, make sure that the wand button on your toolbar is pressed when you drag the button onto the form.<br><br>You can also use the FindRecord method.&nbsp;&nbsp;By setting the focus to the field you want to search right before calling this method, you will make that field the current field, which is what the method defaults to.&nbsp;&nbsp;Look at the help file for the method for more detail.<br><br>Good luck
 
villica,<br><br>Assuming your form is Bound, say to a table or query, you'd have, in addition to all the Bound textboxes, 3 additional Unbound textboxes, called txtFirst,txtLast,txtID.<br><br>In the Click event of the search button do this:<br>(assume table is called Table1)<br>dim sq as string<br>sq = &quot;&quot; 'init<br>If NOT isnull(Me!txtID) then<br>&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND table1.ID = &quot; & Me!txtID<br>End If<br><br>If NOT isnull(Me!txtID) then<br>&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND table1.FirstName = &quot;&quot;&quot; & Me!txtFirst & &quot;&quot;&quot;&quot;<br>End If<br><br>If NOT isnull(Me!txtID) then<br>&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND table1.Lastname = &quot;&quot;&quot; & Me!txtLast & &quot;&quot;&quot;&quot;<br>End If<br><br>If sq &lt;&gt; &quot;&quot; 'if any criteria<br>&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;Where &quot; & right$(sq,len(sq)-3) 'strip the first AND<br>End IF<br>sq = &quot;SELECT * FROM Table1 &quot; & sq 'if sq is blank, no problem, select all<br><br>Me.Recordsource = sq 'setting recordsource automatically requeries<br><br>You can mess with the equality operators and add LIKE, etc, also you'll probaly want to do some validation to make sure ID is a number (if it is indeed a number) etc...<br>--Jim<br>
 
Hi Jim thank you for your help. I am wondering if is not too much trouble to help me figure out where the syntax error is.<br>Dim sq As String<br><br>sq = &quot;&quot;<br>If Not IsNull(Me!Idno) Then<br>&nbsp;&nbsp;sq = &quot;and Employee.Idno = &quot; & Me!Id<br>End If<br><br>If sq &lt;&gt; &quot;&quot; Then<br>sq = &quot;Where&quot; & Right$(sq, Len(sq) - 3)<br>End If<br><br>sq = &quot;Select * Employee&quot; & sq<br><br>&nbsp;&nbsp;Me!EmployeeSubform.Form.RecordSource = sq<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;If no records match criteria, reset subform's RecordSource property,<br>&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;display message, and move focus to Id text box.<br>&nbsp;&nbsp;&nbsp;&nbsp;'If Me!EmployeeSubform.Form.RecordsetClone.RecordCount = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;&nbsp;Me!EmployeeSubform.Form.RecordSource = &quot;SELECT * FROM Employee WHERE False;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;&nbsp;MsgBox &quot;No records match the criteria you entered.&quot;, vbExclamation, &quot;No Records Found&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me!Idno.SetFocus<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me!EmployeeSubform!EmployeeName.SetFocus<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;<br> <p>Villica<br><a href=mailto:villica67@hotmail.com>villica67@hotmail.com</a><br><a href= > </a><br>
 
villica,<br>You seem to be missing the FROM clause in the statement.&nbsp;&nbsp;&nbsp;Also, make sure you leave spaces between the concatentaded variables, ie:<br><br>sq = &quot;Select * from employees&quot;<br>sq = sq & &quot;Where idno = &quot; & me!idno '&lt;--ERROR, no leading space (alterntatively a trailing space in the first line)<br>me.recordsource = sq<br><br>The above will fail because the result is:<br>Select * from employeesWhere idno = 5&nbsp;&nbsp;'&lt;--No space between table name and 'where'<br><br>Also, you had me!id instead of me!idno in the sq clause..<br>--Jim
 
Hi Jim I changed the code as follow<br><br>Dim sq as string<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;&quot; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If NOT isnull(Me!IDno) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND Employee.ID = &quot; & Me!IDno<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If NOT isnull(Me!Last) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND Employee.LastName = &quot;&quot;&quot; & Me!Last & &quot;&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;If NOT isnull(Me!First) then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;AND Employee.Firstname = &quot;&quot;&quot; & Me!First & &quot;&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;If sq &lt;&gt; &quot;&quot; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;Where &quot; & right$(sq,len(sq)-3) <br>&nbsp;&nbsp;&nbsp;End IF<br>&nbsp;&nbsp;&nbsp;&nbsp;sq = &quot;SELECT * FROM Employee &quot; & sq <br><br>MsgBox &quot;test&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;Me!EmployeeSubform.Form.RecordSource = sq<br>&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;test&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;If Me!EmployeeSubform.Form.RecordsetClone.RecordCount = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Me!EmployeeSubform.Form.RecordSource = &quot;SELECT * FROM Employee WHERE False;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;No records match the criteria you entered.&quot;, vbExclamation, &quot;No Records Found&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Me!Idno.SetFocus<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;<br><br>End Sub<br><br><br>I am getting a runtime error 2001 &quot;cancelled previous operation&quot;<br>When I preesed the button debug it highlights this line and I can't see anything wrong with it. I think one of the most difficult things in access is creating sql statements. I need all the help I can get. <br><br>&nbsp;&nbsp;&nbsp;&nbsp;Me!EmployeeSubform.Form.RecordSource = sq <p>Villica<br><a href=mailto:villica67@hotmail.com>villica67@hotmail.com</a><br><a href= > </a><br>
 
villica,<br>Check the value of sq in the debug window (type ?sq while that line is highlighted in debug mode).&nbsp;&nbsp;Examine the statement for syntax.&nbsp;&nbsp;Copy the sql from the debug window and put it into a new query in sql view, and see if it runs.&nbsp;&nbsp;Assuming all table and field names are correct, what you have looks fine <i>however</i>... I made a mistake in my example...the statements should read<br>sq = sq & &quot;AND ...etc<br>rather than<br>sq = &quot;And...etc<br>But that is not the problem, the code should still work, you just wouldn't get all the criteria.<br>--Jim
 
Jim thank you thank you thank you. My search works wonderfull. Thanks again. You get another star * <p>Villica<br><a href=mailto:villica67@hotmail.com>villica67@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top