1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Delphi: Use of Text Files in Procedures

Discussion in 'Programming' started by robsharpe, Mar 30, 2005.

  1. #1
    procedure BoldIt(s:string;PF:textfile);
    begin
    Printer.Canvas.Font.Style:=[fsBold,fsItalic];
    Write(PF,s);
    end;

    [Error] Unit1.pas(655): File type not allowed here

    Does anyone know why this causes an error? I don't understand why I can't pass the text file through the procedure as I would any other variable?

    Rob
     
    robsharpe, Mar 30, 2005 IP
  2. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi Rob,

    Can you give me an idea of what you are trying to accomplish? Here's how I read your code:

    s = string is passed in, PF is delcared as TextFile
    Printer.Canvas.Font.Style has the value [fsBold,fsItalic] to it
    You write s that is passed in to PF.

    Has PF been assigned? Are you trying to write the Printer value to the file assigned to PF?

    I can probably help you through this, as I'm an intermediate delphi programmer. Let me know and I'll try my best.
     
    GTech, Mar 30, 2005 IP
  3. robsharpe

    robsharpe Peon

    Messages:
    258
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here is the rest of my code which uses this... you may have guessed I'm not an expert...

    procedure BoldIt(s:string;PF:textfile);
    begin
    Printer.Canvas.Font.Style:=[fsBold,fsItalic];
    Write(PF,s);
    end;

    procedure TForm1.PrintNoPayClick(Sender: TObject);
    var
    PrintFile:TextFile;
    oid:integer;
    dte:Tdate;
    tp:currency;
    ap:currency;
    owd:currency;
    cid:integer;
    nme:string;
    sch:string;
    email:string;
    begin
    pd.Execute;
    AssignPrn(PrintFile);
    Printer.Canvas.Font.Name:='Times New Roman';
    Printer.Canvas.Font.Size:=14;
    Rewrite (PrintFile);
    Printer.Canvas.Font.Style:=[];
    Writeln(PrintFile,' ');
    Writeln(PrintFile,' ');
    Printer.Canvas.Font.Style:=[fsBold, fsItalic, fsUnderline];
    Writeln(PrintFile,'Non Payers Report - Unpaid Orders On Or Before: '+(NoPayDate.Text));
    Printer.Canvas.Font.Style:=[];
    Writeln(PrintFile,' ');
    Writeln(PrintFile,' ');

    with DM.Order do begin
    first;
    while not eof do begin
    if ((FieldByName('Date').AsDateTime)<=strtodate(NoPayDate.Text)) and (FieldByName('Paid').AsBoolean=false) then begin
    Printer.Canvas.Font.Style:=[fsBold, fsItalic];
    oid:=FieldByName('OrderID').AsInteger;
    dte:=FieldByName('Date').AsDateTime;
    tp:=FieldByName('TotalPrice').AsCurrency;
    ap:=FieldByName('AmountPaid').AsCurrency;
    owd:=(tp-ap);
    cid:=FieldByName('CustomerID').AsInteger;

    with DM.Customer do begin
    first;
    while not eof do begin
    if (FieldByName('CustomerID').AsInteger=cid) then begin
    sch:=FieldByName('SchoolName').AsString;
    nme:=FieldByName('ContactName').AsString;
    email:=FieldByName('Email').AsString;
    end;
    next;
    end;
    end;

    BoldIt('Customer ID: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,cid);

    BoldIt('Customer Name: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,nme);

    BoldIt('School Name: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,sch);

    BoldIt('Email Address: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,email);

    BoldIt(' Order ID: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,oid);

    BoldIt(' Date Of Order: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,datetostr(dte));

    BoldIt(' Total Price: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,'£'+currtostr(tp));

    BoldIt(' Amount Already Paid: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,'£'+currtostr(ap));

    BoldIt(' Balance Outstanding: ',PrintFile);
    Printer.Canvas.Font.Style:=[fsItalic];
    Writeln(PrintFile,'£'+currtostr(owd));

    Writeln(PrintFile,' ');
    Writeln(PrintFile,' ');

    end;
    next;
    end;
    end;
    CloseFile(PrintFile); // close printfile
    end;
     
    robsharpe, Mar 30, 2005 IP
  4. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm taking a stab here, but have two suggestions (without cracking D7 open):

    WriteLn is being used in the TForm1.procedure, but in the BoldIt procedure, it is using Write(). Try changing that to WriteLn.

    And/or...

    procedure BoldIt(s:string;PF:var);

    I'm taking a stab there, but try passing PF as a Var.

    and finally, if those don't work, put a stop/trace point on the line: Write(PF,s); in the BoldIt procedure and run your program. When it stops there, hold your cursor over PF and see what the value is. I guess worst case, you could define PF globally (outside of the procedure) to make it available for both procedures.

    Hope that helps.
     
    GTech, Mar 30, 2005 IP
  5. robsharpe

    robsharpe Peon

    Messages:
    258
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Write is definitely what I require, as writeln writes, then moves the pointer to a new line, whereas write writes to wherever the pointer is.

    It works when I don't use the boldit procedure, and just have two lines for every
    BoldIt('LABEL HERE',PrintFile);, as well, so I don't think it's the fact it says write.

    Changing PF from textfile to var causes the following error:
    [Error] Unit1.pas(655): Identifier expected but 'VAR' found

    And I can't even get to that stage of the program, as it won't let me get past the procedure BoldIt(s:string;PF:textfile); line... :(
    [Error] Unit1.pas(655): File type not allowed here

    Thanks for your help GTech... still very confused :confused:
     
    robsharpe, Mar 30, 2005 IP
  6. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Not sure how familiar you are with break points, but I took a screen shot:

    [​IMG]

    You can click in the area where the red dot is in the photo, next to a line of code, and it will highlight red. You can then run your program and when it gets to that line of code, it will stop. Then you can hold your cursor over variables and see their values. In this case, you could check the value of PF in the BoldIt procedure.

    I'm off to run some errands, thought I would post this last bit to try and help. Good luck!
     
    GTech, Mar 30, 2005 IP
  7. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #7
    Should be
    procedure BoldIt(s:string;var PF:Textfile);

    Never thought I'd be answering a Delphi question here :)
     
    dct, Mar 30, 2005 IP
  8. robsharpe

    robsharpe Peon

    Messages:
    258
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That works! :D

    Thank you so much! :)

    Rob
     
    robsharpe, Mar 30, 2005 IP
  9. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #9
    No problem, nice to need my Delphi head for a change, makes a change from C++
     
    dct, Mar 30, 2005 IP
  10. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Glad you got it sorted!
     
    GTech, Mar 30, 2005 IP