First Published 3 Nov 2023
One of my regular readers, Lorenzo Garuglieri, saw my recent article Open Database with Shift Bypass using Code and emailed me to ask:
If the database you want to open is password protected, how does the procedure work?
An excellent question! The original code won't correctly handle opening external databases that are encrypted with a password.
However, it is easily adapted for that purpose.
The OpenCurrentDatabase command has 3 arguments, two of them optional. The syntax is
Application.OpenCurrentDatabase(filepath As String, Optional Exclusive As Boolean = False, Optional strPassword As String)
The modified code below handles the open exclusively option as well as the password where relevant. It also includes error handling.
The code should be placed in a standard module
CODE:
Option Compare Database
Option Explicit
'====== API DECLARATIONS ========
'Pause code using the Sleep API
#If VBA7 Then 'A2010 or later (32/64-bit)
Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
#Else 'A2007 or earlier
Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
#End If
'Simulate a keystroke
#If VBA7 Then
Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
#Else
Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If
'====== END OF API DECLARATIONS ========
Sub OpenWithShiftBypass(strPath As String, Optional blnExcl As Boolean = False, Optional strPwd As String)
On Error GoTo Err_Handler
Dim appAccess As Access.Application
' Create a new instance of Access
Set appAccess = New Access.Application
' Simulate pressing the Shift key to bypass startup code
keybd_event vbKeyShift, 0, 0, 0
'Open the target database
If strPwd <> "" Then
appAccess.OpenCurrentDatabase strPath, blnExcl, strPwd
Else
appAccess.OpenCurrentDatabase strPath, blnExcl
End If
'OPTIONAL - Make the target database visible
appAccess.Visible = True
' Simulate releasing the Shift key
keybd_event vbKeyShift, 0, 2, 0
' OPTIONAL - Wait 5 seconds (5000 milliseconds) so external app can be viewed before closing it
' NOT NEEDED if app is hidden
Sleep 5000
'OPTIONAL - Close the target database
appAccess.CloseCurrentDatabase
' Quit the new instance of Access
appAccess.Quit
' Release the object
Set appAccess = Nothing
Exit_Handler:
Exit Sub
Err_Handler:
'err 2467 = The expression you entered refers to an object that is closed or doesn't exist.
' occurs if user closed external database manually prior to code running
If Err = 2467 Then Resume Next
MsgBox "Error " & Err.Number & " in OpenWithShiftBypass procedure : " & _
Err.Description, vbOKOnly + vbCritical, "Critical Error"
keybd_event vbKeyShift, 0, 2, 0 'release the Shift key
Resume Exit_Handler
End Sub
Example usage:
a) Open database with no password
OpenWithShiftBypass "G:\MyFiles\ExampleDatabases\Northwind\2023\Northwind22Dev.accdb"
b) Open database encrypted with password: isladogs
OpenWithShiftBypass "G:\MyFiles\ExampleDatabases\ShiftBypass\TestDBPwd.accdb", , "isladogs"
. . . OR use
OpenWithShiftBypass "G:\MyFiles\ExampleDatabases\ShiftBypass\TestDBPwd.accdb", False, "isladogs"
c) Open database exclusively, encrypted with password: letmein
OpenWithShiftBypass "G:\MyFiles\ExampleDatabases\ShiftBypass\AnotherTestDBPwd.accdb", True, "letmein"
Further Information
As before, the above code will not work where the shift bypass property has been disabled.
This is often done to protect databases and ensure required startup code is run.
However, it is also possible to re-enable the shift bypass from another Office application.
The third article in this series will provide code to manage the shift bypass and help protect your databases.
Feedback
Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.
Please also consider making a donation towards the costs of maintaining this website. Thank you
Colin Riddington Mendip Data Systems Last Updated 3 Nov 2023
Return to Code Samples Page
Page 2 of 3
1
2
3
Return To Top
|
|