Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set permission on Access database file by VB6

Status
Not open for further replies.

acjeff

Programmer
Aug 10, 2004
148
0
0
US
By VB6 code program, how to grant or set permission to an Access database file? I need it to be least secured, that is, full control for all users, so that normal operator can use it. I will set other security issues in the VB program to protect the database.

Jeff
 
I do not think they are what I want. I want to set permission to a file instead of any tables in a database.

Here is the code in my VB program:

Set db = DBEngine.Workspaces(0).OpenDatabase("C:\db1.mdb", False, False, "MS Access;PWD=admin")

From Windows Explorer, when right click the file db1.mdb, select properties and under Security tab, I need to check Full Control to Everyone or Users.

However, every time I do Compact/Repair through VB code, the file's security settings are defaults. That's why I need more VB code to set the file's security preference every time after Compacting.

Hopefully my intention is understood.

Thanks,
 
What you will probably want to do is set the permissions on the folder that the file resides in.

If you really want to change file permissions then here is a class that I originally wrote in .NET that I just ported over to VB6:


In a new class (Class1 for this example)
Code:
Option Explicit

Private mobjADsSec As ADsSecurity
Private mobjSecDes As SecurityDescriptor
Private mobjDAcl As AccessControlList
Private mFolderOwner As String

Public Property Get FolderOwner() As String
    FolderOwner = mFolderOwner
End Property

Public Property Let FolderOwner(value As String)
    mFolderOwner = value
End Property

Public Sub Initialize(ByVal strFile As String)
    Set mobjADsSec = New ADsSecurity
    Set mobjSecDes = mobjADsSec.GetSecurityDescriptor("FILE://" & strFile)
    Set mobjDAcl = mobjSecDes.DiscretionaryAcl
End Sub

Public Sub AddUser(ByVal UserName As String)
    Dim objAce As Object
    Dim objAce1 As AccessControlEntry
    Dim objAce2 As AccessControlEntry
    Dim objSId As ADsSID
    Dim objSIdHex As String

    Set objSId = New ADsSID
    objSId.SetAs ADS_SID_SAM, CStr(UserName)
    objSIdHex = objSId.GetAs(ADS_SID_SDDL)

    Set objAce1 = New AccessControlEntry
    objAce1.Trustee = CStr(objSIdHex)
    objAce1.AccessMask = ADS_RIGHT_GENERIC_ALL
    objAce1.AceType = ADS_ACETYPE_ACCESS_ALLOWED
    objAce1.AceFlags = ADS_ACEFLAG_INHERIT_ACE Or ADS_ACEFLAG_INHERIT_ONLY_ACE Or 1
    mobjDAcl.AddAce objAce1

    ' Add a new objAce so that the User has Full Control on NTFS Folders.
    Set objAce2 = New AccessControlEntry
    objAce2.Trustee = CStr(objSIdHex)
    objAce2.AccessMask = ADS_RIGHT_GENERIC_ALL
    objAce2.AceType = ADS_ACETYPE_ACCESS_ALLOWED
    objAce2.AceFlags = ADS_ACEFLAG_INHERIT_ACE Or 1
    mobjDAcl.AddAce objAce2
End Sub

Public Sub CommitChanges()
    If mFolderOwner <> "" Then
        mobjSecDes.Owner = mFolderOwner
    End If
    mobjSecDes.DiscretionaryAcl = mobjDAcl
    ' Set Permissions on the NTFS folder.
    mobjADsSec.SetSecurityDescriptor mobjSecDes
End Sub

Public Sub ClearAll()
    Dim objAce1 As AccessControlEntry
    For Each objAce1 In mobjDAcl
        mobjDAcl.RemoveAce objAce1
    Next
End Sub

Code to execute it:
Code:
Private Sub Command1_Click()
    Dim objChangeUser As Class1
    
    Set objChangeUser = New Class1
    objChangeUser.FolderOwner = "bdenning"
    objChangeUser.Initialize "C:\dailyinv.txt"
    objChangeUser.ClearAll
    objChangeUser.AddUser "kbiermann"
    objChangeUser.AddUser "bdenning"
    objChangeUser.CommitChanges
    
    Set objChangeUser = Nothing
End Sub
 
Forgot to mention that you will need to add the following References to your application:

ADsSecurity 2.5 Type Library
Active DS Type Library
 
Check out SetAttr() to change attributes of the file in VB, much like the Attrib command used to do in DOS.

Code:
Sets attribute information for a file.

Syntax

SetAttr pathname, attributes

The SetAttr statement syntax has thesenamed arguments:

Part Description 
pathname Required.String expression that specifies a file name — may include directory or folder, and drive.  
attributes Required.Constant ornumeric expression, whose sum specifies file attributes. 


Settings

The attributesargument settings are:

Constant Value Description 
vbNormal 0 Normal (default). 
vbReadOnly 1 Read-only. 
vbHidden 2 Hidden. 
vbSystem 4 System file. 
vbArchive 32 File has changed since last backup. 


Note   These constants are specified by Visual Basic for Applications. The names can be used anywhere in your code in place of the actual values.

Remarks

Arun-time error occurs if you try to set the attributes of an open file.

David
 
David,

I think the SetAttr() method only set the file to be like hidden, read-only, etc. but not setting its security and users permission.

Thanks for help.

Jeff
 
I have not tried yet. In fact, I did not found the ADsSecurity.dll on my system so I need to download one online.

Your solution seems complicated. I want to ask if it will be risky to hurt the Windows system if I make any mistake or anything I miss?

Jeff
 
>>will be risky to hurt the Windows system

No. You will only be effecting the file that you when you call the Initialize method of the class.

>>Your solution seems complicated
This is about as easy as it could get to modify permissions on a file... The easier route (though understandable if it won't work for you) would be to set the permission you would like to have on the Access Database at the folder in which it resides. Then when you compact and repair the database it will be created with the permissions that are on the folder.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top