Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1997-10-15
2
Medium Priority
?
628 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

876 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