• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

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.
0
Eduardo1
Asked:
Eduardo1
1 Solution
 
mhervaisCommented:
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
0
 
TAZICommented:
Hi...

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);
var
 NoOfRec,i : Integer;
 Name : String;
 NumberToInsert : Integer;
begin
 Query1.Close;
 Query1.SQL.Clear;
 Query1.SQL.ADD('Select * from Buildings')
 Query1.Open;
 Query1.First;
 NumberToInsert := 100;
 Name := query1.fieldbyname('BuildingName').asString
 while not Query1.eof do
   begin
    Name := query1.fieldbyname('BuildingName').asString
    Query2.Close;
    Query2.SQL.Clear;
    Query2.SQL.ADD('Update Building set
       Building.initialnumber = :0 where
       Buiiding.Buildingname = :1')
    Query2.params[0].asInteger := NumberToInsert;
    Query2.params[1].asString := Name;
    Query2.ExecSQL;
    NumberToInsert := NumberToInsert + 100;
    Query1.next;
   end;
 end;

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

Kind Regards
Tazi
0
 
Eduardo1Author Commented:
Thanks Tazi for participating...
What I ended up doing was the following:

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

So, I solved the problem today, but I will accept your answer to learn from your code. Thanks, keep in touch!
   
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now