How to pass FileName as a string?
Posted on 2004-09-18
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);
OpenFile : TOpenDialog;
FileName : TStringList;
OpenFile := TOpenDialog.Create(self);
OpenFile.InitialDir := GetCurrentDir;
OpenFile.Options := [ofFileMustExist];
OpenFile.Filter := 'CSV files only |*.csv';
OpenFile.FilterIndex := 2;
then FileName := Tstringlist.Create
else MessageDlg('Are you sure?',mtError, mbOKCancel, 0);
Edit1.Text := OpenFile.FileName;
//Here's the procedure which loads the CSV and stores the data in an array.
Procedure CSVimport(FileName: String);
MyFile, MyColumns: TStringList;
Row, Col: integer;
MyFile := TStringList.Create;
MyColumns := TStringList.Create;
MyColumns.QuoteChar := ' ';
MyFile.LoadFromFile(FileName); //This is where the code errors. File name is not passed.
If MyFile.Count > 0 then
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));';
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...