Solved

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

Posted on 1997-10-15
2
620 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 79
Tviruailstringtree sort multi columns on header click 1 86
Delphi and Access based Enumeration 9 78
Way to create an iPhone app for my customers 8 47
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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