Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delphi auto increment

141 views
Skip to first unread message

Jim Wharton

unread,
Feb 25, 2002, 12:11:54 AM2/25/02
to
When I create a new record via the dbnav, is there a way to have it check
the last record in the table, add 1 and insert that back to the database. In
my mind it seems plausible.(from an Object Pascal POV) I have tried many of
the auto increment triggers or procedures from the net, and they don't seem
to work until after a record is inserted back into a table and the
transaction is closed.

What I want to do is this: (Currently using Client DataSets) User is paging
through records, user decides to add new customer ID to customer table, user
hits new button on DBNav bar, Grid clears (ready to accept info) User
doesn't need to manually enter customer ID interbase or Delphi will do it
auto-magically.

Any suggestions?

Sorry for so many similar question, I am coming from a MySQL/Postgresql
background. As you may know, auto-increments are not a big issue there. (not
meant to be a troll)


Vincent Bergeron

unread,
Feb 25, 2002, 12:41:12 AM2/25/02
to
Hi!

I would suggest you do to that:

Method #1
1- Create a generator in IB.
2- Create a storec proc that will return the next value of the generator.
3- On the OnNewRecord event (I don't know if ClientDataSet have this event.
If not, find an event that's similar), do a query will return the stored
proc value.
4- Put that returned value into the field.

Method #2
1- On the OnNewRecord event, do this query: Select Max(ID) as MAX_ID from
table.
2- Then, increment the number;
iMax := sql.FieldByName('MAX_ID').asInteger+1;
3- Put the iMax value into the field.

HTH

VB


Holger Klemt

unread,
Feb 25, 2002, 3:50:26 AM2/25/02
to
i would recommend, that you have to forget the second way.
It takes a lot of time on getting the result, because no index
is used with max() and it is not sure, that you will be the only
one in the network, who gets the last result.

--
IBExpert - The most Expert for InterBase --- http://www.ibexpert.com
HK Software - Holger Klemt - Huntestrasse 15 - D-26135 Oldenburg
Telefon Telefax +49 700 IBEXPERT (42397378) www.h-k.de
Schulungen - Projektunterstützung - Delphi - InterBase - AS/400
"Vincent Bergeron" <in...@vincentbergeron.com> schrieb im Newsbeitrag
news:3c79cd59$1_1@dnews...

J Hall

unread,
Feb 25, 2002, 3:48:02 AM2/25/02
to
In article <3c79cd59$1_1@dnews>, in...@vincentbergeron.com wrote...

Hi,

> Method #1
> 1- Create a generator in IB.

This is multi-user safe.

> Method #2
> 1- On the OnNewRecord event, do this query: Select Max(ID) as MAX_ID from
> table.

This is NOT multi-user safe.

J

Craig Stuntz

unread,
Feb 25, 2002, 11:52:52 AM2/25/02
to
In article <3c79fae2$1_2@dnews>, hkl...@h-k.de says...

> It takes a lot of time on getting the result, because no index
> is used with max()

MAX will use a DESCENDING index, if available.

> and it is not sure, that you will be the only
> one in the network, who gets the last result.

Quite correct. This is the real problem.

-Craig

--
Craig Stuntz (TeamB) · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase WebLog: http://delphi.weblogs.com
InterBase PLANalyzer (Free IB optimization tool):
http://delphi.weblogs.com/IBPLANalyzer

Jim Wharton

unread,
Feb 25, 2002, 5:24:26 PM2/25/02
to
Created a new generator that supposedly autoincrements. When I try to
inssert a new record, it just tells me I can't leave that fiel null. I want
the database to place that value there.

Jim

"Vincent Bergeron" <in...@vincentbergeron.com> wrote in message
news:3c79cd59$1_1@dnews...

Jeff Overcash (TeamB)

unread,
Feb 25, 2002, 5:26:03 PM2/25/02
to

Jim Wharton wrote:
>
> Created a new generator that supposedly autoincrements. When I try to
> inssert a new record, it just tells me I can't leave that fiel null. I want
> the database to place that value there.

You need to set the TField's Required to false for that field. The error
message is a client side check that stops even bothering the DB if the required
fields are not set on the client. If you plan on settign them on the server you
need to tell the client to not check those fields.

>
> Jim
>
--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
This sad little lizard told me that he was a brontosaurus on his mother's
side. I did not laugh; people who boast of ancestry often have little else
to sustain them. Humoring them costs nothing and ads to happiness in
a world in which happiness is in short supply. (RAH)

0 new messages