Solved

How to pass FileName as a string?

Posted on 2004-09-18
4
324 Views
Last Modified: 2010-04-05
O.K., I have been stuck on this for about 3 days straight.  I am trying to pass the name of a file as a string so I can open it in a procedure.  The problem I am having, is that TStringList is not compatible with String (obvious, I know).  Here's some of my code:

//Onclick procedure which lets the user navigate to and select a file.

procedure TForm1.Button1Click(Sender: TObject);

var
  OpenFile : TOpenDialog;
  FileName : TStringList;

begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist];
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
     then FileName := Tstringlist.Create
         else MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   Edit1.Text := OpenFile.FileName;

end;

//Here's the procedure which loads the CSV and stores the data in an array.


Procedure CSVimport(FileName: String);
Var
  MyFile, MyColumns: TStringList;
  Row, Col: integer;
begin
 
  MyFile := TStringList.Create;
  MyColumns := TStringList.Create;
  MyColumns.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);  //This is where the code errors.  File name is not passed.
    If MyFile.Count > 0 then
    Begin
      MyColumns.DelimitedText := MyFile[Row];

      For Col := 0 to MyColumns.Count -1 Do
        MyColumns[Col] := MyColumns[Col] + ' varchar(50)';

      MyFile[Row] := 'CREATE TABLE TABLE1 (ID INT(2), ' + MyColumns.DelimitedText +  ', PRIMARY KEY(ID));';
    end;  

This is not all the code, the rest seems to compile no probs.  I just can't figure out how pass the file name to the procedure, so I can load it in the procedure and pass the contents of the CSV into the array.  I realize there are a few ways of doing this, however, my main goal is to pass the filename as a string, so I can potentially use a constructor Create(FileName: String); in another unit.  I can figure this part out, just need to get through my initial stumbling block...  

HELP!!

Ta.
0
Comment
Question by:south_paw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:gary_williams
ID: 12093486

Why do you not call

CSVImport(OpenFile.FileName);

instead of stuffing the filename into an edit control?
0
 
LVL 4

Accepted Solution

by:
Evarest earned 500 total points
ID: 12093490
I'm at a loss :-)

Your code as you give it gives no error with me. Its syntax is correct.

If you want to pass the FileName as string to a procedure, and that filename is stored in a TStringList, just pass an item out of that list to the procedure. I've altered your code so that every file opened by using TOpenDialog will be sent to the CSVImport procedure:

procedure TForm1.Button1Click(Sender: TObject);
var
  OpenFile : TOpenDialog;
  FileName : TStringList;
  Q: integer;
begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist, ofAllowMultiSelect]; //<-- ofAllowMultiSelect to select more files in the TOpenDialog
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
    then
     begin
      for Q :=0 to OpenFile.Files.Count -1 do // <-- Loop to send the FileNames to the procedure
       begin
        CSVImport(OpenFile.Files[Q]); // <-- Call to the procedure CSVImport
       end;
     end
     else MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   // Edit1.Text := OpenFile.FileName; // <-- has no use when multiselecting

end;

//Here's the procedure which loads the CSV and stores the data in an array.


Procedure CSVimport(FileName: String);
Var
  MyFile, MyColumns: TStringList;
  Row, Col: integer;
begin
 
  MyFile := TStringList.Create;
  MyColumns := TStringList.Create;
  MyColumns.QuoteChar := ' ';
  try
    MyFile.LoadFromFile(FileName);  //This is where the code errors.  File name is not passed.
    If MyFile.Count > 0 then
    Begin
      MyColumns.DelimitedText := MyFile[Row];

      For Col := 0 to MyColumns.Count -1 Do
        MyColumns[Col] := MyColumns[Col] + ' varchar(50)';

      MyFile[Row] := 'CREATE TABLE TABLE1 (ID INT(2), ' + MyColumns.DelimitedText +  ', PRIMARY KEY(ID));';
    end;
  except
  end;
end;

Let me know if this is what you mean!
Evarest
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12093493
Maybe I don't understand the problem, but why don't you make Filename a string, as in:

procedure TForm1.Button1Click(Sender: TObject);

var
  OpenFile : TOpenDialog;
  FileName : String;

begin

  OpenFile := TOpenDialog.Create(self);
  OpenFile.InitialDir := GetCurrentDir;
  OpenFile.Options := [ofFileMustExist];
  OpenFile.Filter := 'CSV files only |*.csv';
  OpenFile.FilterIndex := 2;

   if OpenFile.Execute
     then begin
        FileName := OpenFile.FileName;

       CVSImport(FileName);
    end
    else
      MessageDlg('Are you sure?',mtError, mbOKCancel, 0);

   Edit1.Text := OpenFile.FileName;

end;
0
 

Author Comment

by:south_paw
ID: 12093586
Thanks, I get it now.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question