Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 134

# A better solution / algoritm

Does anyone hav a better solution for this code! It's reading data from a text file and inserting it to a table.

b:=0;
if OpenDialog1.Execute then
begin
AssignFile(F1, OpenDialog1.Filename);
Reset(F1);
while not eof(f1) do
begin

a:=0;
if b=0 then
begin
while a<50 do
begin
a:=a+1;
end;
b:=1;
end;

tableProdukter.Edit;
tableProdukter.Insert;
a:=0;
while a<2 do
begin
a:=a+1;
end;
a:=0;
while a<7 do
begin
a:=a+1;
tableProdukterProduktnummer.Text:=tableProdukterProduktnummer.Text+ch;
end;
a:=0;
while a<14 do
begin
a:=a+1;
end;
a:=0;
while a<35 do
begin
a:=a+1;
if Ch=' ' then tableProdukterProduktnamn.Text:=tableProdukterProduktnamn.Text+Chr(160)
else
tableProdukterProduktnamn.Text:=tableProdukterProduktnamn.Text+ch;
end;
a:=0;
while a<35 do
begin
a:=a+1;
if Ch=' ' then tableProdukterTillgsuppgiter.Text:=tableProdukterTillgsuppgiter.Text+Chr(160)
else
tableProdukterTillgsuppgiter.Text:=tableProdukterTillgsuppgiter.Text+ch;
end;
a:=0;
while a<141 do
begin
a:=a+1;
end;
end;
CloseFile(F1);
end;

EXAMPLE OF THE TEXTFILE

OBYXXXXXXXXXXX      L01
OSE003710712079     L01
RL0101104  2SWE20020521SG-TYTON TRYCKRÖR                  DN  80                             SG-TYTON            AA002  10105 M  00150000006400000000006000000600000000000000000000000000000000000   00V                    M  000010000
RL0101105  2SWE20020521SG-TYTON TRYCKRÖR                  DN  90                             SG-TYTON            AA002  10105 M  00150000006400000000006000000600000000000000000000000000000000000   00V                    M  000010000

0
tscooter82
• 4
• 3
• 3
• +3
1 Solution

Commented:
Hi,

just some thoughts:
You can use a TMemo (maybe invisible) to read the file (LoadFromFile method). Once the file is loaded you can use TMemo.Lines.Count (= number of lines) for you loop. Access lines in Memo via Memo1.Lines[index]. To extract the desired substrings use the Copy() function (-> Delphi help).

Regards und Tschüß
J
0

Commented:
WERY EASY

FIRST  PUT ONE MEMO COMPONENT PIT BOUTH SCROLLBARS TO TRUE

ON THE FORM AND DO THIS (MEMO COMPONENT MEY BE  INVISIBLE)

VAR S:STRING;
I:INTEGER;

IF OPENDIALOG.EXECUTE THEN
BEGIN
FOR I:=0 TO MEMO1.LINES.COUNT-1 DO
BEGIN
TABLE1.APPEND;
S:=MEMO1.LINES[I];
TABLE1.......TEXT:=COPY(S,X,Y)
END;

...... IS THE NAME OF FIELD
{WHERE X IS A NUMER OF START CARACTER AND Y IS A NUMBER OF CARACTER TO LOAD}

0

Commented:
Darn, no Umlaute: Tschüß -> Tschuess
0

Commented:
why use TMemo?? TStringList will be enough.
ziolko.
0

Commented:
if you use the bde,
then you can use tbatchmove

->just create a schema file, where the fields are defined

see bde-online-help
section
Binding external schema to text tables

meikl ;-)
0

Commented:
well, ILE did the coding so points belong to him ;-)
Hey, what about releasing caps lock!

Cheers
0

Commented:
hmm, i see,
you have different records in one file,
then tbatchmove cannot work with

but if you have control of the textfile-output,
then place a record-identifier in front of each
line

meikl ;-)
0

Commented:
implementation

type
TProdRec = record
res1: array [0..1] of char; // unused
pno: array [0..6] of char; // product number
res2: array [0..13] of char; // unused
pna: array [0..34] of char; // product name
ptl: array [0..34] of char;  // product something :)
res3: array [0..140] of char; // including CRLF
end;

procedure TForm1.Button1Click(Sender: TObject);
var
fr: TProdRec;
fs: TFileStream;
begin
if OpenDialog1.Execute then begin
try
while fs.Position < fs.Size do begin
tableProdukter.Append;
tableProdukterProduktnummer.Text := fr.pno;
tableProdukterProduktnamn.Text :=
StringReplace(fr.pna, ' ', Chr(160), [rfReplaceAll]);
tableProdukterTillgsuppgiter.Text :=
StringReplace(fr.ptl, ' ', Chr(160), [rfReplaceAll]);
tableProdukter.Post;
end;
finally
fs.free;
end;
end;
end;

wbr, mo.
0

Commented:
in line where reading goes on must be:

mo.
0

Author Commented:
Comment from mocarts!!

Why dosnt it read in scandinavian chars? ÅÄÖ
0

Commented:
code do not read or data not displayed correctly?

what type of database you use? (paradox, oracle etc?)
is your BDE alias configured for correct Language?
if you use paradox you must ensure that your tables have correct code page.
mo.
0

Author Commented:
0

Commented:
can you tell me in what exactly place you don't see correct characters?
in what code page is that data file?

Configuration -> Drivers -> Native -> Paradox option Language Driver
for Windows scandinavian character sets there is "Pdox ANSI nordan40" (Norvegian/Danish) and "Pdox ANSI SwedFin" (Swedish/Finnish)

also check font charset you use in grid (if any) when display data.

wbr, mo.
0

Author Commented:
I checked the BDE admin and it was set to SwedFin.
I also checked the charset and it was set to Default_Charset.

oooh noo stupid me, the fault was in the text file!!!
0

## Featured Post

• 4
• 3
• 3
• +3
Tackle projects and never again get stuck behind a technical roadblock.