Page 1 of 1

Split form parameters for the query

Posted: Thu Jan 03, 2013 9:02 am
by Jennypi
Hi all,

In one of my report's form, users can choose several controls to run their query on:
ID: ...
or Product name: ...
or Comment: ...
or Category: ...
then, depending on their answer, I fill a variable ("query") to complete my SQL query:

Code: Select all

if (TextBoxControl1.Text!="")
{query=query+" and ID="+TextBoxControl1.Text;}
if (ComboBoxControl1.SelectedItem!=null)
{query=query+" and inventaire.[Product_name]='"+ComboBoxControl1.Control.Text+"'";}
if (TextBoxControl2.Text!="")
{query=query+" and Comments='"+TextBoxControl2.Text+"'";}
if (ComboBoxControl2.SelectedItem!=null)
{query=query+" and Category='"+ComboBoxControl2.Control.Text+"'";}
inventaire.Connect();
Now, they're asking me to be able to query multiple IDs.
My idea is that they would type, in the ID field, something like this: "10, 24, 58, 47".
I would like then to split this text field on comas to be able to retrieve the four IDs they want.

How can I do that? With a loop maybe? I don't know how to write a split instruction, same for the loop... :oops:

Thanks for your help!

Re: Split form parameters for the query

Posted: Thu Jan 03, 2013 1:38 pm
by HighAley
Hello.

You could add a verification if there any coma in the IDs string. And then add code like this:

Code: Select all

if (TextBoxControl1.Text != String.Empty)
    if (TextBoxControl1.Text.IndexOf(",") != -1)
        query += " and ID IN(" + TextBoxControl1.Text + ")";
    else
        query += " and ID=" + TextBoxControl1.Text;
Thank you.

Re: Split form parameters for the query

Posted: Thu Jan 03, 2013 4:16 pm
by Jennypi
Working perfectly, thanks a lot!!

Re: Split form parameters for the query

Posted: Fri Jan 04, 2013 8:40 am
by HighAley
Hello.

We are always glad to help you.
Let us know if you need any additional help.

Thank you.