Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Table['Field'] vs Table.FieldByName('Field')

Posted on 1997-10-15
2
619 Views
Last Modified: 2010-04-03
What is the difference between

Table['Field']
AND
Table.FieldByName('Field')

I was under the impression that the two methods both returned the same and that the first was quick-to-type
version of the second.

However in my program

 str := folders.fieldByName('name').AsString;

Works fine but
     
str := folders.['name'].AsString;

Genrates an exception.

Does anybody know why and how to use Table['field'] safely?
I cannot find it in the on-line help.
0
Comment
Question by:essbb
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1347614
Normally the 2 methods are interchangable.  However, if the field being returned is empty (i.e. null) then you cannot assign it to a string, as in "s := MyTable['MyField']".  You get an exception because you are trying to assign a null variant to a string type.

I.e. using the "MyTable['MyField']" method returns a variant, which can be null.  But, using the "MyTable.FieldByName('MyField').AsString" function will always return a string.  If the field is null, then the AsString function will return an empty string.

You could do the following, but you will end up typing more code anyway:
if (VarIsNull(MyTable['MyField'])) then
    s := ''
else
    s := MyTable['MyField'];

Normally, I only use the "MyTable['MyField']" method when assigning a value to the field.  When I read a value FROM the field, I use the AsString or AsInteger functions if there's a possibility of the return value being null.

Hope this is what you were looking for.
JB
0
 

Author Comment

by:essbb
ID: 1347615
JimBob,

Yes That seems to be the problem! I shouldn't be getting any Null
fields with my DB setup but it appears that I am. At least I know what bug(in my program) I am looking for now!

Thanks for your help
-Brett
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 166
Delphi XE10 Round Image 2 154
Performance of SQL statement 37 125
FMX TEdit KeyUp handler detecting  "enter" key 4 12
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question