BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie questions about ODBC

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop)
View previous topic :: View next topic  
Author Message
Stacey R. Brodsky
Guest





PostPosted: Thu Oct 16, 2003 12:20 am    Post subject: Newbie questions about ODBC Reply with quote



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.


Back to top
Walter Prins
Guest





PostPosted: Tue Oct 21, 2003 10:37 pm    Post subject: Re: Newbie questions about ODBC Reply with quote




"Stacey R. Brodsky" <sbrodsky68@nospam^aol.com> wrote

Quote:
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 don't think you can do it in the way thay you're trying to. The easiest
way is probably to use ADOX, see for example here
http://delphi.about.com/library/weekly/aa072401a.htm
and
http://groups.google.com/groups?q=adox+create+database+delphi&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=ocd7as4nd0t06n90e0u5td89d5lr34oa4f%404ax.com&rnum=1
(which is also http://urljr.com/6p)

Regards

Walter Prins



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.