DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Need to call macros commands in Access database from An existing Visual Basic Program

  1. #1
    Bobby Hsiao Guest

    Need to call macros commands in Access database from An existing Visual Basic Program


    I need to know how to call an macros command in an Access Database from an
    existing Visual Basic Program.

    I am creating a command button in Visual Basic 6.0 on a form and would like
    to call a specific macros command in an access database.

    Do you have any example code for this.

    Thanks

    Bobby

  2. #2
    Roy Guest

    Re: Need to call macros commands in Access database from An existing Visual Basic Program

    Set your project references to MS Access x.x object type library and try running this code:

    Public Sub RunAccessMacro(strDB As String, strMacro As String)
    '================================================================
    Dim AccessDB As Access.Application

    Set AccessDB = New Access.Application
    With AccessDB
    .OpenCurrentDatabase strDB
    .DoCmd.RunMacro strMacro, 1
    '.Visible = True 'you decide
    .CloseCurrentDatabase
    End With
    Set AccessDB = Nothing

    End Sub

    Private Sub btnRunMacro_Click()
    '================================

    RunAccessMacro App.Path & "\nwind.mdb", "Suppliers"

    End Sub

    NOTE: the following line "... App.Path & "\nwind.mdb", "Suppliers" will have to be replaced with YOUR database path and YOUR macro
    name.

    Regards, Roy

    "Bobby Hsiao" <vb.@127.0.0.1> wrote in message news:3d80a5ed$1@10.1.10.29...
    >
    > I need to know how to call an macros command in an Access Database from an
    > existing Visual Basic Program.
    >
    > I am creating a command button in Visual Basic 6.0 on a form and would like
    > to call a specific macros command in an access database.
    >
    > Do you have any example code for this.
    >
    > Thanks
    >
    > Bobby




  3. #3
    Bernie Guest

    Re: Need to call macros commands in Access database from An existing Visual Basic Program


    "Roy" <iroy55@hotmail.com> wrote:
    >Set your project references to MS Access x.x object type library and try

    running this
    >code:
    >
    >Public Sub RunAccessMacro(strDB As String, strMacro As String)
    >'================================================================
    >Dim AccessDB As Access.Application
    >
    > Set AccessDB = New Access.Application
    > With AccessDB
    > .OpenCurrentDatabase strDB
    > .DoCmd.RunMacro strMacro, 1
    > '.Visible = True 'you decide
    > .CloseCurrentDatabase
    > End With
    > Set AccessDB = Nothing
    >
    >End Sub
    >
    >Private Sub btnRunMacro_Click()
    >'================================
    >
    > RunAccessMacro App.Path & "\nwind.mdb", "Suppliers"
    >
    >End Sub
    >
    >NOTE: the following line "... App.Path & "\nwind.mdb", "Suppliers" will

    have to be replaced
    >with YOUR database path and YOUR macro
    >name.
    >
    >Regards, Roy
    >
    >"Bobby Hsiao" <vb.@127.0.0.1> wrote in message news:3d80a5ed$1@10.1.10.29...
    >>
    >> I need to know how to call an macros command in an Access Database from

    an
    >> existing Visual Basic Program.
    >>
    >> I am creating a command button in Visual Basic 6.0 on a form and would

    like
    >> to call a specific macros command in an access database.
    >>
    >> Do you have any example code for this.
    >>
    >> Thanks
    >>
    >> Bobby

    >
    >


    Hi,

    Set AccessDB = New Access.Application only works as long as the db doesn't
    have a certain system.mdw with needs of a certain usename and password. If
    so, you need to start it with the shell command (msaccess.exe) and stating
    the database.mdb, wrkgrp (system.mdw), user- and pwd-argument like when you
    start a MSAccess application from a shortcut. Then you need to get the object
    with with GetObject() of the db before continuing the code. GetObject() takes
    a while since it takes some time for Access to launch, meaning you need to
    let the function run over and over again a few seconds until it gets the
    object.
    It's a bit tricky to do it, but it works...

    Bernie


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center