Author: Lou Adler How to create an ODBC datasource for an Access database Answer: 1 unit odbcsetup; 2 {Written By Aaron Miles 2002 3 Creates ODBC for Access Database} 4 5 interface 6 7 uses 8 sysutils, windows; 9 10 procedure CreateDSN(Name, Database: string); 11 procedure RemoveDSN(Name: string); 12 13 implementation 14 15 const 16 ODBC_ADD_DSN = 1; {Add data source} 17 ODBC_CONFIG_DSN = 2; {Configure (edit) data} 18 ODBC_REMOVE_DSN = 3; {Remove data source} 19 ODBC_ADD_SYS_DSN = 4; {Add a system DSN} 20 ODBC_CONFIG_SYS_DSN = 5; {Configure a system DSN} 21 ODBC_REMOVE_SYS_DSN = 6; {Remove a system DSN} 22 ODBC_REMOVE_DEFAULT_DSN = 7; {Remove the default DSN} 23 24 function SQLConfigDataSource(hwndParent: HWND; fRequest: WORD; lpszDriver: LPCSTR; 25 lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL'; 26 27 procedure CreateDSN(Name, Database: string); 28 begin 29 SQLConfigDataSource(0, ODBC_ADD_DSN, 'Microsoft Access Driver (*.mdb)', 30 PChar('DSN = 31 '+ #0 + ' Driver = ODBCJT32.DLL '#0 + ' DBQ = '+Database+ #0 + 32 'DefaultDir=' + ExtractFilePath(Database) + #0 + 'Description=Auto Setup 33 Aaron Miles 'ccess '#0 + ' UID = Admin'#0)); 34 end; 35 36 procedure RemoveDSN(Name: string); 37 begin 38 SQLConfigDataSource(0, ODBC_REMOVE_DSN, ' 39 PChar('DSN=' + Name + #0)); 40 end; 41 42 end.