My database should be read-only except for a few people. My idea is to check the user as the database opens if they are not in the table, then change the validation rule for the table to False. I have the following code. Note that db is defined as a dao.database at the module level.
The code runs, and in the immediate window I can see the changes, but the tables do not change.
Function ResetValidationRule()
Dim iCounter As Integer
Dim propName As String
Dim propType As Integer
Dim propVal As Integer
Dim strS As String
Set db = CurrentDb
propName = "Validation Rule"
propType = 10 '10=text or vbtext 11=boolean or vbboolean
propVal = -1
On Error Resume Next
For iCounter = 0 To db.TableDefs.Count - 1
If (db.TableDefs(iCounter).Attributes And dbSystemObject) = 0 Then
If db.TableDefs(iCounter).Properties(propName).Value <> propVal Then
db.TableDefs(iCounter).Properties(propName).Value = propVal
End If
' 3270 propery not found
If Err.Number = 3270 Then
Set MyProperty = db.TableDefs(iCounter).CreateProperty(propName)
MyProperty.Type = propType
MyProperty.Value = propVal
db.TableDefs(iCounter).Properties.Append MyProperty
Else
If Err.Number < 0 Then
MsgBox "Error: " & Err.Number & " on Table " _
& db.TableDefs(iCounter).Name & "."
db.Close
Exit Function
End If
End If
End If
Next iCounter
MsgBox "The " & propName & _
" value for all non-system tables has been updated to " & propVal & "."
End Function
The code runs, and in the immediate window I can see the changes, but the tables do not change.
Function ResetValidationRule()
Dim iCounter As Integer
Dim propName As String
Dim propType As Integer
Dim propVal As Integer
Dim strS As String
Set db = CurrentDb
propName = "Validation Rule"
propType = 10 '10=text or vbtext 11=boolean or vbboolean
propVal = -1
On Error Resume Next
For iCounter = 0 To db.TableDefs.Count - 1
If (db.TableDefs(iCounter).Attributes And dbSystemObject) = 0 Then
If db.TableDefs(iCounter).Properties(propName).Value <> propVal Then
db.TableDefs(iCounter).Properties(propName).Value = propVal
End If
' 3270 propery not found
If Err.Number = 3270 Then
Set MyProperty = db.TableDefs(iCounter).CreateProperty(propName)
MyProperty.Type = propType
MyProperty.Value = propVal
db.TableDefs(iCounter).Properties.Append MyProperty
Else
If Err.Number < 0 Then
MsgBox "Error: " & Err.Number & " on Table " _
& db.TableDefs(iCounter).Name & "."
db.Close
Exit Function
End If
End If
End If
Next iCounter
MsgBox "The " & propName & _
" value for all non-system tables has been updated to " & propVal & "."
End Function