Connecting Tech Pros Worldwide Forums | Help | Site Map

Search routine for a data access page.

Member
 
Join Date: Nov 2006
Location: Indiana
Posts: 77
#1: Sep 24 '07
This loop searches through all the fields in a table. I get a problem when executing it says "expected end of statement"; line 24 on here. Syntax problem? I don't see it.

Expand|Select|Wrap|Line Numbers
  1. 'code from MSDN for a simple search function.
  2. 'http://support.microsoft.com/kb/271728
  3.  
  4. dim i           'Counter variable
  5. dim rs          'ADO recordset object
  6. dim fld         'ADO field object
  7. dim FieldCount  'Number of fields in the recordset
  8.  
  9. FieldCount = MSODSC.DefaultRecordset.Fields.Count
  10.  
  11. 'This will return the default recordset on the page
  12. 'in this case, the Customers table.
  13. set rs = MSODSC.DefaultRecordset
  14.  
  15. for i = 0 to FieldCount - 1
  16.     'get a field object
  17.     set fld = rs.Fields(i)
  18.  
  19.     '0 = Skip no records
  20.     '1 = Search forward
  21.     '1 = Start with the first record
  22.  
  23. 'the next line is where the debugger stops on the error.
  24.     rs.Find fld.name & " = '" & txtSearch.value & "'", 0, 1, 1 
  25.  
  26.     'Check for EOF.  If  at EOF but have not exhausted
  27.     'all the fields, then reset to the first position in the 
  28.     'recordset.  Otherwise, if a match was found, exit the loop.
  29.     if rs.EOF then 
  30.         rs.MoveFirst
  31.     else
  32.         exit for
  33.     end if
  34. next
  35.  
  36. 'Clean up.
  37. set fld = nothing
  38. set rs = nothing

Member
 
Join Date: Nov 2006
Location: Indiana
Posts: 77
#2: Sep 25 '07

re: Search routine for a data access page.


This is how I got it to work. There are quite a few questions about this floating around on here so I will try to post the link on there discussions.

FOR A DATA ACCESS PAGE! Straight from microsoft.
This will search a single Integer Field for an Integer given by the user and display the corresponding record set. No wildcards allowed in this example.

(A string search will also work with a bit of tweaking)

In design view
1. turn off the controls wizard
2. create a command button and name it cmdSearch
3. go to Tools --> Macro --> Microsoft Script Editor
4. from the Objects and Events box select cmdSearch
5. from the Events box(to the right) select OnClick
6. this will set your cursor in the correct place to insert the following code.


Expand|Select|Wrap|Line Numbers
  1. ' Clone the recordset.
  2.  
  3. Dim rs
  4. Set rs = MSODSC.DataPages(0).Recordset.Clone
  5. On error resume next
  6.  
  7. ' This line assumes that the value you are filtering on is an integer.
  8. ' If the search value is a string, use slightly different syntax.
  9. ' For example, "Field you want to search = '" & CStr(InputBox("Please enter Whatever you want to find", "Find")) & "'" 
  10. rs.find "[yOUR fIELD HERE]=" & cLng(inputbox("Enter Something to find","Find"))
  11. ' Custom error handling.
  12. If (err.number <> 0) Then
  13.     Msgbox "Error: " & err.number & " " & err.description,,"Invalid Search"
  14.     Exit Sub
  15. End If
  16. ' Check search results for success.
  17. If (rs.bof) or (rs.eof) Then
  18.     Msgbox "No wHATEVER found",,"Search Done"
  19.     Exit Sub
  20. End If
  21. MSODSC.DataPages(0).Recordset.Bookmark = rs.Bookmark
  22. -->

Hope this helps.

James
Member
 
Join Date: Nov 2006
Location: Indiana
Posts: 77
#3: Sep 26 '07

re: Search routine for a data access page.


Alright so I got it to work searching for Integers only. I thought I would try out searching for strings so I take the:
Expand|Select|Wrap|Line Numbers
  1. rs.find "[WO NUMBER]=" & cLng(inputbox("Enter a Workorder Number","Find"))
and change it to:
Expand|Select|Wrap|Line Numbers
  1. rs.find "[CUSTOMER NAME]= " & CStr(InputBox("Please enter customer to find", "Find"))&""
and it gets an error about using the wrong data type. It will find an integer (a number) if I put one into the table. I tried making a customer named 12345 and used the CStr syntax above and it works fine. It will not, however, find a string.

In the example their is an example to search a string:
Expand|Select|Wrap|Line Numbers
  1. ' If the search value is a string, use slightly different syntax.
  2. ' For example, "CustomerID = "'" & CStr(InputBox("Please enter customer to find", "Find")) & "'" 
this in itself will not work. the syntax is wrong.
Member
 
Join Date: Nov 2006
Location: Indiana
Posts: 77
#4: Sep 28 '07

re: Search routine for a data access page.


This here is teh goodness.
Expand|Select|Wrap|Line Numbers
  1. rs.find "[Customer NAME] = '" & CStr(InputBox("Please enter customer to find", "Find")) & "'"
Now I'm trying to get wildcards to work. From what I've read you can change the = to LIKE.
Expand|Select|Wrap|Line Numbers
  1. rs.find "[job name] LIKE '" & CStr(InputBox("Please enter a job name to find", "Find")) & "'"
This will not return a record though. I think this will become a "How To" search for record sets for noobs before I'm done with it.
Newbie
 
Join Date: Mar 2008
Posts: 1
#5: Mar 17 '08

re: Search routine for a data access page.


Quote:

Originally Posted by kcddoorman

This here is teh goodness.

Expand|Select|Wrap|Line Numbers
  1. rs.find "[Customer NAME] = '" & CStr(InputBox("Please enter customer to find", "Find")) & "'"
Now I'm trying to get wildcards to work. From what I've read you can change the = to LIKE.
Expand|Select|Wrap|Line Numbers
  1. rs.find "[job name] LIKE '" & CStr(InputBox("Please enter a job name to find", "Find")) & "'"
This will not return a record though. I think this will become a "How To" search for record sets for noobs before I'm done with it.

I have somewhat of a solution for this. Change your line to the below:

rs.find "[job name] LIKE %" & CStr(InputBox("Please enter a job name to find", "Find")) & "%"

However, It only brings you to the closest record that you typed and doesnt filter the records down which would be ideal. Any thoughts of how to set it to not only find but filter?
Reply