Topic: ComboBox Search

Hi Dmitry and Guys,


I was trying to use combobox as an increm. search.


For example, there is many names starting with J in customers db. When I type J in combobox, it displays first record starting with J and dropdown list shows all records. What I like to do is when I type J in combo box and open combo dropdown list it should only be shoving names starting with J instead of all records.


Ps/. For Dmitry, when I type %$% in combobox properties/filter it crashes the app on compile. App runs on invisible mode. Only way to terminate it by using Windows Task Manager.

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

At the office right now, so not much time but....


Did you try with %s% instead of %$% in the Filter for the combobox ?

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: ComboBox Search

Thanks Mathias... Unfortunately, that crashes it too.

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Hello AD1408

It's true, when you enter a value (i.e J% or other) in the property of your comboBox, the application crashes.
The best way to succeed is to use a script behin MyCombobox_OnChange, of the kind :

  Filter := ComboBox1.Text;      (Filter is a variable typed String)
  ComboBox1.DroppedDown := True;
  ComboBox1.Text := Filter;
  ComboBox1.SelStart := Length(Filter);

But why to have only customers names beginning by J.in your combobox ?
As you fill properties : TableName and FieldName, automatically your combobox will be filled with all the datas behind FieldName.
Do you associate a tablegrid with your combobox ?

JB

Re: ComboBox Search

Re Hello AD1408

Youc could also use this code modifying it according to your application :

procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
begin
if key = VK_F9 then begin
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('SELECT Field1 FROM Table1 WHERE Field1 LIKE "%' + ComboBox1.Text + '%"');
Query1.Open;

ComboBox1.Items.Clear;

while NOT Query1.Eof do begin
ComboBox1.Items.Add(Query1['Field1']);

Query1.Next;
end;

if ComboBox1.Items.Count > 0 then
ComboBox1.ItemIndex := 0;
end;
end

JB

Re: ComboBox Search

jean.brezhonek wrote:

Hello AD1408

But why to have only customers names beginning by J.in your combobox ?
As you fill properties : TableName and FieldName, automatically your combobox will be filled with all the datas behind FieldName.
Do you associate a tablegrid with your combobox ?

JB


Hi JB,


Thanks a lot for the info and suggestions...


I'm sorry, perhaps I wasn't clear enough. I'm trying to use combobox search to locate records on different table to insert into current form.  Combobox itself without search may be good enough in most cases but when you have 100s of items to pick from combo search needed. Please see the attached sample project

Post's attachments

Attachment icon Insert.zip 6.1 kb, 494 downloads since 2016-10-20 

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Please check out this example
http://myvisualdatabase.com/forum/misc. … download=1

Dmitry.

Re: ComboBox Search

Hi Dmitry,


Thank you very much......

Nice.. That's what I was looking for.

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

I wanted add search in combobox on another form (frmInvoice) where combo field contains firstname and lastname. {firstname} {lastname}
It works fine with last name but not with first name or both together.


on frmInvoice
i.e. typing bai finds all persons with lastname Bailey and lists in combo drop-down OK.
However, typing david or david bai doesnt find anything?
There is a David Bailey saved in person db.


Please see the attached sample projects:

Post's attachments

Attachment icon Search in ComboBox 3.zip 104.68 kb, 485 downloads since 2017-02-01 

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Hello AD1408

To display two fields in a combobox, place a comma between two values :  {firstname} ,{lastname}

Example :

Name: {firstname}, phone {phonefield}

Result :

Name: Smith, phone +134643643


JB

Re: ComboBox Search

Hi JB,


Thanks for the extra information.
However, my question remains unanswered. Please see my previous question above post#9
It's about searching in combobox.

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Adam, your combobox filter needs to include the first name. Replace your frmInvoice.cbCustomer.dbFilter with the following:

frmInvoice.cbCustomer.dbFilter := '(lastName LIKE ''%'+ s_cbCustomer +'%'''+') or (firstName LIKE ''%'+ s_cbCustomer +'%'''+')';

13 (edited by AD1408 2017-02-02 20:58:40)

Re: ComboBox Search

Hi EHW,


Thanks a lot for the correction......
However, with that correction combo on inv form lets me enter only one letter at my end?


----------
Edit:

Sorry EHW. I was testing it with MVD 3.1x. It works fine with MVD 2.7
Need an update for search in combo script so that it works with MVD 3.1x
I have to use MVD 3.1x as I'm using some tGrid display filtering which only works on MVD 3.1x

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

AD1408
I have added new method for ComboBox, dbUpdate.
You should use it instead procedure UpdateDatabase(tablename)


dbUpdate will work more faster and solve bug in your project.


Please download 3.21beta:
https://www.dropbox.com/s/xf20ksdbdgj6w … b.zip?dl=0


Revised project:

Post's attachments

