Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with calculated field 3

Status
Not open for further replies.

xxman

Technical User
Jun 1, 2004
80
US
Hi All
I did my first attempt at a calculated field in a dbgrid using a Microsoft Access database. Here is the code:AdoTable1.FieldValues['CalAdd']:= AdoTable1.FieldValues['Part Name']+ AdoTable1.FieldValues['Part Number'];
end;
The results were not as I expected when I added the two values..ie 1 + 2= 12 in the CalAdd field. I also tried the
formula with a multiply operator just as a test and the result was 2. The multiplied result was correct but the addition looked like it was adding a text string together.
In the access table I tried to set the field property to number type but when I ran the program I got a type error. So I set the access property to Text. I assume this is the problem. Not certain how to correct this.
 
I don't use ADO or Access so I am unable to test this. However I always use the AsInteger property where it is appropriate. Try something like:
Code:
AdoTable1.FieldByName('CalAdd').AsInteger := AdoTable1.FieldByName('Part Name').AsInteger +
AdoTable1.FieldByName('Part Number').AsInteger;
I am puzzled by your field names. Do you really want to add a Part Name to a Part Number? I suspect that Access didn't like you changing the field property to number type because there is non numeric data there.

You will hit problems if you have alphabetic data in fields that you want to perform arithmetic on. I suggest you correct that first.

Andrew
Hampshire, UK
 
and plus you can just do that in your query:

Code:
SELECT FirstName, LastName, FirstName & " " & LastName As FullName FROM Employees

will return:
[tt]
FirstName LastName FullName
Joe Blow Joe Blow
Jane Doe Jane Doe
[/tt]

Leslie

Have you met Hardy Heron?
 
Hi
I went into the access database and changed the field type to a integer. I rewrote the code as Towerbase supplied. When I run the program I get Type mismatch for field Part Name, expecting: WideString actual: Integer. I thought changing the
type of data in the database table would make a difference but
that seems not to be the case. It seems I`m lost.
PS. The field names were just some filler names placed to get started learning. After they were in place I then decided to try a calculated field on numerical data. I`m certain that`s not good naming practice.
Thanks and Regards
 
Can you show us the actual code that causes the error? Please use TGML tags so that code comes out like this:
Code:
AdoTable1.FieldByName('CalAdd').AsInteger := AdoTable1.FieldByName('Part Name').AsInteger +
AdoTable1.FieldByName('Part Number').AsInteger;
If you don't know TGML click on the Process TGML link at the bottom of this page.

Andrew
Hampshire, UK
 
Changing Field types can caused a new problem with your application. Delphi does not know it has been changed. You must "tell" it.

To do this, double-click your your AdoTable1 icon on the form where it was created. This will open the 'Fields Editor'.

Right-click within the 'Fields Editor' and select 'Select all'.

Right-click again and select 'Delete'.

Right-click again and select 'Add all Fields'.

Your Delphi app is now in sync with the new data field types.

HTH

Roo
Delphi Rules!
 
Hi roo0047
That tip allowed me to reset the data. In the fields editor
I set the CalAll field to integer. Then ran the program and
got a new and exciting error to deal with.
EvarientInvaildOpError: Invalid varient operation

AdoTable1.FieldValues['CalAdd'].AsInteger:= AdoTable1.FieldValues['Part Name'].AsInteger* AdoTable1.FieldValues['Part Number'].AsInteger;


.
 
I don't think you can use
Code:
AdoTable1.FieldValues['CalAdd'].AsInteger
It's either
Code:
AdoTable1.[b]FieldValues['CalAdd'][/b]
or
Code:
AdoTable1.[b]FieldByName('CalAdd').AsInteger[/b]





Andrew
Hampshire, UK
 
Hi Towerbase...this worked nicely. With all the mistakes I`ve made so far. I think it might be wise to do more reading of a manual then coding. Thanks.

AdoTable1.FieldByName('CalAdd').AsInteger:= AdoTable1.FieldByName('Part Name').AsInteger + AdoTable1.FieldByName('Part Number').AsInteger;
 
if you have added the fields to the fields editor then you can access them by thier name (you cant have spaces in the names).

AdoTable1CalAdd.AsInteger := AdoTable1PartName.AsInteger + AdoTable1PartNumber.AsInteger;

by doing it this it doesnt have to perform multiple string functions to find the field by its name, depending on the size of the table this can dramatically increase table performance.

Aaron
 
Hi Aaron
I`ve seen this in some sample code before. Part_Name, is this proper syntax for a field name.

Thnaks and Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top