Very easy: code for numbering records

Hi all...

I have a table named buildings and another one named apartments. I need code to assign consecutive numbers to the apartments like this:

Building1:  initialnumber 100
Building2:  initialnumber 200
(These are values for each record in a field called initialnumber)

B1Apartment1:  number 100
B1Apt2:  number 101
B1Apt3:  number 102 ...

B2Apt1:  number 200
B2Apt2:  number 201
B2Apt3:  number 202 ...

My main form has a button from which I would launch this procedure (from its onclick event).

I'm a beginner, but this is a piece of cake for you guys!
Thanks for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eduardo, Of cours I can give you the code that you ask for, but if your tables are in a database, I would recommend you to do anotherway :

on the table Building, create a key that will be of type integer, and with the special clause autoincrement.

Autoincrement means that when you store a building if you do not fill the key column with a number, the building number will be added automatically.

you will then have to retrieve the building number with an appropriate select.

for the apartments, create 1 column where you will store the building number of your previously stored building, and an other column that will be the apartment number and will be in autoincrement too.

If you do this you have done the job except that you cannot speak about initial numbers.

other possibilities exist of course but they will not cost you 25 points

regards marc

There are many ways in which this can be done. Here is one for you to try. Hopefully this works for you.

I see that your example numbers the Records as 100,200,300 . . . .So if you want to assign Consecutive Numbers try this.

For the Buildings Table.. you should have the 2 columns

BuildingName  initialnumber
 Building1        100

Put 2 TQuery Component on the Form

TForm1.Button1Click(Sender : TObject);
 NoOfRec,i : Integer;
 Name : String;
 NumberToInsert : Integer;
 Query1.SQL.ADD('Select * from Buildings')
 NumberToInsert := 100;
 Name := query1.fieldbyname('BuildingName').asString
 while not Query1.eof do
    Name := query1.fieldbyname('BuildingName').asString
    Query2.SQL.ADD('Update Building set
       Building.initialnumber = :0 where
       Buiiding.Buildingname = :1')
    Query2.params[0].asInteger := NumberToInsert;
    Query2.params[1].asString := Name;
    NumberToInsert := NumberToInsert + 100;;

This should populate the Buildings Table with consective numbers starting from 100.  
Hope this does it for you ??

Kind Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eduardo1Author Commented:
Thanks Tazi for participating...
What I ended up doing was the following:

procedure (name of procedure btnClick)
  valor : smallint;
  esteedif : string;
  while not frmDepartamentos.Table1.Eof do
  esteedif := frmDepartamentos.Table1.FieldByName('DirEdif').Text;
  valor := frmEdificios.Table1.FieldByName('Folioini').Value;
  while esteedif = frmDepartamentos.Table1.FieldByName('DirEdif').Text do
  frmDepartamentos.Table1.FieldByName('Folio').Text := inttostr(valor);
  valor := valor + 1;
  if frmDepartamentos.Table1.Eof then break;
  frmEdificios.Table1.FieldByName('Folioini').Text := IntToStr(valor);

So, I solved the problem today, but I will accept your answer to learn from your code. Thanks, keep in touch!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.