Public Function SetRunPermissions(qryName As String, SetToOwner As Boolean)
On Error GoTo SetRunPermissions_Error
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim mySQL As String
mySQL = dbs.QueryDefs(qryName).SQL
If (SetToOwner) Then
If (InStr(1, mySQL, "WITH OWNERACCESS OPTION", vbBinaryCompare) <> 0) Then
'MsgBox "Owner option already set."
Exit Function
Else
mySQL = Left(mySQL, InStr(1, mySQL, ";", vbBinaryCompare) - 1) & " WITH OWNERACCESS OPTION;"
'MsgBox "Setting owner option."
End If
Else
If (InStr(1, mySQL, "WITH OWNERACCESS OPTION", vbBinaryCompare) <> 0) Then
mySQL = Left(mySQL, InStr(1, mySQL, "WITH OWNERACCESS OPTION", vbBinaryCompare) - 1) & " ;"
'MsgBox "Removing owner access option."
Else
'MsgBox "Owner option already removed."
Exit Function
End If
End If
dbs.QueryDefs(qryName).SQL = mySQL
'MsgBox dbs.QueryDefs(qryName).SQL
Exit Function
SetRunPermissions_Error:
MsgBox "Error " & Err.Number & ": " & Err.Description
End Function