Search code examples
ms-accessvbams-access-2010

disabling the bypass key


I would like to disable the bypass key of my database on the open form event during the autoexec so that the user is not able to view the underlying tables of my form. I have found the following code and creatd a module to run upon opening the form during the auto exec. The module is called SetBypass

Call SetBypass

Option Compare Database

Public Function SetByPass(rbFlag As Boolean, File_name As String) As Integer
    DoCmd.Hourglass True
    On Error GoTo SetByPass_Error
    Dim db As Database
    Set db = DBEngine(0).OpenDatabase(File_name)
    db.Properties!AllowBypassKey = rbFlag
setByPass_Exit:
    MsgBox "Changed the bypass key to " & rbFlag & " for database " &     File_name, vbInformation, "Skyline Shared"
    db.Close
    Set db = Nothing
    DoCmd.Hourglass False
    Exit Function


SetByPass_Error:
    DoCmd.Hourglass False
    If Err = 3270 Then
        ' allowbypasskey property does not exist
        db.Pro  perties.Append db.CreateProperty("AllowBypassKey", dbBoolean, rbFlag)

        Resume Next
    Else
        ' some other error message
        MsgBox "Unexpected error: " & Error$ & " (" & Err & ")"
        Resume setByPass_Exit
    End If
End Function  

Solution

  • The above module need to be called from out side the application. Try the below code if you are in same database.

    Sub blockBypass()
    Dim db As Database, pty As DAO.Property
    Set db = CurrentDb
      On Error GoTo Constants_Err 'Set error handler
      db.Properties("Allowbypasskey") = False
      db.Close
    
    Constants_X:
        Exit Sub
    
    Constants_Err:
        If Err = 3270 Then 'Bypass property doesn't exist
    
            'Add the bypass property to the database
            Set pty = db.CreateProperty("AllowBypassKey", dbBoolean _
                , APP_BYPASS)
            db.Properties.Append pty
            Resume Next
    
        End If
        MsgBox Err & " : " & Error, vbOKOnly + vbExclamation _
            , "Error loading database settings"
    
    End Sub