PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → How to fill combobox from database
How to fill combobox from database
Débuté par PETR_K, 27 mar. 2015 20:55 - 10 réponses
Membre enregistré
68 messages
Posté le 27 mars 2015 - 20:55
Hello,

I am trying to find the best way how to fill combobox with Employees names of certain Company.

I have 3 database files. ORDERS, COMPANIES, EMPLOYEES.

I have a window with quite typical layout - on the top is Table, below are fields, Table and fields are linked to ORDERS. When you click on a table row (each row is one order), fields are filled with appropriate data from ORDERS, COMPANIES, EMPLOYEES. I use linking from visual editor. Please see a picture of my window.





I have a problem with combobox with Employee name. I want to fill it only with Employees from selected Company.

For example: You select some order, this order contains Company called "Tiskap" and this company has 5 employees. But as you can see on my picture, combobox is filled up with ALL Employees of all companies. So question is how to fill combo with Employees only from "Tiskap".

Of course it must work when you click on another order with another Company, combobox should be filled with Employees from that another Company.

Combobox is now linked also via visual editor with multi-file linking.

I probably need something like "SELECT Employees.FirstName, Employees.Second FROM EMPLOYEES WHERE EMPLOYEES.KontaktID=COMPANIES.KontaktID"...


I hope I am clear enough and that somebody will help me.

Thank you

Regards

Petr
Membre enregistré
68 messages
Posté le 31 mars 2015 - 12:17
PETR_K wrote:

Hello,
please is here anybody who could point me in a right direction?

I have quite a lot of these combos and I would like to choose a good way how to filter content. At this moment I see the only solution to somehow programatically filter the content on change of row in a table.

I am still new to WinDev and I am not sure what is best/easiest way.

Kind regards

Petr
Posté le 31 mars 2015 - 14:09
Hi Petr,

unfortunately, there is no BEST way... Only different ways that you can
use, and mostly it's a question of choice...

You can :
1- fill the combo grammatically, based on a hfilter or a query on the file
2- link the combo to the file, and set a hfilter on it before running
the combo init code
3- link the combo to a query then set the query parameters before
running the combo init code...

In case 2 and 3, you can first (general init) set the link to the
file/query to nothing with the combo source property, then set your
filter or query parameter, then set the combo source back to the file or
query, to avoid filling the the combo initially for nothing

Mostly, it's your choice, as all of these methods are perfectly valid

Of course, case 1 gives you a much greater control, and you can add
"control" lines in your combo in that case (---Please select one---,
---add a new line---, -----Separator-----, etc...)

Best regards

--
Fabrice Harari
International WinDev, WebDev and WinDev mobile Consulting

NEW: WXReplication, your open source replication system is available on
my web site!!!
WXShowroom.com: Show your projects!
More information on http://www.fabriceharari.com


On 3/31/2015 4:17 AM, PETR_K wrote:
PETR_K wrote:
Hello,
please is here anybody who could point me in a right direction?

I have quite a lot of these combos and I would like to choose a good way
how to filter content. At this moment I see the only solution to somehow
programatically filter the content on change of row in a table.

I am still new to WinDev and I am not sure what is best/easiest way.

Kind regards

Petr
Membre enregistré
68 messages
Posté le 31 mars 2015 - 14:29
Thank you Fabrice,

I will examine these ways and I hope I will be satisfied.

WinDev is new for me and it is still difficult for me WHICH way to choose, there is also a lot of that "10 times faster features" which are very useful but sometimes you can not use them because you need a kind of more complex approach. I believe it will get better after some time of developing in WinDev.

Until now I was developing in C# without any addons.

Regards

Petr
Membre enregistré
68 messages
Posté le 02 avril 2015 - 14:53
Hi,

I did tests, and for now I will stick with option 2 and I will see how it works during next development. So thank you again.

I have one more question, I hope this is still a good place to ask.

I am not sure where the code with HFILTER should be placed. I thought I will place this code into combobox and that I will use some kind of combobox event that is triggered when value of my combobox has changed, (which happens when you move between records in the table with orders). But it looks like that such an event does not exist for combobox or I am blind.

For now I see the only usable event which is "Selecting a row of TABLE_Orders" but then I probably should place code somewhere else. Also, there is more combos similar to this one, so I probably should create global procedure and call it from each combo? Or should I do a code that will loop through each combo and set HFILTER for each combo? Or is here some better approach that I am overlooking?

I did not find any example that would help me...

I know that this is quite basic task but it looks that my transfer from c# to WinDev shakes my brain.

Regards

Petr
Posté le 02 avril 2015 - 15:18
Hi Petr,

You will put your hfilter in the code where the users changes the
condition... So it depends of your UI...

In a combo, there is a code area called "Selecting a Row of xxx" that is
fired when the user, well, select a row :-)

That may be what you were looking for...

Best regards

--
Fabrice Harari
International WinDev, WebDev and WinDev mobile Consulting

NEW: WXReplication, your open source replication system is available on
my web site!!!
WXShowroom.com: Show your projects!
More information on http://www.fabriceharari.com




On 4/2/2015 6:53 AM, PETR_K wrote:
Hi,

I did tests, and for now I will stick with option 2 and I will see how
it works during next development. So thank you again.

I have one more question, I hope this is still a good place to ask.

I am not sure where the code with HFILTER should be placed. I thought I
will place this code into combobox and that I will use some kind of
combobox event that is triggered when value of my combobox has changed,
(which happens when you move between records in the table with orders).
But it looks like that such an event does not exist for combobox or I am
blind.

For now I see the only usable event which is "Selecting a row of
TABLE_Orders" but then I probably should place code somewhere else.
Also, there is more combos similar to this one, so I probably should
create global procedure and call it from each combo? Or should I do a
code that will loop through each combo and set HFILTER for each combo?
Or is here some better approach that I am overlooking?

