ST3VO
asked on
Big Query Assistance Required
Hi Experts,
I know I have another opened question which has to do with this but I have a problem putting it all together.
This is want I'm trying to do:
I have 4 ComboBoxes
1. MAKE
2. MODEL
3. MAX PRICE
4. POSTCODE
By default I'm set the comboBox Text String to ANY
My biggest problem here is going to be getting a queries for all combinations.
The way I started doing it is by using an IF Statement
For example:
If ComboBox1.text = 'ANY' then
DoQuery
also
If ComboBox1.text<>'ANY' then
DoAnotherQuery
Etc...
And this goes for all the others.
Is this the best way to do this?
Also, with the last ComboBox (PostCode) I want to search the First 3 Digits of the postcode and not the full 6 Digits
I guess this is complex, but if you guys cannot help NO-ONE can....So I'm giving MAX Points possible for it.
Thanks
ST3VO
if
I know I have another opened question which has to do with this but I have a problem putting it all together.
This is want I'm trying to do:
I have 4 ComboBoxes
1. MAKE
2. MODEL
3. MAX PRICE
4. POSTCODE
By default I'm set the comboBox Text String to ANY
My biggest problem here is going to be getting a queries for all combinations.
The way I started doing it is by using an IF Statement
For example:
If ComboBox1.text = 'ANY' then
DoQuery
also
If ComboBox1.text<>'ANY' then
DoAnotherQuery
Etc...
And this goes for all the others.
Is this the best way to do this?
Also, with the last ComboBox (PostCode) I want to search the First 3 Digits of the postcode and not the full 6 Digits
I guess this is complex, but if you guys cannot help NO-ONE can....So I'm giving MAX Points possible for it.
Thanks
ST3VO
if
oops, small mistake : line 5-10 corrected
SQL.Add('Select * from motors');
if copy(AnsiUpperCase(ComboBo x1.text),1 ,3) <> 'ANY' then
begin
SQL.Add(' where Make = :p1');
Parameters.Parambyname('p1 ').value := ComboBox1.text;
end;
--sorry--
SQL.Add('Select * from motors');
if copy(AnsiUpperCase(ComboBo
begin
SQL.Add(' where Make = :p1');
Parameters.Parambyname('p1
end;
--sorry--
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
MerijnB:
Your code compiles but I get the error below when I run the query
Invalid SQL statement; expected 'DELETE','INSERT','PROCEDU RE','SELEC T', or 'UPDATE'
Any ideas???
Your code compiles but I get the error below when I run the query
Invalid SQL statement; expected 'DELETE','INSERT','PROCEDU
Any ideas???
what is the final result (query) you get?
ASKER
It doesn't go ahead and execute the query.
Messagebox:
Invalid SQL statement; expected 'DELETE','INSERT','PROCEDU RE','SELEC T', or 'UPDATE'
Comes out and doesn't continue.
Messagebox:
Invalid SQL statement; expected 'DELETE','INSERT','PROCEDU
Comes out and doesn't continue.
ASKER
Coould it be that after your code...I have this:
if not sCheckBox1.Checked then
begin
ADOQuery1.SQL.Add('UNION ALL SELECT TOP 200 * from motors where Make <> "' + (ComboBox1.Text) + '"');
end;
ADOQuery1.Active := true;
ADOQuery1.Open;
if ADOQuery1.EOF then
begin
sMessageDlg('Sorry No Matches Found! Please try another search!', mtInformation, [mbOK], 0);
Exit;
end;
if not sCheckBox1.Checked then
begin
ADOQuery1.SQL.Add('UNION ALL SELECT TOP 200 * from motors where Make <> "' + (ComboBox1.Text) + '"');
end;
ADOQuery1.Active := true;
ADOQuery1.Open;
if ADOQuery1.EOF then
begin
sMessageDlg('Sorry No Matches Found! Please try another search!', mtInformation, [mbOK], 0);
Exit;
end;
I mean the query (string) you sent to the DB
Pehaps it's worth to try my solution also.
ASKER
I just run the code you posted :o/
Query := 'select * from mytable' + WhereClause;
I'm I being a n00b again? :o(
Query := 'select * from mytable' + WhereClause;
I'm I being a n00b again? :o(
nope, but I can't see what's in the different comboboxes.
so what is in Query after you did:
Query := 'select * from mytable' + WhereClause;
so what is in Query after you did:
Query := 'select * from mytable' + WhereClause;
ASKER
Actually the query was:
Query := 'select * from motors' + WhereClause;
Query := 'select * from motors' + WhereClause;
post the contents of Query
ASKER
I think I don't understand what I needd to do here...
Could you post a dummy Query so I can see please?
Could you post a dummy Query so I can see please?
ASKER
Something like this???
ADOQuery1.SQL.Add('select * from motors where (Make) = "' + (sco.Text) + '"');
ADOQuery1.SQL.Add('select * from motors where (Make) = "' + (sco.Text) + '"');
ASKER
Sorry....I should have posted this here:
procedure TForm1.Button12Click(Sende r: TObject);
var WhereClause: string;
WhereClauseParts: TStringList;
i: integer;
Query: string;
begin
ADOQuery1.active := false;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
////////////////////////// ////////// ////////// ////////// /////
begin
WhereClauseParts := TStringList.Create();
// check make combobox
if sco.Text <> 'ANY' then
WhereClauseParts.Add(Forma t('(Make = %s)', [QuotedStr(sco.Text)]));
// check model combobox
if sModels.Text <> 'ANY' then
WhereClauseParts.Add(Forma t('(Model like "%%%s%%")', [sModels.Text]));
// check price combobox
if sPrice.Text <> 'ANY' then
WhereClauseParts.Add(Forma t('(Price <= %s)', [sPrice.Text]));
// check postcode combobox
if sPostCode.Text <> 'ANY' then
WhereClauseParts.Add(Forma t('(Postco de like "%s%%")', [Copy(sPostCode.Text, 1, 3)]));
// now combine all where clauses
if WhereClauseParts.Count > 0 then
begin
WhereClause := ' where';
for i := 0 to WhereClauseParts.Count - 1 do
begin
WhereClause := WhereClause + ' ' + WhereClauseParts[i];
if i < WhereClauseParts.Count - 1 then
WhereClause := WhereClause + ' AND';
end;
end;
WhereClauseParts.Free();
// now we have the where clause, make the rest of the query
Query:=('select * from motors' + WhereClause);
end;
////////////////////////// ////////// ////////// ////////// ////////
if not sCheckBox1.Checked then
begin
ADOQuery1.SQL.Add('UNION ALL SELECT TOP 200 * from motors where Make <> "' + (sco.Text) + '"');
end;
// ADOQuery1.Active := true;
// ADOQuery1.Open;
if ADOQuery1.EOF then
begin
sMessageDlg('Sorry No Matches Found! Please try another search!', mtInformation, [mbOK], 0);
Exit;
end;
end;
procedure TForm1.Button12Click(Sende
var WhereClause: string;
WhereClauseParts: TStringList;
i: integer;
Query: string;
begin
ADOQuery1.active := false;
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
//////////////////////////
begin
WhereClauseParts := TStringList.Create();
// check make combobox
if sco.Text <> 'ANY' then
WhereClauseParts.Add(Forma
// check model combobox
if sModels.Text <> 'ANY' then
WhereClauseParts.Add(Forma
// check price combobox
if sPrice.Text <> 'ANY' then
WhereClauseParts.Add(Forma
// check postcode combobox
if sPostCode.Text <> 'ANY' then
WhereClauseParts.Add(Forma
// now combine all where clauses
if WhereClauseParts.Count > 0 then
begin
WhereClause := ' where';
for i := 0 to WhereClauseParts.Count - 1 do
begin
WhereClause := WhereClause + ' ' + WhereClauseParts[i];
if i < WhereClauseParts.Count - 1 then
WhereClause := WhereClause + ' AND';
end;
end;
WhereClauseParts.Free();
// now we have the where clause, make the rest of the query
Query:=('select * from motors' + WhereClause);
end;
//////////////////////////
if not sCheckBox1.Checked then
begin
ADOQuery1.SQL.Add('UNION ALL SELECT TOP 200 * from motors where Make <> "' + (sco.Text) + '"');
end;
// ADOQuery1.Active := true;
// ADOQuery1.Open;
if ADOQuery1.EOF then
begin
sMessageDlg('Sorry No Matches Found! Please try another search!', mtInformation, [mbOK], 0);
Exit;
end;
end;
I don't see you use the contents of the variable Query anywhere.
ASKER
Query:=('select * from motors' + WhereClause);
Also Tried:
Query:=('select * from motors' + WhereClause + '(Make) = "' + (sco.Text) + '"');
But got no results back....Hmmm
Also Tried:
Query:=('select * from motors' + WhereClause + '(Make) = "' + (sco.Text) + '"');
But got no results back....Hmmm
the variable Query is only a string, it doesn't do anything by itself.
You'll need to execute the string on the database to get anything back from it
probably something like:
ADOQuery1.SQL.Add(Query);
You'll need to execute the string on the database to get anything back from it
probably something like:
ADOQuery1.SQL.Add(Query);
ASKER
Right!!!!
Got it now!!! :o) Sorry for being sooo Damn!
The only thing that I cannot get to work is the Price.
I get an error when it's not ANY
Error is: Data Type mismatch in ceteria expression...It is to do with the fact that the Price is actually a String?
Got it now!!! :o) Sorry for being sooo Damn!
The only thing that I cannot get to work is the Price.
I get an error when it's not ANY
Error is: Data Type mismatch in ceteria expression...It is to do with the fact that the Price is actually a String?
please post the contents of the field query (the sql query you actually sent to the DB) and the type definition of the field price in the database
ASKER
It's a CSV file it's reading the data from.
That's the main problem with Price :o/
That's the main problem with Price :o/
can you post some records from the CSV file?
ASKER
Sorted! I managed to change the data type to Integer!
Thanks a million for your help!!!!
Works perfect!!!
Thanks a million for your help!!!!
Works perfect!!!
One of possible solutions :
Open in new window