Stacey R. Brodsky Guest
|
Posted: Thu Oct 16, 2003 12:20 am Post subject: Newbie questions about ODBC |
|
|
First off, I hope I'm posting in the right forum. I wasn't exactly sure
where this went. I am trying to create a .mdb file, and then create a table
within it...all at run-time.
I can create the database correctly, can create an ODBC DSN entry in the
registry. Problem is, when I try to work with the database I get the
following error whether I am trying to open it, or get table info, etc.
Key Violation.
[Microsoft][ODBC Microsoft Access Driver]
Could not find file ('Unknown')
Alias : CRAccessData
I have a TDatabase, TQuery and TDataset all connected. There is nothing in
the params for the database, login is false.
Here is the code...can someone tell me what I'm doing wrong...Thanks!
Sorry : Long Post.....
unit formMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, StdCtrls, Registry;
const
ODBC_ADD_DSN = 1;
type
TForm1 = class(TForm)
Button1: TButton;
dbAccessDatabase: TDatabase;
Query1: TQuery;
DataSource1: TDataSource;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function SQLConfigDataSource(
hwndParent: HWND;
fRequest: WORD;
lpszDriver: LPCSTR;
lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';
procedure CreateDatabase(DbName: String);
begin
SQLConfigDataSource(0, ODBC_ADD_DSN,
'Microsoft Access Driver (*.mdb)',
PChar('CREATE_DB=' + DbName + #0));
end;
function GetWindowsSystemFolder: string;
var
Required: Cardinal;
begin
{This API wrapper simply gets the Windows System Directory
Win2K and Win98 have different system dirs.}
Result := '';
Required := GetSystemDirectory(nil, 0);
if Required <> 0 then
begin
SetLength(Result, Required -1);
GetSystemDirectory(PChar(Result), Required);
end;
end;
procedure CreateCRAccessODBCDSN;
const
ODBCDRIVERKEY = 'SoftwareODBCODBC.INI';
DATASOURCES = ODBCDRIVERKEY + 'ODBC Data Sources';
// SRB 20031015
DSNNAME = 'CRAccessData'; {Database Alias}
// END 20031015
JETKEY = ODBCDRIVERKEY + '' + DSNNAME + 'EnginesJet';
var
Reg : TRegistry;
begin
{A ODBC user DSN is stored in HKEY_CURRENT_USERSoftwareODBCODBC.INI
We are going to create a DSN on the fly that connects via the Jet Engine
ODBC Driver
This is so that we don't have to set up a DSN at each site.}
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
Reg.OpenKey(DATASOURCES,True);
Reg.WriteString(DSNNAME,'Microsoft Access Driver (*.mdb)');
Reg.OpenKey(ODBCDRIVERKEY + '' + DSNNAME,True);
Reg.WriteString('DBQ', 'CRACCESS.MDB');
Reg.WriteString('Driver',GetWindowsSystemFolder + 'odbcjt32.dll');
Reg.WriteString('FIL','MS Access;');
Reg.WriteString('UID','Admin');
Reg.WriteString('PWD','');
Reg.WriteInteger('SafeTransactions',0);
reg.WriteInteger('DriverId',25);
Reg.OpenKey(JETKEY,True);
Reg.WriteString('ImplicitCommitSync','');
Reg.WriteInteger('MaxBufferSize',2048);
Reg.WriteInteger('PageTimeout',5);
Reg.WriteInteger('Threads',3);
Reg.WriteString('UserCommitSync','Yes');
finally
Reg.Free;
end;
end;
procedure CreateRoamingTable;
var
FTable : TTable;
begin
FTable := TTable.Create(nil);
try
with FTable do begin
DatabaseName := 'C:TEMPCRACCESS.MDB';
TableName := 'ROAMING';
TableType := ttDefault;
Exclusive := False;
with FieldDefs do begin
Clear;
Add('UID', ftString, 20, True);
Add('ACCOUNT_NUMBER', ftInteger, 10, True);
Add('MEMBER_NUMBER', ftInteger, 10, True);
Add('MEMBERSHIP_TYPE', ftString, 50, True);
Add('FIRST_LAST', ftString, 100, True);
Add('FIRST_INIT_LAST_NAME', ftString, 50, True);
Add('HOME_PHONE', ftString, 12, False);
Add('HOME_PHONE_STRIPPED', ftString, 7, False);
Add('WORK_PHONE', ftString, 12, False);
Add('WORK_PHONE_STRIPPED', ftString, 7, False);
Add('ADDRESS_1', ftString, 100, False);
Add('HOME_CLUB', ftString, 3, True);
Add('ALLOW_ENTRY', ftString, 5, False);
Add('ACCESS_MESSAGE', ftString, 10, False);
Add('ACCESS_COLOR', ftString, 10, False);
Add('ACCESS_FONT_COLOR', ftString, 10, False);
Add('BARCODE', ftString, 12, False);
end;
IndexDefs.Clear;
CreateTable;
AddIndex('IDX_MEMBERNUMBER', 'MEMBER_NUMBER', []);
AddIndex('IDX_FIRSTINITLASTNAME', 'FIRST_INIT_LAST_NAME', []);
AddIndex('IDX_ACCOUNTNUMBER', 'ACCOUNT_NUMBER', []);
AddIndex('IDX_HOMEPHONE', 'HOME_PHONE_STRIPPED', []);
AddIndex('IDX_WORKPHONE', 'WORK_PHONE_STRIPPED', []);
end;
finally
FreeAndNil(FTable);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TableList : TStringList;
begin
if not FileExists('C:TEMPCRACCESS.MDB') then
CreateDatabase('C:TEMPCRACCESS.MDB');
CreateCRAccessODBCDSN;
dbAccessDatabase.Connected := True;
TableList := TStringList.Create();
try
Session.GetTableNames('CRAccessDatabase', '*.*', True, False,
TableList);
if TableList.IndexOf('ROAMING') = -1 then
CreateRoamingTable;
finally
FreeAndNil(TableList);
end;
ShowMessage('Fini!');
end;
end.
|
|