Results 1 to 17 of 17

Thread: Calling a MSAccess macro

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Calling a MSAccess macro

    Is there a way that I can call a MSAcess macro in a closed database from VB 6.0? Thanks for any help.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Calling a MSAccess macro

    Just use Excel's Run method:
    VB Code:
    1. Dim excl As Excel.Application
    2. Dim wrbk As Workbook
    3.  
    4.     Set excl = New Excel.Application
    5.     excl.DisplayAlerts = False
    6.     Set wrbk = excl.Workbooks.Open("myfile", , True, , "mypassword")
    7.     excl.Run "MacroName", "arg1", "arg2" ...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Calling a MSAccess macro

    Quote Originally Posted by RhinoBull
    Just use Excel's Run method:
    VB Code:
    1. Dim excl As Excel.Application
    2. Dim wrbk As Workbook
    3.  
    4.     Set excl = New Excel.Application
    5.     excl.DisplayAlerts = False
    6.     Set wrbk = excl.Workbooks.Open("myfile", , True, , "mypassword")
    7.     excl.Run "MacroName", "arg1", "arg2" ...

    Thanks, but I am trying to run a macro inside of a MSAccess database that runs a series of queries within that database. Or even call a module inside that DB.... any help is appreciated.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Calling a MSAccess macro

    substitute access for excell it is the same principal

    effectively you open an access object then can access all the properties and methods within including the macro

    rgds pete

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Calling a MSAccess macro

    Quote Originally Posted by Besoup
    Thanks, but I am trying to run a macro inside of a MSAccess database that runs a series of queries within that database. Or even call a module inside that DB.... any help is appreciated.
    Oops, sorry about that ... here we go:
    VB Code:
    1. Public Sub RunAccessMacro(strDB As String, strMacro As String)
    2. '================================================================
    3. 'for late binding declare it As Object and use CreateObject() function
    4. Dim AccessDB As Access.Application
    5.  
    6.     Set AccessDB = New Access.Application
    7.     With AccessDB
    8.         .OpenCurrentDatabase strDB
    9.         .DoCmd.RunMacro strMacro, 1
    10.         '.Visible = True    'you decide
    11.         .CloseCurrentDatabase
    12.     End With
    13.     Set AccessDB = Nothing
    14.  
    15. End Sub

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Calling a MSAccess macro

    Quote Originally Posted by RhinoBull
    Oops, sorry about that ... here we go:
    VB Code:
    1. Public Sub RunAccessMacro(strDB As String, strMacro As String)
    2. '================================================================
    3. 'for late binding declare it As Object and use CreateObject() function
    4. Dim AccessDB As Access.Application
    5.  
    6.     Set AccessDB = New Access.Application
    7.     With AccessDB
    8.         .OpenCurrentDatabase strDB
    9.         .DoCmd.RunMacro strMacro, 1
    10.         '.Visible = True    'you decide
    11.         .CloseCurrentDatabase
    12.     End With
    13.     Set AccessDB = Nothing
    14.  
    15. End Sub
    Thanks, tried this but keep getting "User-defined type not defined". Besides that the rest looks like exactly what I am looking for with the visibity false ofcourse.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calling a MSAccess macro

    You must add a reference to the Microsoft Access xx.x Object Library

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Calling a MSAccess macro

    Quote Originally Posted by Besoup
    Thanks, tried this but keep getting "User-defined type not defined". Besides that the rest looks like exactly what I am looking for with the visibity false ofcourse.
    You should've had simply follow my instructions:

    'for late binding declare it As Object and use CreateObject() function

    VB Code:
    1. Dim AccessDB As Object
    2.  
    3.     Set AccessDB = CreateObject(Access.Application)
    4.     'rest is the same

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calling a MSAccess macro

    But either way, the reference must still be there.

  10. #10

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calling a MSAccess macro

    For early binding a reference must be added to your project. For late binding no
    references are required. Either way, the target system must have the
    required dependency files installed.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calling a MSAccess macro

    I have Access installed, and it doesn't work for late binding. Says that Access is not defined.

    VB Code:
    1. Public Sub RunAccessMacro(strDB As String, strMacro As String)
    2. '================================================================
    3. 'for late binding declare it As Object and use CreateObject() function
    4. Dim AccessDB As Object
    5.  Set AccessDB = CreateObject(Access.Application)
    6.  
    7.     With AccessDB
    8.         .OpenCurrentDatabase strDB
    9.         .DoCmd.RunMacro strMacro, 1
    10.         '.Visible = True    'you decide
    11.         .CloseCurrentDatabase
    12.     End With
    13.     Set AccessDB = Nothing
    14.  
    15. End Sub

    Compiler Error: Variable Not Defined.

  14. #14

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calling a MSAccess macro

    That should do it, just like RB said. "ClassName.ServerName" as string.
    Edit: I think there is an echo here?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calling a MSAccess macro

    That did it!
    Last edited by dglienna; Feb 2nd, 2005 at 09:23 PM.

  17. #17

Posting Permissions

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



Click Here to Expand Forum to Full Width