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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo to select from a subset of records from a table

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
GB
I want to create a drop-down combo class (amcombo) which lets the user select from a subset of records from a table. there would not be a great number of records in this subset - usually less than 100.

In skeletal form, the user would have mytable.dbf with these fields :
Code:
RecType C(1), MyCode C(6), MyDesc C(30)

The combo would be interested only in records of a particular type, say those with RecTyp = “A”. Then whatever value is keyed into the combo could be used to further define which records are to be shown in the drop-down box. This would be different in each application, but one requirement would be to offer those records where the string entered into the combo say “AB” appears somewhere in MyCode or MyDesc.

To that end I plan to have these properties in this Combo Class
.zfile The alias of the table to be searched. In this case “MyTable"
.zKey The name of the keyfield. In this case “Mycode”
.zDesc The name of the description field. In this case “MyDesc”
.zFilter The string to filter the records. In this case “Rectyp = ‘A’”

Then, specific to the instance, the programmer would be able code some method (which one?) of the class to define which records to be offered. So (as above) if he decided that the user should be allowed to search from records with “AB” in the key or description by entering “#AB” into the combo, there would be code such as :
Code:
IF LEFT(This.value,1) = “#”
    lcSearch = RTRIM(SUBSTR(This.value,2))
   . . . followed by some code either to set a filter or possibly extract records into a new cursor or array.

The questions which I have (apart from “Am I going about this the right way”?) include :
What should I set as the Rowsource of the combo; should I use the original MyTable, subject to a filter, or should I extract the records to a new cursor or array in the Init() method of amcombo?

Would I need to recreate a cursor or array each time the user specifies the group of records he is interested in (e.g. those containing “AB”)? Or would I do this by applying or modifying a filter?

Which methods of amcombo do I need to code to achieve the result.

I would mention that MyTable may well be used elsewhere on the form. In a particular example I am thinking of, I have a table of nominal account codes, some of which are bank accounts. So the user may be invited to select a bank account, while elsewhere on the form he may select from a wider range of accounts.

Thanks.

 
Andrew,

Judging by your description, a drop-down combo is not what you want. In a drop-down combo, the characters that the user types in the editable portion actually become the value of the control. They are not used as a filter to determine what values should appear in the drop-down part.

It's true that you could construct a drop-down combo that meets your requirements, but there are better ways of achieving the goal.

First, consider a text box with autocomplete enabled. That will do what you want, with very little programming needed. The limitation is that it will find entries that begin with the specified characters rather than ones that contain the specified characters.

Another option is to use a text box along with grid. In the text box's InteractiveChange, you write code that filters the subset of records that are used to populate the grid. (That's a simplification, but it should give you the general idea.)

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for that Mike

I take your point that if I let the user key in a more general search key, that key will not remain on the screen after the dropdown of the combo has done its thing.

And thank you for reminding me that the place where I can respond to the user’s input is the InteractiveChange() event.

However, after some thought I felt that the facilities of the drop down combo showing relevant fields from the data set in a drop down window, letting the user select one, tidying up &c - are useful for my purposes, so I have stayed with that.

In fact I offer the user two options : If he keys in a string beginning with # (Hash key) I do a string search (on both fields); if he keys in a normal alphanumeric string the combo works as you would expect. In each case the final value in the text box is the key of the record, in the normal way.

One thing which came up is that I found that I had to use an array (5) as the RowSource. I tried using a temporary cursor (6) e.g. tdata.key, desc, but it didn’t seem to like that. Apart from that it works fine.

Andrew
 
Glad to hear you've got a solution, Andrew. Personally, I don't like the idea of requiring the user to use special keystrokes (a # in this case) to achieve a goal. But you know your users better than I do.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If you would have stayed with a cursor you would have needed to filter it instead of requerying it. Not only the grid is sensitive about a recreated source of it's data binding, also other controls. The grid only is far more complex and sensitive to this change than listbox or combobox and a textbox is almost indescructable with this kind of binding errors.

Bye, Olaf.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top