1. Operate access database in Delphi (create .mdb file, compress database)
The following code has been tested under win2k, d6, and mdac2.6.
The compiled program runs successfully in the win98 second version without access environment.
//before uses comobj,activex
//Declare the connection string
const
connectionstring = 'provider=microsoft.jet.oledb.4.0;data source=%s;'
+'jet oledb:database password=%s;';
//================================================ =============================
// procedure: gettemppathfilename
// author: ysai
// date : 2003-01-27
// arguments: (none)
// result: string
//================================================ =============================
function gettemppathfilename():string;
//Get the temporary file name
var
spath,sfile&:array [0..254] of char;
begin
gettemppath(254,spath);
gettempfilename(spath,'~sm',0,sfile);
result:=sfile;
deletefile(pchar(result));
end;
//================================================ =============================
// procedure: createaccessfile
// author: ysai
// date : 2003-01-27
// arguments: filename:string;password:string=''
// result: boolean
//================================================ =============================
function createaccessfile(filename:string;password:string=''):boolean;
//Create access file, fail if the file exists
var
stempfilename:string;
vcatalog:olevariant;
begin
stempfilename:=gettemppathfilename;
try
vcatalog:=createoleobject('adox.catalog');
vcatalog.create(format(connectionstring,[stempfilename,password]));
result:=copyfile(pchar(stempfilename),pchar(filename),true);
deletefile(stempfilename);
except
result:=false;
end;
end;
//================================================ =============================
// procedure: compactdatabase
// author: ysai
// date : 2003-01-27
// arguments: afilename,apassword:string
// result: boolean
//================================================ =============================
function compactdatabase(afilename,apassword:string):boolean;
//Compress and repair database, overwrite source files
var
stempfilename:string;
vje:olevariant;
begin
stempfilename:=gettemppathfilename;
try
vje:=createoleobject('jro.jetengine');
vje.compactdatabase(format(connectionstring,[afilename,apassword]),
format(sconnectionstring,[stempfilename,apassword]));
result:=copyfile(pchar(stempfilename),pchar(afilename),false);
deletefile(stempfilename);
except
result:=false;
end;
end;
//================================================ =============================
// procedure: changedatabasepassword
// author: ysai
// date : 2003-01-27
// arguments: afilename,aoldpassword,anewpassword:string
// result: boolean
//================================================ =============================
function changedatabasepassword(afilename,aoldpassword,anewpassword:string):boolean;
//Modify access database password
var
stempfilename:string;
vje:olevariant;
begin
stempfilename:=gettemppathfilename;
try
vje:=createoleobject('jro.jetengine');
vje.compactdatabase(format(connectionstring,[afilename,aoldpassword]),
format(sconnectionstring,[stempfilename,anewpassword]));
result:=copyfile(pchar(stempfilename),pchar(afilename),false);
deletefile(stempfilename);
except
result:=false;
end;
end;
2. Things you should pay attention to and some tips when using sql statements in access
The following sql statement passed the test in access xp query
Create table:
create table tab1 (
id counter,
name string,
age integer,
[date] datetime);
Skill:
Auto-increment fields are declared with counter.
Fields whose field names are keywords are enclosed in square brackets [], and numbers are also acceptable as field names.
Create index:
The following statement creates a repeatable index on the date column of tab1
create index idate on tab1 ([date]);
After completion, the date index attribute of the field in access is displayed as - Yes (there are duplicates).
The following statement creates a non-repeatable index on the name column of tab1
create unique index iname on tab1 (name);
After completion, the index attribute of the field name in access is displayed as - Yes (no duplication).
The following statement deletes the two indexes just created
drop index idate on tab1;
drop index iname on tab1;
Comparison of update statements in access and sqlserver:
Update statement to update multiple tables in sqlserver:
update tab1
set a.name = b.name
from tab1 a,tab2 b
where a.id = b.id;
The sql statement with the same function in access should be
update tab1 a,tab2 b
set a.name = b.name
where a.id = b.id;
That is: the update statement in access does not have a from clause, and all referenced tables are listed after the update keyword.
In the above example, if tab2 is not a table, but a query, for example:
update tab1 a,(select id,name from tab2) b
set a.name = b.name
where a.id = b.id;
Access multiple different access databases - use the in clause in sql:
select a.*,b.* from tab1 a,tab2 b in 'db2.mdb' where a.id=b.id;
The above sql statement queries all records associated with tab2 in tab1 and db2.mdb (in the current folder) in the current database.
Disadvantages - External databases cannot have passwords.
Supplement: I saw ugvanxk’s reply in a post and can use it
select * from [c:/aa/a.mdb;pwd=1111].table1;
access xp test passed
Access other odbc data sources in access
The following example queries data in sqlserver in access
select * from tab1 in [odbc]
[odbc;driver=sql server;uid=sa;pwd=;server=127.0.0.1;database=demo;]
The complete parameters of the external data source connection property are:
[odbc;driver=driver;server=server;database=database;uid=user;pwd=password;]
The driver=driver can be found in the registry
hkey_local_machine/software/odbc/odbcinst.ini/
found in
For data import between heterogeneous databases, please refer to Blue Blood Sword
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1691966
access supports subqueries
access supports outer joins, but does not include complete outer joins, if supported
left join or right join
but not supported
full outer join or full join
Date query in access
Note: The date and time separator in access is # instead of quotation marks
select * from tab1 where [date]>#2002-1-1#;
In delphi I use this
sql.add(format(
'select * from tab1 where [date]>#%s#;',
[datetostr(date)]));
Strings in access can be separated by double quotes, but sqlserver does not recognize them, so for the convenience of migration and compatibility,
It is recommended to use single quotes as string delimiters.