473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Sorting records in form after text search command

I found some code for text-searching in forms (using an unbound text box and a command button), and adapted it to look like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command83_Click()
  2. Dim strPersonRef As String
  3.     Dim strSearch As String
  4.  
  5. 'Check txtSearch for Null value or Nill Entry first.
  6.  
  7.     If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Or (Me![txtsearch]) = "Surname" Then
  8.         MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
  9.         Me![txtsearch].SetFocus
  10.     Exit Sub
  11. End If
  12.  
  13. 'Performs the search using value entered into txtSearch
  14. 'and evaluates this against values in Surname
  15.  
  16.     DoCmd.ShowAllRecords
  17.     DoCmd.GoToControl ("Surname")
  18.     DoCmd.FindRecord Me!txtsearch
  19.  
  20.     Surname.SetFocus
  21.     strPersonRef = Surname.Text
  22.     txtsearch.SetFocus
  23.     strSearch = txtsearch.Text
  24.  
  25. 'If matching record found sets focus in Surname and shows msgbox
  26. 'and clears search control
  27.  
  28.     If Surname = strSearch Then
  29.         MsgBox "Matches Found For: " & strSearch, , "Congratulations!"
  30.  
  31.         Surname.SetFocus
  32.         txtsearch = ""
  33.  
  34.     'If value not found sets focus back to txtSearch and shows msgbox
  35.         Else
  36.            MsgBox "Match Not Found For: " & strSearch & " - Please Try Again.", _
  37.             , "Invalid Search Criterion!"
  38.             txtsearch.SetFocus
  39.     End If
  40.  
  41. End Sub
The problem I now have is that the results are now sorted by the primary key of the table this form applies to, instead of alphabetically by surname, which I would like. This is also how I've set it to sort when the form opens. How can I make this bit of code re-sort the results?
Nov 28 '07 #1
4 2209
Your code does not appear to do anything with the sort order. The form by default will sort by the primary key unless you change it.

You can just change the sort order in the form's design or add code to do it.

Example:

Me.OrderBy = "FieldName"
Me.OrderByOn = True
Nov 28 '07 #2
Your code does not appear to do anything with the sort order. The form by default will sort by the primary key unless you change it.

You can just change the sort order in the form's design or add code to do it.

Example:

Me.OrderBy = "FieldName"
Me.OrderByOn = True
That's the odd thing, I did already set Order By in the form design - running this code seems to somehow 'un-set' it.

I managed to re-sort it by putting those in after DoCmd.Showallrecords, and it seems to be working, cross fingers. I'm not so familiar with this stuff, and didn't know what the code for 'order by' was, so many thanks for the help.
Nov 28 '07 #3
Jim Doherty
897 Expert 512MB
I found some code for text-searching in forms (using an unbound text box and a command button), and adapted it to look like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command83_Click()
  2. Dim strPersonRef As String
  3. Dim strSearch As String
  4.  
  5. 'Check txtSearch for Null value or Nill Entry first.
  6.  
  7. If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Or (Me![txtsearch]) = "Surname" Then
  8. MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
  9. Me![txtsearch].SetFocus
  10. Exit Sub
  11. End If
  12.  
  13. 'Performs the search using value entered into txtSearch
  14. 'and evaluates this against values in Surname
  15.  
  16. DoCmd.ShowAllRecords
  17. DoCmd.GoToControl ("Surname")
  18. DoCmd.FindRecord Me!txtsearch
  19.  
  20. Surname.SetFocus
  21. strPersonRef = Surname.Text
  22. txtsearch.SetFocus
  23. strSearch = txtsearch.Text
  24.  
  25. 'If matching record found sets focus in Surname and shows msgbox
  26. 'and clears search control
  27.  
  28. If Surname = strSearch Then
  29. MsgBox "Matches Found For: " & strSearch, , "Congratulations!"
  30.  
  31. Surname.SetFocus
  32. txtsearch = ""
  33.  
  34. 'If value not found sets focus back to txtSearch and shows msgbox
  35. Else
  36. MsgBox "Match Not Found For: " & strSearch & " - Please Try Again.", _
  37. , "Invalid Search Criterion!"
  38. txtsearch.SetFocus
  39. End If
  40.  
  41. End Sub
