MYOB Customer File CSV Import     

 

{This script shows importing from an MYOB comma separated values file}

{Shows parsing values from the line using its index}

{How to use a progress indicator is also shown}

 

function GetCSVFile: string;

var

OpenDlg: TOpenDialog;

begin

OpenDlg := TOpenDialog.Create(Nil);

try

   OpenDlg.Filter := 'Text Files (*.txt)|*.txt|Comma Separated Values (*.csv)|*.csv|All Files (*.*)|*.*';

   if OpenDlg.Execute then

     Result := OpenDlg.filename

   else

     Result := '';

finally

  OpenDlg.Free;

end;

end;

 

function ImportFromCSVFile: boolean;

var

ImportFile: String;

ImportValuesList, FieldMappings: TStringList;

x: Integer;

CustomerType, TaxGroup: String;

begin

Result := True;

ImportFile := GetCSVFile;

if trim(ImportFile) = '' then

begin

   Result := False;

   exit;

end;

{Ask for a customer type, Tax Group to be used}

CustomerType := AskQuestionWithLookup('Select a Customer Type', 'This will be used for all Customers you are importing', 1028, '');

TaxGroup := AskQuestionWithLookup('Select a Tax Group', 'This will be used for all Customers you are importing', 1017, '');

if trim(CustomerType) = '' then

begin

   Result := False;

   exit;

end;

ImportValuesList := TStringList.Create;

FieldMappings := TStringList.Create;

try

   {Load ImportFile into StringList}

   ImportValuesList.LoadFromFile(ImportFile);

   FieldMappings.clear;

   {Loop the list and add fieldnames/values to mappings StringList}

   {Start at 1 rather than 0 to skip header record if you exported it from MYOB}

   ShowProgress('Importing Customers...', (ImportValuesList.Count-1), False);

   for x := 1 to ImportValuesList.Count -1 do {check customer does not already exist}

   begin

     if DBValueExists('CUSTOMERMASTER', 'CUSTOMER', ParseString(ImportValuesList.Strings[x],',',0), False) <> True then

     begin

       FieldMappings.clear;

       FieldMappings.Add('CUSTOMER=' + ParseString(ImportValuesList.Strings[x],',',0));

       FieldMappings.Add('CUSTOMERTYPE=' + CustomerType);

       FieldMappings.Add('CUSTOMERSTATUS=Active');

       FieldMappings.Add('CUSTOMERINVOICENAME=' + ParseString(ImportValuesList.Strings[x],',',0));

       FieldMappings.Add('CUSTOMERADDRESS1=' + ParseString(ImportValuesList.Strings[x],',',5));

       FieldMappings.Add('CUSTOMERADDRESS2=' + ParseString(ImportValuesList.Strings[x],',',6));

       FieldMappings.Add('CUSTOMERADDRESS3=' + ParseString(ImportValuesList.Strings[x],',',7));

       FieldMappings.Add('CUSTOMERPOSTALCODE=' + ParseString(ImportValuesList.Strings[x],',',11));

       FieldMappings.Add('CUSTOMERSTATE=' + ParseString(ImportValuesList.Strings[x],',',10));

       FieldMappings.Add('CUSTOMERCITY=' + ParseString(ImportValuesList.Strings[x],',',9));

       FieldMappings.Add('CUSTOMERCOUNTRY=' + ParseString(ImportValuesList.Strings[x],',',12));

       FieldMappings.Add('CUSTOMERPHONE=' + ParseString(ImportValuesList.Strings[x],',',13));

       FieldMappings.Add('CUSTOMERFAX=' + ParseString(ImportValuesList.Strings[x],',',16));

       FieldMappings.Add('CUSTOMEREMAIL=' + ParseString(ImportValuesList.Strings[x],',',17));

       FieldMappings.Add('CUSTOMERWEB=' + ParseString(ImportValuesList.Strings[x],',',18));

       FieldMappings.Add('TAXGROUP=' + TaxGroup);

       InsertRecord('CUSTOMERMASTER', FieldMappings.text, False);

     end;

     UpdateProgress(x);

   end;

finally

   EndProgress;

   ImportValuesList.Free;

   FieldMappings.Free;

end;

end;

 

begin

if ImportFromCSVFile then

   MessageDlg('Import Complete',mtinformation,mbok,0)

else

   MessageDlg('Import Cancelled',mtinformation,mbok,0);

end.