TC said:
Sure. See CreateQueryDef for how to create a new query. As for setting
the permissions, I don't have Access here to check, but something along
the following lines should work:
(untested)
dim db as database, con as container, doc as document
set db = currentdb()
set con = db.containers![querydefs]
set doc = con.documents![Your_Query_Name]
with doc
.user = "the user you want to set the permissions for"
.permissions = acReadDef OR ... (whatever)
end with
set doc = nothing
set con = nothing
set db = nothing
I can't remember whether a "document" object (named 'doc' in the code
above) has a "group" property to let you set permissions for a group
(instead of a user). If it does - that's how you do that! If it
doesn't, I can't remember & would have to check.
As I say - untested.
HTH,
TC
TC, thanks for putting me on the right track concerning the permissions. You
were very close in what you posted off the top of your head. For the benefit
of others, here is the code I used for a query called MyQuery and two user
groups (Full Data Access and Read-Only Access).
Dim db As Database, con As Container, doc As Document
Set db = CurrentDb()
Set con = db.Containers![Tables]
Set doc = con.Documents![MyQuery]
With doc
.UserName = "Read-Only Access"
.Permissions = dbSecReadDef Or dbSecWriteDef
.UserName = "Full Data Access"
.Permissions = dbSecReadDef Or dbSecWriteDef
End With
Set doc = Nothing
Set con = Nothing
Set db = Nothing
From playing around and finding some documentation it appears that you need
to use Tables as the container and UserName refers to users and groups. This
last point leads to another question. Does this imply that you cannot have a
User with the same name as a Group?
Brian