Author: John Pears How to export All Tables in a Microsoft Jet DB to a CSV file Answer: 1 2 procedure TMainForm.SaveAllTablesToCSV(DBFileName: string); 3 var 4 InfoStr, 5 FileName, 6 RecString, 7 WorkingDirectory: string; 8 OutFileList, 9 TableNameList: TStringList; 10 TableNum, 11 FieldNum: integer; 12 VT: TVarType; 13 begin 14 ADOTable1.Active := false; 15 WorkingDirectory := ExtractFileDir(DBFileName); 16 TableNameList := TStringList.Create; 17 OutFileList := TStringList.Create; 18 InfoStr := 'The following files were created' + #13#13; 19 20 ADOConnection1.GetTableNames(TableNameList, false); 21 for TableNum := 0 to TableNameList.Count - 1 do 22 begin 23 FileName := WorkingDirectory + '\' + 24 TableNameList.Strings[TableNum] + '.CSV'; 25 Caption := 'Saving "' + ExtractFileName(FileName) + '"'; 26 ADOTable1.TableName := TableNameList.Strings[TableNum]; 27 ADOTable1.Active := true; 28 OutFileList.Clear; 29 30 ADOTable1.First; 31 while not ADOTable1.Eof do 32 begin 33 34 RecString := ''; 35 for FieldNum := 0 to ADOTable1.FieldCount - 1 do 36 begin 37 VT := VarType(ADOTable1.Fields[FieldNum].Value); 38 case VT of 39 // just write the field if not a string 40 vtInteger, vtExtended, vtCurrency, vtInt64: 41 RecString := RecString + ADOTable1.Fields[FieldNum].AsString 42 else 43 // it IS a string so put quotes around it 44 RecString := RecString + '"' + 45 ADOTable1.Fields[FieldNum].AsString + '"'; 46 end; { case } 47 48 // if not the last field then use a field separator 49 if FieldNum < (ADOTable1.FieldCount - 1) then 50 RecString := RecString + ','; 51 end; { for FieldNum } 52 OutFileList.Add(RecString); 53 54 ADOTable1.Next; 55 end; { while } 56 57 OutFileList.SaveToFile(FileName); 58 InfoStr := InfoStr + FileName + #13; 59 ADOTable1.Active := false; 60 61 end; { for TableNum } 62 TableNameList.Free; 63 OutFileList.Free; 64 Caption := 'Done'; 65 ShowMessage(InfoStr); 66 end; 67 68 procedure TMainForm.Button1Click(Sender: TObject); 69 const 70 ConnStrA = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='; 71 ConnStrC = ';Persist Security Info=False'; 72 ProvStr = 'Microsoft.Jet.OLEDB.4.0'; 73 begin 74 OpenDialog1.InitialDir := ExtractFileDir(ParamStr(0)); 75 if OpenDialog1.Execute then 76 77 try 78 ADOConnection1.ConnectionString := 79 ConnStrA + OpenDialog1.FileName + ConnStrC; 80 ADOConnection1.Provider := ProvStr; 81 ADOConnection1.Connected := true; 82 ADOTable1.Connection := ADOConnection1; 83 SaveAllTablesToCSV(OpenDialog1.FileName); 84 except 85 ShowMessage('Could not Connect to ' + #13 + 86 '"' + OpenDialog1.FileName + '"'); 87 Close; 88 end; 89 90 end;