Solved

How to pass FileName as a string?

Posted on 2004-09-18
4
318 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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