Program: Create, Compress Access database and modify password demo
Last Update:2017-02-28
Source: Internet
Author: User
access| Program | create | data | database | compress
* --------------------------------------------
* Program: Create, Compress Access database and modify password demo
* Design: Red rain
* --------------------------------------------
Local Lcmdbfile, Lcretustr
Lcmdbfile = [C:\Temp\TestCreaMdbFile.mdb]
LCPSWD1 = [Test1]
LCPSWD2 = [Test2]
LCPSWD3 = [Test3]
LCRETUSTR = [Create, compress Access database and modify password Demo:] + Chr (13)
If Createmdb (Lcmdbfile, LCPSWD1)
Lcretustr = Lcretustr + Chr (13) + [1, CREATE DATABASE Success-Password:] + lcPswd1
If Compactmdb (Lcmdbfile, LCPSWD1, LCPSWD2)
Lcretustr = Lcretustr + Chr (13) + [2, Compress and modify password Success-Password:] + lcPswd2
If Changemdbpassword (Lcmdbfile, LCPSWD2, LCPSWD3)
Lcretustr = Lcretustr + Chr (13) + [3] Modify database password successfully-password:] + lcPswd3
Else
Lcretustr = Lcretustr + Chr (13) + [3] Failed to modify database password individually]
Endif
Else
Lcretustr = Lcretustr + Chr (13) + [2, compressing and modifying password failed]
Endif
Else
Lcretustr = Lcretustr + Chr (13) + [1, failed to create database]
Endif
= MessageBox (Lcretustr, 0+64+0, [red rain hint])
Return
* --------------------------------------------
Function Createmdb (Tcmdbfile, TCPSWDSTR)
* Create an Access database file (. mdb)
Local IsOK
IsOK =. F.
Tcmdbfile = IIF (Type ([Tcmdbfile]) =[c], Tcmdbfile, [])
Tcpswdstr = IIF (Type ([tcpswdstr]) =[c], TCPSWDSTR, [])
If File (Tcmdbfile)
Erase (Tcmdbfile)
Endif
If! File (Tcmdbfile)
IsOK =. T.
Local loengine, Lcolderror, Lccmdstrs
Lcolderror = ON ([ERROR])
On Error IsOK =. F.
Locatalog = CreateObject ([ADOX. Catalog])
Lccmdstrs = [provider=microsoft.jet.oledb.4.0];
+ [;D ata source=] + tcmdbfile;
+ [; Jet oledb:database password=] + tcpswdstr
Locatalog.create (Lccmdstrs)
Release Locatalog
Locatalog = Null
On Error &lcolderror.
Endif
Return IsOK and File (tcmdbfile)
Endfunc
* --------------------------------------------
Function Compactmdb (Tcmdbfile, TCOLDPSWD, TCNEWPSWD)
* Compress Access database and set password
Local IsOK
IsOK =. F.
Tcmdbfile = IIF (Type ([Tcmdbfile]) =[c], Tcmdbfile, [])
TCOLDPSWD = IIF (Type ([tcoldpswd]) =[c], tcoldpswd, [])
TCNEWPSWD = IIF (Type ([tcnewpswd]) =[c], tcnewpswd, TCOLDPSWD)
If File (Tcmdbfile)
IsOK =. T.
Local loengine, Lctmpfile, Lcolderror, Lccompoldstr, Lccompnewstr
Lcolderror = ON ([ERROR])
On Error IsOK =. F.
Lctmpfile = Addb (Justpath (tcmdbfile)) + Subs (Sys (2015), 3) + [. mdb]
Rename (Tcmdbfile) to (Lctmpfile)
If! File (tcmdbfile) and file (Lctmpfile)
LCCOMPOLDSTR = [provider=microsoft.jet.oledb.4.0];
+ [;D ata source=] + lctmpfile;
+ [; Jet oledb:database password=] + tcoldpswd
LCCOMPNEWSTR = [provider=microsoft.jet.oledb.4.0];
+ [;D ata source=] + tcmdbfile;
+ [; Jet oledb:database password=] + tcnewpswd
Loengine = CreateObject ([JRO. JetEngine])
Loengine.compactdatabase (Lccompoldstr, LCCOMPNEWSTR)
Release Loengine
Loengine = Null
If File (Tcmdbfile)
Erase (Lctmpfile)
Else
Rename (Lctmpfile) to (Tcmdbfile)
Endif
Else
IsOK =. F.
Endif
On Error &lcolderror.
Endif
Return IsOK and File (tcmdbfile)
Endfunc
* --------------------------------------------
Function Changemdbpassword (Tcmdbfile, TCOLDPSWD, TCNEWPSWD)
* Modify Access Database password, you must open the database exclusively, make sure no other programs use the database before use
Local IsOK
IsOK =. F.
LCRETUSTR = []
Tcmdbfile = IIF (Type ([Tcmdbfile]) =[c], Tcmdbfile, [])
TCOLDPSWD = IIF (Type ([tcoldpswd]) =[c], tcoldpswd, [])
TCNEWPSWD = IIF (Type ([tcnewpswd]) =[c], tcnewpswd, [])
If File (Tcmdbfile)
IsOK =. T.
Local Loadodb, Lcolderror
Lcolderror = ON ([ERROR])
On Error IsOK =. F.
Loadodb = CreateObject ([ADODB. Connection])
Loadodb.mode = 12
Loadodb.provider = [Microsoft.Jet.OLEDB.4.0]
Loadodb.properties ([Jet oledb:database Password]) = TCOLDPSWD
Loadodb.open ([Data source=] + tcmdbfile)
Loadodb.execute (' ALTER DATABASE PASSWORD [' + tcnewpswd + '] [' + tcoldpswd + '] ')
Loadodb.close
Release Loadodb
Loadodb = Null
On Error &lcolderror.
Endif
Return IsOK
Endfunc
* --------------------------------------------