The problem I now have is that the results are now sorted by the primary key of the table this form applies to, instead of alphabetically by surname, which I would like. This is also how I've set it to sort when the form opens. How can I make this bit of code re-sort the results?
Just an observation :) Are you sure and happy that you are getting the full functionality you require overall? you have a bunch of code there that is just dealing with one specific element and that is surname. What about other fields ie Firstname date of birth etc etc. If you do not need that then ok... I merely illustrate a point... but consider this ...if you did need that you would have to repeat all of that all over again for each field.

In answer to your question....what is your form that provides the results basing itself on? a table? if so swap that to reference a query sorted by surname Also remember that the FindRecord method you use there finds the first match!! What do you if there is fifteen or sixteen 'Smiths' stacked and sorted together in single form view you wouldnt know 'how many' satisfy the search mechanism doing it that way.

If the form is is continuous view then ok you would see a stacked result sorted by surname but not the necessarily the number of? if you understand me.

A simple way of determining a result from a potential search like that would be to use a DCount function in an unbound textbox that references the unbound surname search field as its criteria and counts the relevant table for you based on the value you provided. That would be a visual thing straight away and could be used to dictate what happens next... so to speak.

Regards

Jim
Nov 28 '07 #4
Just an observation :) Are you sure and happy that you are getting the full functionality you require overall? you have a bunch of code there that is just dealing with one specific element and that is surname. What about other fields ie Firstname date of birth etc etc. If you do not need that then ok... I merely illustrate a point... but consider this ...if you did need that you would have to repeat all of that all over again for each field.

In answer to your question if I am interpreting correctly where you are at with this....what is your form that provides the results basing itself on? a table? if so swap that to reference a query sorted by surname Also remember that the FindRecord method you use there finds the first match!! What do you if there isfifteen or sixteen 'Smiths' stacked and sorted together in single form view you wouldnt know 'how many' satisfy the search mechanism doing it that way.

If the form is is continuous view then ok you would see a stacked result sorted by surname but not the necessarily the number of? if you understand me.

A simple way of determining a result from a potential search like that would be to use a DCount function in an unbound textbox that references the unbound surname search field as its criteria and counts the relevant table for you based on the value you provided. That would be a visual thing straight away and could be used to dictate what happens next... so to speak.

Regards

Jim :)
You're certainly right in that it could be far more functional, but this'll probably do for now - this is my main form (multi-page) and has a vast, vast number of subforms, so I want to keep things as easy as possible for now considering I'm not particularly au fait with coding.

Many thanks...
Nov 29 '07 #5

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

Similar topics

12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
2
by: Aravind | last post by:
Hi folks. I have a form, frmHistory, which has 4 command buttons: Sort Title (cmdSortTitle), Sort Name (cmdSortName), Due Today (cmdDueToday), and Due List (cmdDueList). Sort Title and Sort...
2
by: Todd | last post by:
Hi. I want to sort the records on my form (using either a continuous form or a datasheet) by the unbound "description" column in a combo box on the form (or in the datasheet.) Here's a rough...
5
by: Miguel Dias Moura | last post by:
Hello, I have a search form in an ASP.NET/VB page. The form has the input text box and the button "search". The keywords are passed in the URL to results.aspx. Here is an example:...
8
by: Matthew Curiale | last post by:
I am creating an app that lists clients of a company for management of different attributes for that company. The first page is a listing of the companies currently in the database. I have my...
5
by: jean.ulrich | last post by:
Hi I have a continous form that contain 10 fields I would like to be able to sort 3 fields with a command button example columnA can be 100 or 200 or 300 (there are many records of the same...
6
jinalpatel
by: jinalpatel | last post by:
I am using following code for searching records. 'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter. 'Notes: 1. We tack " AND " on...
2
by: didacticone | last post by:
hi, i was wondering if someone can help me, i have a form, with an unbound text box and a command button (code below) that searches through the underlying table and filters the results to show only...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.