Opening a Secured Access Database

   

After you've secured the database, you can open it as usual if Admin has no password. If Admin has a password, you can still open the database, but you'll need to supply your username and password in the Logon dialog box.

You might also need to know how to get the database open using VBA code. To open the database using DAO, you would use code similar to this (don't forget to set a reference to the DAO object library first):

 Dim wkspTempWorkspace As Workspace Dim strSecuredDB As String Dim strUserName As String Dim strPassword As String Dim dbSecureDB As DAO.Database Dim rsTableToOpen As DAO.Recordset 

After declaring the necessary variables, tell VBA where to find the workgroup information file:

 DBEngine.SystemDB = _ "C:\Documents and Settings\Resources.mdw" 

You'll want to be very careful with this code because it exposes a username and password. The username should be for a user identified in the workgroup file:

 strUserName = "Conrad Carlberg" strPassword = "KeepThisSecret" 

Create a new, temporary workspace and append it to the existing workspaces collection. It contains the username and password:

 Set wkspTempWorkspace = DBEngine.CreateWorkspace _ ("New", strUserName, strPassword) DBEngine.Workspaces.Append wkspTempWorkspace 

Now provide the path to and name of the secured database, and open it:

 strSecuredDB = _ "C:\NCS\Room Reservations\Resources 2003.mdb" Set dbSecureDB = DBEngine.Workspaces("New") _ .OpenDatabase(strSecuredDB) 'Code to manipulate the database given the user's _ permissions goes here. 

And close the database:

 dbSecureDB.Close 

The code that is not shown that you would use to manipulate data and objects in the database will run a little more efficiently if you use the DAO approach to opening the secured database. If you prefer, though, you can use ADO. It's not quite as efficient because ADO is not optimized specifically for Jet databases. But it's a lot shorter. The admonition about the exposed username and password still applies (and don't forget to set a reference to the ADO object library first):

 Dim cnResourceConnection As New ADODB.Connection With cnResourceConnection    .Provider = "Microsoft.Jet.OLEDB.4.0"    .Properties("Data Source") = _ "C:\NCS\Room Reservations\Resources 2003.mdb"    .Properties("Jet OLEDB:System database") = _ "C:\Documents and Settings\Resources.mdw"    .Open UserId:="Conrad Carlberg", Password:="KeepThisSecret" End With 'Code to manipulate the database given the user's _ permissions goes here. cnResourceConnection.Close 



Managing Data with Microsoft Excel
Managing Data with Microsoft Excel
ISBN: 789731002
EAN: N/A
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net