Results 1 to 6 of 6
  1. #1
    SP117A is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3

    Created a VBA Macro in Access 2007, but no clue how to make it run

    I don't know how to execute a VBA. It's like Access doesn't recognize the VBA I wrote. It only wants to recognize macros created by adding that out of the box functions. How do I write VBA code so it is available to add it to the RunMacro? I just want to have the VBA code execute when the Access database is opened. I ran the code and the code works just fine.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Let's clarify terms. There is VBA code and there are macros; the terms are not interchangeable, and there is no such thing as a "VBA macro" (Excel confuses this issue as they call VBA code a macro). If you want to have VBA code execute when the database is opened, you can either call it using RunCode in a macro named autoexec, or call it from the open event of whatever form opens with the database. For the former, the code would probably have to be a public function in a standard module.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    SP117A is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    Thank you. The challenge is that I don't know how to attach the VBA code to a RunCode.

    Trying to execute the following code probably by using Task Manager to pull up Access. Was trying to have it execute if Access was left open at a certain time, but it never worked. If I ran the VBA code, and it was the correct hour, then it would run properly. The challenge is getting the VBA code to execute.

    Sub autoexec()Dim strNewFileName, strFileName, strFullTargetPath, strSourcePath As String
    Dim strOldEntirePath, strNewEntirePath As String
    Dim InResult As Long
    Dim NewDBName As String, DBName As String
    Dim BackupDB As Object
    strFileName = "TestBackup-Originalv2.accdb"
    strSourcePath = "X:\TestMe\SecureDatabase\"
    strFullTargetPath = "X:\TestMe\SecureDatabase\backup\"
    strOldEntirePath = strSourcePath + strFileName
    If Hour(Now) = 10 Then
    'If Minute(Now) = 50 Then
    strNewFileName = Left(strFileName, InStr(1, strFileName, ".") - 1) & Format(Day(Now()), "yyyymmdd") & ".accdb"
    strNewEntirePath = strFullTargetPath + strNewFileName
    DBEngine.CompactDatabase strOldEntirePath, strNewEntirePath
    'End If
    End If
    End Sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try a macro named autoexec, change the above to a function rather than a sub (I'd change the name for clarity), and call it from the macro with RunCode.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    SP117A is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    3
    Thank you! Works a little different then Excel, but now I am starting to understand how Access is set up. Macro runs the VBA code just fine.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help, and welcome to the site by the way! Excel definitely confuses things. "Record a macro" in Excel creates VBA code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2013, 02:36 PM
  2. access 2007 macro
    By unit91 in forum Queries
    Replies: 17
    Last Post: 04-19-2012, 02:27 PM
  3. Replies: 1
    Last Post: 09-29-2011, 09:39 AM
  4. What id macro in Access 2007
    By Alaali in forum Access
    Replies: 1
    Last Post: 02-25-2010, 02:08 AM
  5. 1st Access application. Needs a clue..
    By Icedog in forum Access
    Replies: 3
    Last Post: 09-24-2008, 09:38 AM

Tags for this Thread

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