Results 1 to 13 of 13
  1. #1
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40

    Open Password Protected DB with VBA


    I have 2 databases. #1 is for user data entry and#2 is for maintenance. I want on closure of database #1 a macro to be run from database #2 and all databases to be closed. I figured out how to run the macro and close the database using this code:

    Private Sub Command6_Click()
    Call Shell("msaccess ""myDatabasePath.accdb"" /x Update Tables", 1)
    DoCmd.Quit
    End Sub

    The problem I have is if I password protect database #2 it makes me manually enter the password. How can I bypass this or is there an easy fix around this?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    not much point in having a password if you want to bypass it.

    don't think you can do it using shell but you can using opendatabase

    dim db as dao.database

    set db=debengine.opendatabase("myDatabasePath.accdb",0 ,0,";PWD=abcdef")
    Last edited by CJ_London; 11-03-2016 at 01:40 PM. Reason: missed a couple of 0

  3. #3
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    When I tried to use this I get run time error 424 - Object required...am I missing something

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    I can't comment on my own air code, so unless you provide exactly what you have used, I can't really help.

  5. #5
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    This is the code I used. I don't get the error since I fixed the "DBEngine" syntax but now nothing happens when it is run...

    Dim db As DAO.Database

    Set db = dbengine.OpenDatabase("otherDBPath.accdb", 0, 0, ";PWD=Password")

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    think I may have given you the wrong object type to open - see post #2 in this link

    http://www.dbforums.com/showthread.p...-in-Another-DB

  7. #7
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    Now we're getting somewhere. So here is my code now:

    Private Sub Command6_Click()
    Dim acc As Access.Application
    Dim db As DAO.Database
    Dim strDbName As String

    strDbName = "otherDBPath"
    Set acc = New Access.Application
    acc.Visible = True
    Set db = acc.DBEngine.OpenDatabase(strDbName, False, False, ";PWD=Password")
    acc.OpenCurrentDatabase (strDbName)
    acc.DoCmd.RunMacro "Update Tables"

    End Sub

    This opens the other db perfectly but I get an error when trying to run a macro from the 2nd one (acc.DoCmd.RunMacro line)

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    I get an error
    what error?

  9. #9
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    Sorry the error is 2501 "RunMacro action was canceled"

  10. #10
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    If I add "DoCmd.Quit" before End Sub the coding works. However I still get the same error #2501. Maybe an error handler get rid of the error message box?

  11. #11
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    I figured it out. Here is my final coding to share:

    Private Sub Command6_Click()
    Dim acc As Access.Application
    Dim db As DAO.Database
    Dim strDbName As String

    On Error GoTo Error_Handler:

    strDbName = "OtherDBPath.accdb"
    Set acc = New Access.Application
    Set db = acc.DBEngine.OpenDatabase(strDbName, False, False, ";PWD=Password")
    acc.OpenCurrentDatabase (strDbName)
    acc.DoCmd.RunMacro "Update Tables", , ""
    DoCmd.Quit

    Error_Handler:
    If Err.Number <> 2501 Then
    MsgBox Err.Number & ": " & Err.Description
    Else
    DoCmd.Quit
    End If

    End Sub

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    if that is what it requires and your macro has executed successfully

  13. #13
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Oxygen,

    Thank you for posting your actual solution. I was having a similar issue and your code helped me. I wish everyone would put their solutions.

    Thanks,
    Walker

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

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2016, 03:58 AM
  2. Replies: 3
    Last Post: 02-10-2016, 07:14 AM
  3. Open a password protected MDB/MDW
    By abcc14 in forum Security
    Replies: 8
    Last Post: 11-02-2011, 07:41 AM
  4. Replies: 4
    Last Post: 09-14-2011, 12:33 AM
  5. Replies: 1
    Last Post: 10-20-2010, 09:26 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