I did not find any example that would help me...

I know that this is quite basic task but it looks that my transfer from
c# to WinDev shakes my brain.

Regards

Petr
Membre enregistré
68 messages
Posté le 03 avril 2015 - 14:30
Hi Fabrice,
thank you for your answer.

Among the others I also tested this event "Selecting a row of ..." already before but it is triggered only when I select a row manually. But it is not fired when a value is changed "automatically", I mean when I switch between orders in TABLE. I have code "FileToScreen()" in TABLE under "Selecting a row of TABLE_Orders" and as all of my controls in Window are linked to the file ORDERS, they are automatically filled with correct values.

I am sure there must be some easy solution and that I do not have to somehow create my own events.

Regards

Petr
Posté le 03 avril 2015 - 16:16
Hi Petr

you are just making a common mistake... All these event are USERS event,
not program/code events...

So if you select a line by code (by doing a listselectplus, by example),
the event will NOT fire, that that is the normal/desired behavior (you
can very easily end up in infinite loops otherwise)

So if you WANT the event to fire, you ALSO do it by code with:
executeprocess(ComboName, EventName) (I think that event name in that
case is trtSelection, but you'll have to check)

Best regards

--
Fabrice Harari
International WinDev, WebDev and WinDev mobile Consulting

NEW: WXReplication, your open source replication system is available on
my web site!!!
WXShowroom.com: Show your projects!
More information on http://www.fabriceharari.com


On 4/3/2015 6:30 AM, PETR_K wrote:
Hi Fabrice,
thank you for your answer.

Among the others I also tested this event "Selecting a row of ..."
already before but it is triggered only when I select a row manually.
But it is not fired when a value is changed "automatically", I mean when
I switch between orders in TABLE. I have code "FileToScreen()" in TABLE
under "Selecting a row of TABLE_Orders" and as all of my controls in
Window are linked to the file ORDERS, they are automatically filled with
correct values.

I am sure there must be some easy solution and that I do not have to
somehow create my own events.

Regards

Petr
Membre enregistré
68 messages
Posté le 03 avril 2015 - 16:22
For now I will try to live with this code placed under TABLE event "Selecting a row of TABLE_Orders".
HFilter(FirmyKontakty,FirmaID,Firmy.FirmaID)
ListDisplay(COMBO_FirmyKontakty,taReExecuteQuery)


I am just not sure yet what is the best way, how to automatize it for other windows where are more comboboxes.

And I still wonder if there is that combobox event that triggers on value change, as I mentioned previously.

Regards

Petr
Membre enregistré
68 messages
Posté le 07 avril 2015 - 10:26
Hi Fabrice,

I am sorry, I probably explained myself wrong. I meant selecting code by user not by code.

When USER select different order in TABLE, it triggers "Selecting a row of TABLE_Orders" and there is a command "FileToScreen()", this command fills my combobox with correct value but combobox event "Selecting a row of ..." is not triggered (I have here command Info("Test.") to be sure), this event is triggered only when USER select some value in combobox.

And thank you for your advice concerning executeprocess.

Regards

Petr

Fabrice Harari wrote:
Hi Petr

you are just making a common mistake... All these event are USERS event,
not program/code events...

So if you select a line by code (by doing a listselectplus, by example),
the event will NOT fire, that that is the normal/desired behavior (you
can very easily end up in infinite loops otherwise)

So if you WANT the event to fire, you ALSO do it by code with:
executeprocess(ComboName, EventName) (I think that event name in that
case is trtSelection, but you'll have to check)

Best regards

--
Fabrice Harari
International WinDev, WebDev and WinDev mobile Consulting

NEW: WXReplication, your open source replication system is available on
my web site!!!
WXShowroom.com: Show your projects!
More information on http://www.fabriceharari.com


On 4/3/2015 6:30 AM, PETR_K wrote:
Hi Fabrice,
thank you for your answer.

Among the others I also tested this event "Selecting a row of ..."
already before but it is triggered only when I select a row manually.
But it is not fired when a value is changed "automatically", I mean when
I switch between orders in TABLE. I have code "FileToScreen()" in TABLE
under "Selecting a row of TABLE_Orders" and as all of my controls in
Window are linked to the file ORDERS, they are automatically filled with
correct values.

I am sure there must be some easy solution and that I do not have to
somehow create my own events.

Regards

Petr
Membre enregistré
68 messages
Posté le 10 avril 2015 - 22:17
So, final solution with help of Fabrice is like this. There was a lot of small but important details to tune. Also, one important thing is that combobox displaying table has quite limited set of properties in compare with standard combobox.

This code is placed in TABLE with order's records, under "Selecting a row of TABLE_Orders". For selection of current record in combo (with table) it was neccessary to use TableSeek + ListSelectPlus as WinDev does not do that automatically.

HDeactivateFilter(FirmyKontakty)
HFilter(FirmyKontakty,FirmaID,Objednavky.FirmaID)

ListDisplay(SC_SuperControl1.COMBO_FirmyKontakty,taInit)

FileToScreen()

//For the multi-column combo must be used TableSeek and then ListSelectPlus
nIndex is int=TableSeek(SC_SuperControl1.COMBO_FirmyKontakty.KontaktID,Objednavky.KontaktID)
IF nIndex>0 THEN
ListSelectPlus(SC_SuperControl1.COMBO_FirmyKontakty,nIndex)
END


To select current record in combobox it must be placed this code
IF TableSelect(TABLE_Objednavky)>0 THEN
ExecuteProcess(TABLE_Objednavky,trtSelection)
END

into parent window under "End of initialization of..."