I have modified the following example block of code so a user can specify what
city is searched for though the variable "searchCity." My question is how can I replace
some of the other things in the SQL statement with other parameters? For example, I have included the
lines inside the ****'s below in an attempt to replace "State" in the WHERE clause
with the parameter "FieldToSearch", so the user could choose another field other than
State to search for a city in, such as "Country." My attempts below yeild blank columns.
I've also tried using: stmtParams.ParamByName('FieldToSearch').AsString := searchField;
in place of: stmtParams[1].AsString := searchField;
I'm not sure what I'm doing wrong, but I'm thinking I've got to be close. How can I get this to work?
Any help would be greatly appreciated.
procedure TDataForm.SELECT_WithParamsButtonClick(Sender: TObject);
var
SQLstmt: String;
stmtParams: TParams;
Cursor: hDBICur;
begin
searchCity := Edit1.Text; //Value entered by user
searchField := Edit5.Text; //Value entered by user
stmtParams := TParams.Create;
try
Database1.Connected := True;
stmtParams.CreateParam(ftString, 'CityParam', ptInput);
stmtParams[0].AsString := searchCity;
//**********
stmtParams.CreateParam(ftString, 'FieldToSearch', ptInput);
stmtParams[1].AsString := searchField;
//**********
SQLstmt := 'SELECT Company, City '+
'FROM "Customer.db" ' +
// 'WHERE (State = :CityParam) ' + //This is the original line
'WHERE FieldToSearch = :CityParam) ' + //This is what I've been trying.
'ORDER BY City, Company';
Database1.Execute(SQLstmt, stmtParams, False, @Cursor);
Table1.Close;
TDBDataSet(Table1).Handle := Cursor;
finally
stmtParams.Free;
end;
end;
city is searched for though the variable "searchCity." My question is how can I replace
some of the other things in the SQL statement with other parameters? For example, I have included the
lines inside the ****'s below in an attempt to replace "State" in the WHERE clause
with the parameter "FieldToSearch", so the user could choose another field other than
State to search for a city in, such as "Country." My attempts below yeild blank columns.
I've also tried using: stmtParams.ParamByName('FieldToSearch').AsString := searchField;
in place of: stmtParams[1].AsString := searchField;
I'm not sure what I'm doing wrong, but I'm thinking I've got to be close. How can I get this to work?
Any help would be greatly appreciated.
procedure TDataForm.SELECT_WithParamsButtonClick(Sender: TObject);
var
SQLstmt: String;
stmtParams: TParams;
Cursor: hDBICur;
begin
searchCity := Edit1.Text; //Value entered by user
searchField := Edit5.Text; //Value entered by user
stmtParams := TParams.Create;
try
Database1.Connected := True;
stmtParams.CreateParam(ftString, 'CityParam', ptInput);
stmtParams[0].AsString := searchCity;
//**********
stmtParams.CreateParam(ftString, 'FieldToSearch', ptInput);
stmtParams[1].AsString := searchField;
//**********
SQLstmt := 'SELECT Company, City '+
'FROM "Customer.db" ' +
// 'WHERE (State = :CityParam) ' + //This is the original line
'WHERE FieldToSearch = :CityParam) ' + //This is what I've been trying.
'ORDER BY City, Company';
Database1.Execute(SQLstmt, stmtParams, False, @Cursor);
Table1.Close;
TDBDataSet(Table1).Handle := Cursor;
finally
stmtParams.Free;
end;
end;