Using Access 2000, I am trying to disable the shift key on my database so that my users can't bypass my startup procedures.
I found a good example on this site, in FAQ 181-1172 (Disable Design View Through VB Coding). Since I have security set up, I thought this would be a good approach, since it will enable/disable the startup options based on the login ID of the user. When I tried the code, it worked the first time I opened the database, in that the options were restricted. However, when I changed login IDs, the options weren't reset and stayed the same as the previous user's. The submitter stated that the code would need to be modified for Access 2000, but I didn't get any error messages. It just didn't work.
Has anyone else used this method in Access 2000, or would know why the options are not being reset? I am a beginner at VBA, so I am only to the point of being able to copy and paste the code, with minor modifications.
Thanks for your help! (For ease of reference, I have pasted the Access 97 code from FAQ 181-1172 below)
Option Compare Database
Option Explicit
Global sLogon As String
Public Function StartUp()
Dim Designer As String
Dim Restart As Boolean
Dim stAppName As String
Designer = "yourlogonhere"
FIND_USER
If sLogon = Designer Then
Restart = UnlockStartup
Else
Restart = LockStartup
End If
If Restart Then
'Close database and re-open
stAppName = "MSAccess.exe " & CurrentDb.Name
Call Shell(stAppName, 1)
DoCmd.Quit
Else
If sLogon <> Designer Then DoCmd.OpenForm "TitleForm"
End If
End Function
-----------------------------------
Sub FIND_USER()
On Error GoTo ERR_FIND_USER
Dim UserParam$
Dim sChk As String
Dim CurrentAuditor As String
UserParam$ = Environ("S_USER"
If UserParam$ = "" Then UserParam$ = Environ("USERNAME"
sLogon = UCase$(UserParam$)
EXIT_FIND_USER:
Exit Sub
ERR_FIND_USER:
MsgBox Error$
Resume EXIT_FIND_USER
End Sub
---------------------------------------------
Function LockStartup() As Boolean
Dim Restart As Boolean
Restart = False
ChangeProperty "StartupShowDBWindow", dbBoolean, False, Restart
ChangeProperty "AllowBuiltinToolbars", dbBoolean, False, Restart
ChangeProperty "AllowFullMenus", dbBoolean, False, Restart
ChangeProperty "AllowToolbarChanges", dbBoolean, False, Restart
ChangeProperty "AllowBreakIntoCode", dbBoolean, False, Restart
ChangeProperty "AllowSpecialKeys", dbBoolean, False, Restart
ChangeProperty "AllowBypassKey", dbBoolean, False, Restart
Application.SetOption "Show Hidden Objects", False
LockStartup = Restart
End Function
-----------------------------------------------
Function UnlockStartup() As Boolean
Dim Restart As Boolean
Restart = False
ChangeProperty "StartupMenuBar", dbText, "(default)", Restart
ChangeProperty "StartupShowDBWindow", dbBoolean, True, Restart
ChangeProperty "StartupShowStatusBar", dbBoolean, True, Restart
ChangeProperty "AllowBuiltinToolbars", dbBoolean, True, Restart
ChangeProperty "AllowFullMenus", dbBoolean, True, Restart
ChangeProperty "AllowToolbarChanges", dbBoolean, True, Restart
ChangeProperty "AllowBreakIntoCode", dbBoolean, True, Restart
ChangeProperty "AllowSpecialKeys", dbBoolean, True, Restart
ChangeProperty "AllowBypassKey", dbBoolean, True, Restart
UnlockStartup = Restart
End Function
-------------------------------------------
Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant, Restart As Boolean) As Integer
Dim dbs As Database, prp As Property
Dim CurrentPropVal As Variant
Const conPropNotFoundError = 3270
Set dbs = CurrentDb
On Error GoTo Change_Err
CurrentPropVal = dbs.Properties(strPropName)
If CurrentPropVal <> varPropValue Then
dbs.Properties(strPropName) = varPropValue
Restart = True 'need to restart database
End If
ChangeProperty = True
Change_Bye:
Exit Function
Change_Err:
If Err = conPropNotFoundError Then 'Property not found.
Set prp = dbs.CreateProperty(strPropName, varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
ChangeProperty = False
Resume Change_Bye
End If
End Function
To get it working just follow these steps:
1. Put the above code in a module in your db.
2. Create a macro called autoexec and put RunCode StartUp() in it.
3. In the StartUp function change the "Designer = " line to your logon id, or change the designer variable to an array of names which can access the database design.
4. In the StartUp function change the docmd.openform command to reference your start up form.
I found a good example on this site, in FAQ 181-1172 (Disable Design View Through VB Coding). Since I have security set up, I thought this would be a good approach, since it will enable/disable the startup options based on the login ID of the user. When I tried the code, it worked the first time I opened the database, in that the options were restricted. However, when I changed login IDs, the options weren't reset and stayed the same as the previous user's. The submitter stated that the code would need to be modified for Access 2000, but I didn't get any error messages. It just didn't work.
Has anyone else used this method in Access 2000, or would know why the options are not being reset? I am a beginner at VBA, so I am only to the point of being able to copy and paste the code, with minor modifications.
Thanks for your help! (For ease of reference, I have pasted the Access 97 code from FAQ 181-1172 below)
Option Compare Database
Option Explicit
Global sLogon As String
Public Function StartUp()
Dim Designer As String
Dim Restart As Boolean
Dim stAppName As String
Designer = "yourlogonhere"
FIND_USER
If sLogon = Designer Then
Restart = UnlockStartup
Else
Restart = LockStartup
End If
If Restart Then
'Close database and re-open
stAppName = "MSAccess.exe " & CurrentDb.Name
Call Shell(stAppName, 1)
DoCmd.Quit
Else
If sLogon <> Designer Then DoCmd.OpenForm "TitleForm"
End If
End Function
-----------------------------------
Sub FIND_USER()
On Error GoTo ERR_FIND_USER
Dim UserParam$
Dim sChk As String
Dim CurrentAuditor As String
UserParam$ = Environ("S_USER"
If UserParam$ = "" Then UserParam$ = Environ("USERNAME"
sLogon = UCase$(UserParam$)
EXIT_FIND_USER:
Exit Sub
ERR_FIND_USER:
MsgBox Error$
Resume EXIT_FIND_USER
End Sub
---------------------------------------------
Function LockStartup() As Boolean
Dim Restart As Boolean
Restart = False
ChangeProperty "StartupShowDBWindow", dbBoolean, False, Restart
ChangeProperty "AllowBuiltinToolbars", dbBoolean, False, Restart
ChangeProperty "AllowFullMenus", dbBoolean, False, Restart
ChangeProperty "AllowToolbarChanges", dbBoolean, False, Restart
ChangeProperty "AllowBreakIntoCode", dbBoolean, False, Restart
ChangeProperty "AllowSpecialKeys", dbBoolean, False, Restart
ChangeProperty "AllowBypassKey", dbBoolean, False, Restart
Application.SetOption "Show Hidden Objects", False
LockStartup = Restart
End Function
-----------------------------------------------
Function UnlockStartup() As Boolean
Dim Restart As Boolean
Restart = False
ChangeProperty "StartupMenuBar", dbText, "(default)", Restart
ChangeProperty "StartupShowDBWindow", dbBoolean, True, Restart
ChangeProperty "StartupShowStatusBar", dbBoolean, True, Restart
ChangeProperty "AllowBuiltinToolbars", dbBoolean, True, Restart
ChangeProperty "AllowFullMenus", dbBoolean, True, Restart
ChangeProperty "AllowToolbarChanges", dbBoolean, True, Restart
ChangeProperty "AllowBreakIntoCode", dbBoolean, True, Restart
ChangeProperty "AllowSpecialKeys", dbBoolean, True, Restart
ChangeProperty "AllowBypassKey", dbBoolean, True, Restart
UnlockStartup = Restart
End Function
-------------------------------------------
Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant, Restart As Boolean) As Integer
Dim dbs As Database, prp As Property
Dim CurrentPropVal As Variant
Const conPropNotFoundError = 3270
Set dbs = CurrentDb
On Error GoTo Change_Err
CurrentPropVal = dbs.Properties(strPropName)
If CurrentPropVal <> varPropValue Then
dbs.Properties(strPropName) = varPropValue
Restart = True 'need to restart database
End If
ChangeProperty = True
Change_Bye:
Exit Function
Change_Err:
If Err = conPropNotFoundError Then 'Property not found.
Set prp = dbs.CreateProperty(strPropName, varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
ChangeProperty = False
Resume Change_Bye
End If
End Function
To get it working just follow these steps:
1. Put the above code in a module in your db.
2. Create a macro called autoexec and put RunCode StartUp() in it.
3. In the StartUp function change the "Designer = " line to your logon id, or change the designer variable to an array of names which can access the database design.
4. In the StartUp function change the docmd.openform command to reference your start up form.