Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    189

    Question Unanswered: How to run VBA code on DB open?

    Hello,
    I'm looking for a way to run a VBA code when the database (an MDB file) is open.

    I'm trying to run a "backup" code like this one:

    Code:
    Sub sCrearBackup()
    'Otra opción: http://www.vbforums.com/showthread.php?t=499872
    Dim strPath, strBackup As String
    
    'Hago la carpeta del BACK
    If Not Dir(Application.CurrentProject.path & "\BACK\", vbDirectory) <> "" Then
        MkDir (Application.CurrentProject.path & "\BACK\")
    End If
    
    strPath = Application.CurrentProject.path & "\" & Application.CurrentProject.Name
    strBackup = Application.CurrentProject.path & "\BACK\" & Application.CurrentProject.Name & " - " & Format(Now, "yyyy-mm-dd HH-mm-ss") & " - " & fNombreUsuario & ".mdb"
    
    'http://support.microsoft.com/kb/172711
    Call Shell("CMD.EXE /C COPY """ & strPath & """ """ & strBackup & """", vbHide)
    
    End Sub
    Any ideas?

    Something like
    Code:
    private sub OpenDB()
    ???

    Thanks!

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    You can create an autoexec macro and call an arbitrary module from that macro.

    Or, one of my preferred methods, create a startup form and fire your modules from one of the form's methods. I like this approach because it allows me to have more granular control over the startup process by exposing a variety of events for me to hook in to.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Apr 2008
    Posts
    189
    And for the close?
    I mean, a way to execute a macro when the MDB is closing?

    Thanks!!!

  4. #4
    Join Date
    Apr 2008
    Posts
    189

    Cool

    Quote Originally Posted by Teddy
    Or, one of my preferred methods, create a startup form and fire your modules from one of the form's methods.
    Ja! Right now I'm using that method.
    As I see, I'm not in the wrong path

  5. #5
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Quote Originally Posted by pepemosca
    And for the close?
    I mean, a way to execute a macro when the MDB is closing?

    Thanks!!!
    That's where this form method can come in handy. If you keep the form open but hide it from the user, you can then use the forms exit/close events to fire off whatever procedures you want on application close.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  6. #6
    Join Date
    Apr 2008
    Posts
    189
    Mmm, I get it.

    So there's no way to do it without this little "trick"?

  7. #7
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    I don't think the application itself has any events you can hook in to.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  8. #8
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Agreed.

    I usually handle that by using the On Close even for my main menu form.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  9. #9
    Join Date
    Sep 2009
    Posts
    1
    Quote Originally Posted by Teddy
    You can create an autoexec macro and call an arbitrary module from that macro.

    Or, one of my preferred methods, create a startup form and fire your modules from one of the form's methods. I like this approach because it allows me to have more granular control over the startup process by exposing a variety of events for me to hook in to.
    would you mind offering some sample code on the latter option suggested? i am a very junior access+vba user and feel this could be a very interesting learning step forward.
    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •