Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now