Solved Locking a Access VBA Project programmatically

Pac-Man

Active member
Local time
Today, 13:25
Joined
Apr 14, 2020
Messages
432
Is it possible to lock a VBA project with VBA instead of doing it manually by going to the Tools menu, select <Database Name> Properties and protection tab?

Best Regards
 
Yes its possible though somewhat pointless as its very easy to bypass the VB project password.

If you really want to do this, then AFAIAA you have to use SendKeys to set the password.

Code:
Sub ProtectVBProject(Pwd As String)

    Dim vbProj As Object

    Set vbProj = Application.VBE.ActiveVBProject
    If vbProj.Protection = 1 Then Exit Sub ' already protected
    Set Application.VBE.ActiveVBProject = vbProj
    SendKeys "%TE+{TAB}{RIGHT}%V%P" & Pwd & "%C" & Pwd & "{TAB}{ENTER}"

End Sub

Typical usage:
ProtectVBProject "ABC"

You need to close & reopen (or compact the database) for this to take effect
 
Thanks a lot. Worked like charm
 
You’re welcome but don’t rely on it for security. It only takes a couple of seconds to bypass the password if you know how.
Yes I know how. But I do it along with other security measures like hiding navpane, setting allowbyoas to false, converting to accde and encrypting it.
 
If you convert to ACCDE, the code is unviewable so no point in setting the VB project password in that case
 
If you convert to ACCDE, the code is unviewable so no point in setting the VB project password in that case
I thought it might make it more secure. Can we see module names in accde?
 
If you had tried this, you would already know the answer
Both Module and procedure names can still be viewed even in an ACCDE and even if a VBA project password is added.
The VBA project password offers minimal extra protection.

At some point you need to ask who or what you are trying to protect your application from.
  1. Unauthorised users?
  2. Authorised users who like to meddle?
  3. Hackers trying to crack your app?
  4. Hackers trying to steal your data?
If its groups 3 & 4, then you should probably be using a SQL Server or similar BE file
 
When VBA project is password protected, modules amd procedures are not visible until password is entered or password is removed by any method. I am concered about keeping safe my app from being cracked and data as well. I am isong BE encrypted with password.
 
When VBA project is password protected, modules amd procedures are not visible until password is entered or password is removed by any method. I am concered about keeping safe my app from being cracked and data as well. I am isong BE encrypted with password.

Sorry but I checked before my last reply. You are incorrect.
I can get a complete list of modules & procedures from an encrypted database with a VBA password without entering (or even knowing the VBA password).
No trickery. No clever code. Its visible to all
 
Yes its possible though somewhat pointless as its very easy to bypass the VB project password.

If you really want to do this, then AFAIAA you have to use SendKeys to set the password.

Code:
Sub ProtectVBProject(Pwd As String)

    Dim vbProj As Object

    Set vbProj = Application.VBE.ActiveVBProject
    If vbProj.Protection = 1 Then Exit Sub ' already protected
    Set Application.VBE.ActiveVBProject = vbProj
    SendKeys "%TE+{TAB}{RIGHT}%V%P" & Pwd & "%C" & Pwd & "{TAB}{ENTER}"

End Sub

Typical usage:
ProtectVBProject "ABC"

You need to close & reopen (or compact the database) for this to take effect
Hello again,

Can I use this method to perform the same operation on VBE project in other db? I could find a way to set active VB project to other db VB project.

Best Regards
 
Sorry but I checked before my last reply. You are incorrect.
I can get a complete list of modules & procedures from an encrypted database with a VBA password without entering (or even knowing the VBA password).
No trickery. No clever code. Its visible to all

I don't think you can get a list of the procedures within the modules can you?
You could just rename the modules, mod1, mod2, mod3 etc to obfuscate what they do.
 
I don't think you can get a list of the procedures within the modules can you?
You could just rename the modules, mod1, mod2, mod3 etc to obfuscate what they do.
Yes you can as I stated in post #10
Not only that, you can also see variable definitions, the arguments for any functions and the values of any constants set in the code
If you're foolish enough to store the BE password in the code, that will also be visible (although there is an easier method of getting that)

As I said visible to all without knowing the VBA password

1679175709081.png
 
Hello again,

Can I use this method to perform the same operation on VBE project in other db? I could find a way to set active VB project to other db VB project.

Best Regards
I doubt it but I've never had a reason to try and I never use SendKeys myself.
I'll leave you to investigate if you think its worth the effort
 
If the VBA password is set, you can see all public declared elements in the object browser.
Everything is visible that has been declared so that it can be seen from the outside.
Declarations with Private and Friend are not visible. Modules with Option Private Module are not visible either.
 
Last edited:
Hi Josef
I had deliberately avoided details but those are indeed important distinctions.
I have never used Option Private Module until now but can confirm your findings. Added to my toolkit. Thanks

Nevertheless, my original point still stands. The VBA password is so easy to bypass that, in my opinion, its of limited use in terms of protection
 
Hi Colin,
I only mentioned this because it is also relevant for an accde.
Ideally, you should only allow access from the outside that you want to be used from the outside.
'Mostly' VBA does exactly what you specify. ;)
 
Last edited:
Hi Josef
That's absolutely fine. Thank you for adding useful information
In fact I've already used Option Private Module, probably for the first time, to hide one of the code modules in the ACCDEs available in this new web page
 
Thanks everyone for explaining. @isladogs yes, If module and variables could be seen then it is not worth the effort.
 

Users who are viewing this thread

Back
Top Bottom