Question

I have the following query to one of my database tables:

select count(*) as mycount
  from mytable
 where fieldone = :fieldone
   and fieldtwo = :fieldtwo

Parameters are correctly loaded into the query (both of type String).

When I run this query outside the app (for instance, through the dbexplore) and replace the parameters with the actual values, I get the correct result. But when running it in the app, I get a Field 'fieldtwo' not found error, right on the Query.Open call.

Why would the BDE not find this field, when it actually exist?

Update: The following query, executed right after the first one (the one that fails), works fine in the app:

select *
  from mytable
 where fieldone = :fieldone
 order by fieldone, fieldtwo
Was it helpful?

Solution

Clear the query content using Query1.SQL.Clear; statement before opening it.

Other reason can be you are opening other database which may not have the specified field. Be sure that both the DatabaseName's in your app and dbexplore are same

OTHER TIPS

The best guess is that you have populated the field list in the query, this overrides any concept of the underlying fields that are in the query and is a cause of countless confusion.

Right click on the query, pick the fields editor clear all the values that are there and then choose 'add all fields' that should cause the missing field to appear once the query is executed.

I think it should auto-populate the fields if there are no defined fields when the query is executed, so you may not need to choose 'add all fields' after clearing the fields.

Whenever we come across a problem like this we tend to remove the query from the form and create it dynamically at run time... It depends how ingrained into the form it is...

E.g. If you have a data aware control looking at "fieldtwo" which tries to fetch some data when the underlying data set gets updated then it'll trigger an error like this, but it's more obvious when you've written code such

SomeEdit.Text = Query.FieldByName("fieldtwo").AsString;

That way it falls over on the relevant line instead of the open (triggering a related event)

I used to face porblems with BDE when i have SQLExplorer open and the app accesses the DB at the same time (but i had errors like ), try closing the Explorer it may help, if not i would build the SQL as text without the Parameters and try if it works then (if its possible in your situation).

I don't use parameters, so I'm just grabbing at straws here. I still use the BDE regularly, but am no expert. I find I shy away from more complex expressions (which yours is not!) because of the little "surprises" like this that the BDE throws at you.

Perhaps adding parentheses:

 where (fieldone = :fieldone)
 and (fieldtwo = :fieldtwo)

Or, single or double quote signs (this probably will make it worse?)

 where (fieldon = ":fieldone")
 and   (fieldtwo = ":fieldtwo")

Or, to explore the problem, remove the "and fieldtwo = :fieldtwo" line and see if it runs.

Would it be possible for you to do your own parameter substitution with a StringReplace as in

 Query1.SQL.Text := StringReplace(Query1.SQL.Text, ":fieldone", "MyVarName",[rfReplaceAll   ]); 

If you are creating a ClienDataSet in memory by the Create DataSet method, you should check the TFieldDefs property, which must have a different field name or not created

I was having a weird but small problem, I'll post in case it will help someone in some day.

uRegPeople.pas

with frmEditPerson do
  begin
    PersonID := qryPerson.FieldByName(ID).AsInteger;
    ...
  end;

I had qryPerson both in frmRegPeople and in frmEditPerson, by using with I was referencing to frmEditPerson.qryPerson, however I wanted to reference to frmRegPeople.qryPerson. Then I need to change to the following code.

with frmEditPerson do
  begin
    PersonID := Self.qryPerson.FieldByName(ID).AsInteger;
    ...
  end;

// Explanation
// qryPerson --> frmEditPerson.qryPerson;
// Self.qryPerson --> frmRegPeople.qryPerson;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top