Attachment icon Search in ComboBox 4 (v3.21b and higher).zip 104.6 kb, 499 downloads since 2017-02-03 

Dmitry.

Re: ComboBox Search

Thank you sooooooo much Dmitry for the update and revised sample project.......................


Name or Surname search works fine now.


Taking it little further, what about if I want to use name and surname as a search term?
For instance:
1. If I type Amy and Ba in combo (space between Amy and Ba) it'd find;
Amy Baker
Amy Bailey
Amy Ball
Amy Bancroft
.....


2. If I type J and Ba in combo (space between J and Ba) it'd find;
Jon Baker
John Bailey
John Baker
James Ball
Jack Bancroft
Jack Baker
Jack Ball
.......

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

AD1408
Example:

Post's attachments

Attachment icon Search in ComboBox 5 (v3.21b and higher).zip 105.16 kb, 482 downloads since 2017-02-06 

Dmitry.

Re: ComboBox Search

Thx a lot Dmitry...........................


I've tested MVD v 3.21beta but it didn't work at my end.


For instance, on groups combo typing J and hitting space clears combo list then typing more after space doesn't make any difference.


You have applied to on form1 groups combo. I tried to apply same script with some edit to frmInvoice combo and it didn't accept any any input?

procedure frmInvoice_cbCustomer_OnKeyPress (Sender: string; var Key: Char);
var
    id: integer;
    ar: array of string;

begin

    if ord(Key)= 13 then
    begin
        id := frmInvoice.cbCustomer.dbItemID;
        frmInvoice.cbCustomer.DroppedDown:=False;
        frmInvoice.cbCustomer.dbItemID := id;
        Key := #0;
        Exit;
    end;

    if ord(Key)= 8 then // for backspace button
    begin
        s_cbCustomer := '';
        frmInvoice.cbCustomer.Text := '';
        frmInvoice.cbCustomer.dbUpdate; //UpdateDatabase('person');
        Key := #0;
        frmInvoice.cbCustomer.DroppedDown:=False;
        frmInvoice.cbCustomer.dbItemID := -1;
        Exit;
    end;

    s_cbCustomer := s_cbCustomer + Key;
    Key := #0;

    ar := SplitString(s_cbCustomer, ' ');
    if (Length(ar)=2) and (ar[1]<>'') then
    frmInvoice.cbCustomer.dbFilter := '(firstname LIKE '''+ ar[0] +'%'') AND (lastname LIKE '''+ar[1]+'%'')'
    else
        frmInvoice.cbCustomer.dbFilter := '(lastname LIKE ''%'+ s_cbCustomer +'%'''+')';

    frmInvoice.cbCustomer.dbUpdate;
    frmInvoice.cbCustomer.DroppedDown:=True;
    frmInvoice.cbCustomer.Text := s_cbGroups;
    frmInvoice.cbCustomer.SelStart := Length(s_cbCustomer);

end;



procedure frmInvoice_cbCustomer_OnCloseUp (Sender: string);
var
    id: integer;

begin

    id := frmInvoice.cbCustomer.dbItemID;
    frmInvoice.cbCustomer.dbFilter := '';
    frmInvoice.cbCustomer.dbUpdate; //UpdateDatabase('person');
    frmInvoice.cbCustomer.dbItemID := id;
    s_cbCustomer := '';
end; 
Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

AD1408
Please attach your project.

Dmitry.

Re: ComboBox Search

DriveSoft wrote:

AD1408
Please attach your project.


Hi Dmitry,


Please see the attached sample project.
I was trying to apply search in combo (invoice form) as described on my post #15

Post's attachments

Attachment icon Search in ComboBox 5b (v3.21b and higher).zip 105.84 kb, 457 downloads since 2017-02-07 

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Adam, when you copied Dimitry's code from Groups you missed replacing a variable in the following line:

frmInvoice.cbCustomer.Text := s_cbGroups;

It should be

frmInvoice.cbCustomer.Text := s_cbCustomer;

Re: ComboBox Search

Thx EHW,


You're right, there is an error on pasted script. That has been corrected in "Search in ComboBox 5b (v3.21b and higher).zip " sample project.


Still not possible to do a search with and.
For instance If I want to search in combo for A Ba to find all names starting with A and surnames starting with BA. combo stops (showing nothing) after pressing space bar and then typing anything else after pressing space bar doesn't make any difference.


Currently, it's doing searching  name OR surname fine. I was hoping to add to OR search AND search as well as described above and my post #15.

Adam
God... please help me become the person my dog thinks I am.

Re: ComboBox Search

Project fixed

Post's attachments

Attachment icon Search in ComboBox 5b (v3.21b and higher)_Fixed.zip 438.95 kb, 522 downloads since 2017-02-07 

Re: ComboBox Search

Great stuff EHW........
Thank you so much............................

Adam
God... please help me become the person my dog thinks I am.