Results 1 to 5 of 5
  1. #1
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77

    Exclamation Unlock VBA editor with a command button


    How can I create a command button with VBA code to unlock the VBA editor. I got the window to open however, I need help with getting my password to auto populate in the window to unlock the editor.

    Code:
    Private Sub openVBA_Click()
         Application.VBE.MainWindow.Visible = True
    End Sub

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    How was the editor locked in the first place? Are you able to open a form in design view and see the code (View code on the ribbon)?

    If you are using an accde file, you can't see or update the code anyway.

  3. #3
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by John_G View Post
    How was the editor locked in the first place? Are you able to open a form in design view and see the code (View code on the ribbon)?

    If you are using an accde file, you can't see or update the code anyway.
    Hello,

    I locked it through the VBA editor. I have the password and I'm able to edit manually. However, my goal is to use a command button to access the VBA editor by entering it through code. Meaning, I press the button the code than opens the editor and enters my password in the API that prompts for it, "VBAproject Password". Below is the code that I have so far and its not working. I can't get it to input the password in the API that prompts the password.
    Code:
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
    As Long
     
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    ByVal hWndParent As Long, _
    ByVal hwndChildAfter As Long, _
    ByVal lpszClass As String, _
    ByVal lpszWindow As String) _
    As Long
     
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hWnd As Long, _
    ByVal Msg As Long, _
    wParam As Any, lParam As Any) _
    As Long
    Private Declare Function SetActiveWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
     
    Private Sub openVBA_Click()
     VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
     
        Dim hDialog As Long
        Dim hTextBox As Long
        Dim hButton As Long
         
        Const WM_SETTEXT = &HC
        Const BM_CLICK = &HF5
         
        Const cPASSWORD As String = "123"
         
         '// Try to find the Project password dialogue
        hDialog = FindWindow(vbNullString, "VBAProject Password")
         
         '// If  NOT found then Exit
        If (hDialog = 0) Then
            MsgBox "Project Password dialog not found...", vbExclamation, "Error"
            Exit Sub
        End If
         
         '// Check if the Text is found in the dialog - The Class ('EDIT', 'BUTTON') names are found using SPY
         '// or similar. These are constant and have not changed since Windows 1!!
        hTextBox = FindWindowEx(hDialog, 0, "Edit", "")
         
         '// If the textbox is NOT found then Exit
        If (hTextBox = 0) Then
            MsgBox "Couldn't find the textbox in the window " & CStr(hDialog) & "...", vbExclamation, "Error"
            Exit Sub
        End If
         
         '// Send text to the textbox
        SendMessage hTextBox, WM_SETTEXT, ByVal 0, ByVal cPASSWORD
         
         '// Now get a handle to the "OK" button in the dialog.
        hButton = FindWindowEx(hDialog, 0, "BUTTON", "OK")
         
        If hButton = 0 Then
            MsgBox "Unable to find the 'OK' button on the Project Password dialog...", vbExclamation, "Error"
        Else
             '// After making sure that the dialog box is the active window, click "OK".
             '// This does not mean it is topmost, simply that it will receive any input
            SetActiveWindow hDialog
            SendMessage hButton, BM_CLICK, ByVal CLng(0), ByVal CLng(0)
        End If
    End Sub

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    However, my goal is to use a command button to access the VBA editor by entering it through code. Meaning, I press the button the code than opens the editor and enters my password in the API that prompts for it, "VBAproject Password".
    I guess I don't understand what you are trying to do.

    If you password protect the code, then have a button that "unlocks" the IDE, what would stop any user from clicking the button to be able to edit the code???

  5. #5
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by ssanfu View Post
    I guess I don't understand what you are trying to do.

    If you password protect the code, then have a button that "unlocks" the IDE, what would stop any user from clicking the button to be able to edit the code???
    I see what you're saying. Let me know explain my objective in a different way. The scope of this is as follows:
    1. I have an admin form that is password protected. So other users can't open it or access the code. in this form is where the button is located.
    2. My goal is to be able to open the VBA editor through code due a few other tools that are build in the DB. They require the VBA editor to be open to import/export a particular form out.

    I hope this helps explain why I need that code to work. The IDE or Password prompt is my currently difficulty. The code is not recognizing it nor is it populating the password in it.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 03-29-2015, 07:42 PM
  2. Replies: 1
    Last Post: 09-12-2014, 06:09 AM
  3. Replies: 3
    Last Post: 08-04-2013, 07:11 AM
  4. Button that open Visual Basic Editor?
    By 95DSM in forum Programming
    Replies: 5
    Last Post: 12-20-2010, 10:40 AM
  5. Replies: 1
    Last Post: 07-27-2010, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums