1

This is the error I get when I run the code below. I'm not too sure what's wrong any help is appreciated

procedure TFSearchMember.btnSearchClick(Sender: TObject);
var buttonSelected:integer;
WhereTextSelection,WhereFieldSelection:string;
begin

WhereFieldSelection:=cboWhereField.Text;
WhereTextSelection:=txtWhere.Text;

adoQuery1.Parameters[0].Value:=WhereTextSelection;
adoQuery1.Open;
ADOQuery1.Requery;

txtWhere.Text:='';
cboWhereField.Text:='';


  if ADOQuery1.RecordCount=0 then
  begin
    buttonSelected:=MessageDlg('Record not found.', mtError, [mbOK],0);
    if buttonSelected = mrOK then
    Exit;
  end;

I also have this in the ADOQuery SQL property

5
  • Please include the text of your error message, and the text of your query within the question. Commented Dec 11, 2015 at 12:53
  • Is there a field WhereFieldSelection in table tblMembers in the SQL database? The error message might be hiding the true problem. Commented Dec 11, 2015 at 13:11
  • Why are you calling adoQuery1.Open; and then immediately calling adoQuery1.Requery;? Use some common sense: if you've just opened the query, why in the world would you think you need to requery it again? Let's see. I'll turn the doorknob on my front door, push the door open, and then immediately push it again without doing anything in between. Does that make any kind of sense to you? Commented Dec 11, 2015 at 13:25
  • @KenWhite Yea i understand thanks. Commented Dec 11, 2015 at 14:01
  • @penarthur66 no it is the result of what is in-putted into the combobox. Commented Dec 11, 2015 at 14:01

2 Answers 2

1

Although your question doesn't say so, I'd suspect that you're trying to allow the user to both select the field to use in the WHERE (from cboWhereField) and the value (from txtWhere). If that's the case, you have to update the SQL first before you can assign the parameter, as both are changing.

Something like this should work (in a basic sense):

procedure TFSearchMember.btnSearchClick(Sender: TObject);
const
  SQLBase = 'SELECT * FROM tblMembers WHERE %s = :SearchValue';
begin
  if AdoQuery1.Active then
    AdoQuery1.Close;
  AdoQuery1.SQL.Text := Format(SQLBase, [cboWhereField.Text]);
  AdoQuery1.Parameters.ParamByName('SearchValue').Value := txtWhere.Text;
  AdoQuery1.Open;

  txtWhere.Text:='';
  cboWhereField.Text:='';

  if not ADOQuery1.IsEmpty then
  begin
    // No need for if test here, as you're only offering one value.
    // MessageDlg can't return anything other than mrOK
    MessageDlg('Record not found.', mtError, [mbOk], 0);
    Exit;
  end;

  // Your other code
end;
Sign up to request clarification or add additional context in comments.

Comments

0

I assume that not all code is shown, because it seems that you try to create dynamic SQL where the "WhereFieldSelection" can be changed to whatever is shown in the combobox. If you change the SQL.Text property (e.g. to use "Forename") of the ADOQuery AFTER the parameter value is set, the value of the parameter is lost, and you will have to reset it. Also, the Requery, is not necessary, as Ken White mentioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.